Commit 6b7cad1f authored by jinny.wilkin's avatar jinny.wilkin

Week2Fin

parents
{
"java.project.sourcePaths": ["src"],
"java.project.outputPath": "bin",
"java.project.referencedLibraries": [
"lib/**/*.jar"
]
}
## Getting Started
Welcome to the VS Code Java world. Here is a guideline to help you get started to write Java code in Visual Studio Code.
## Folder Structure
The workspace contains two folders by default, where:
- `src`: the folder to maintain sources
- `lib`: the folder to maintain dependencies
Meanwhile, the compiled output files will be generated in the `bin` folder by default.
> If you want to customize the folder structure, open `.vscode/settings.json` and update the related settings there.
## Dependency Management
The `JAVA PROJECTS` view allows you to manage your dependencies. More details can be found [here](https://github.com/microsoft/vscode-java-dependency#manage-dependencies).
File added
File added
public class App {
public static void main(String[] args) throws Exception {
/* Week 2 Exercises
14/02 Jinny Wilkin */
int fingersOnRightHand = 5; // Ex 4.1
countDays(); // Ex 8.1
circumference(3); // Ex 8.2
System.out.println(inigoSpeech(5)); // Ex 8.4
int month = 1; // Ex 5.1
int monthDo = 1;
for (int i =1; i < 14; i++){ // Ex 6.1
switch (i){ // Ex 5.2 & 6.2
case 1:
System.out.println("January");
break;
case 2:
System.out.println("February");
break;
case 3:
System.out.println("March");
break;
case 4:
System.out.println("April");
break;
case 5:
System.out.println("May");
break;
case 6:
System.out.println("June");
break;
case 7:
System.out.println("July");
break;
case 8:
System.out.println("August");
break;
case 9:
System.out.println("September");
break;
case 10:
System.out.println("October");
break;
case 11:
System.out.println("November");
break;
case 12:
System.out.println("December");
break;
default:
System.out.println("Not a valid month");
}
}
while(month < 14){ // Ex 7.1
switch (month){ // Ex 5.2 & 6.2
case 1:
System.out.println("January");
break;
case 2:
System.out.println("February");
break;
case 3:
System.out.println("March");
break;
case 4:
System.out.println("April");
break;
case 5:
System.out.println("May");
break;
case 6:
System.out.println("June");
break;
case 7:
System.out.println("July");
break;
case 8:
System.out.println("August");
break;
case 9:
System.out.println("September");
break;
case 10:
System.out.println("October");
break;
case 11:
System.out.println("November");
break;
case 12:
System.out.println("December");
break;
default:
System.out.println("Not a valid month");
}
month++;
}
do{ // Ex 7.2
switch (monthDo){ // Ex 5.2 & 6.2
case 1:
System.out.println("January");
break;
case 2:
System.out.println("February");
break;
case 3:
System.out.println("March");
break;
case 4:
System.out.println("April");
break;
case 5:
System.out.println("May");
break;
case 6:
System.out.println("June");
break;
case 7:
System.out.println("July");
break;
case 8:
System.out.println("August");
break;
case 9:
System.out.println("September");
break;
case 10:
System.out.println("October");
break;
case 11:
System.out.println("November");
break;
case 12:
System.out.println("December");
break;
default:
System.out.println("Not a valid month");
}
monthDo++;
}while(monthDo < 14);
System.out.println("There are " + daysBetween(26, 8, 1996, 14, 2, 2022));
}
public static void countDays(){ // Ex 8.1
int daysPerWeek = 7; // Ex 2.1
int numberOfWeeks = 6; // Ex 2.2
int totalDays = numberOfWeeks * daysPerWeek; // Ex 2.3
System.out.println(totalDays + " days");
boolean onHoliday = false; // Ex 2.4
if(onHoliday){
System.out.println("I am currently on Holiday");
}else{
System.out.println("I am currently available");
}
}
public static void circumference(int radius){ // Ex 8.2 & 8.3
float approxPi = 3.14f; // Ex 2.5
System.out.println("The circumference of a circle with radius " + radius + " is approximately " + 2 * approxPi * radius);
}
public static String inigoSpeech(int numFingers){
String greetingString = "Hello,"; // Ex 2.7
String introductionString = " My name is Inigo Montoya,"; // Ex 2.8
String reminderString = " You killed my father,"; // Ex 2.9
String threatString = " Prepare to die!"; // Ex 2.10
if (numFingers == 6){ // Ex 4.2
return(greetingString + introductionString + reminderString + threatString); // Ex 2.11
} else {
return("I am searching for the man who killed my father"); // Ex 4.5
}
}
public static boolean isItALeapYear(int year){
if(year % 4 == 0){
return(true);
}else{
return(false);
}
}
public static int daysInYear(int year){
if(isItALeapYear(year)){
return(366);
}else{
return(365);
}
}
public static int daysInMonth (int month, int year){
if(month == 4 || month == 6 || month == 9 || month == 11){
return(30);
}else if (month == 2){
if(isItALeapYear(year)){
return(29);
}else{
return(28);
}
}else{
return(31);
}
}
public static int daysToYearEnd(int day, int month, int year){
int dayCount = 0;
day++; //discount the start day
while(month <=12){
for(int i = day; i <= daysInMonth(month, year); i++){
dayCount++;
}
day = 1;
month++;
}
return(dayCount);
}
public static int daysFromYearStart(int day, int month, int year){
int dayCount = 0;
while(month > 0){
for(int i = day; i > 0; i--){
dayCount++;
}
month--;
day = daysInMonth(month, year);
}
return dayCount;
}
public static int daysBetween(int startDay, int startMonth, int startYear, int endDay, int endMonth, int endYear){
int totalCount = 0;
totalCount += daysToYearEnd(startDay, startMonth, startYear);
totalCount += daysFromYearStart(endDay, endMonth, endYear);
startYear++;
while(startYear < endYear){
totalCount += daysInYear(startYear);
startYear++;
}
return(totalCount);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment