Kaynağa Gözat

(fix) Bugfix "How many years old are you" exercise

It blithely ignored unimportant things like what day of the year it is today and
in the DOB.

Note that Java Calendar instances are zero-indexed, so Calendar.set(1990, 12, 1)
sets the object to 1st January 1991, not 1st Dec 1990 (!!)
main
Rob Hallam 1 yıl önce
ebeveyn
işleme
1ffe5ad5b6
1 değiştirilmiş dosya ile 146 ekleme ve 19 silme
  1. +146
    -19
      week1.java

+ 146
- 19
week1.java Dosyayı Görüntüle

@@ -6,9 +6,9 @@ import java.util.concurrent.ThreadLocalRandom;
class runLabs {
public static void main(String[] args) {
new Lab4();
new Lab5();
new Lab7();
new Lab6();
// new Lab5();
// new Lab6();
// new Lab7();
}
}

@@ -74,7 +74,7 @@ class Lab {
class Lab4 extends Lab {
public Lab4() {
super(4);
ex44();
// ex44();
ex46();
}

@@ -147,8 +147,7 @@ class Lab4 extends Lab {
*
* Write a program that can calculate how many whole years old someone is from
* today’s date and their date of birth. You could store each date as three
* integers (day,
* month, year)
* integers (day, month, year)
*/
public static void ex46() {
String labSection = "4.6";
@@ -164,15 +163,40 @@ class Lab4 extends Lab {
System.out.println(String.format(
"DOB 1:\t%s (difference:\t%d years)",
dob1.getTime(),
yearDifference(today, dob1)));
yearsOld(today, dob1)));

Calendar dob2 = Calendar.getInstance();
dob2.set(1994, 12, 22);
dob2.set(1994, 11, 22);

System.out.println(String.format(
"DOB 2:\t%s (difference:\t%d years)",
dob2.getTime(),
yearDifference(today, dob2)));
yearsOld(today, dob2)));

Calendar dob3 = Calendar.getInstance();
dob3.set(2000, 8, 19); // 23 years old

System.out.println(String.format(
"DOB 3:\t%s (difference:\t%d years)",
dob3.getTime(),
yearsOld(today, dob3)));

Calendar dob4 = Calendar.getInstance();
dob4.set(2000, 8, 20); // 23 years old

System.out.println(String.format(
"DOB 4:\t%s (difference:\t%d years)",
dob4.getTime(),
yearsOld(today, dob4)));

Calendar dob5 = Calendar.getInstance();
dob5.set(2000, 8, 21); // 22 years old

System.out.println(String.format(
"DOB 5:\t%s (difference:\t%d years)",
dob5.getTime(),
yearsOld(today, dob5)));

}

public static boolean isLeap(int year) {
@@ -208,10 +232,25 @@ class Lab4 extends Lab {
}
}

public static int yearDifference(Calendar date1, Calendar date2)
public static int yearsOld(Calendar laterDate, Calendar earlierDate)
{
return (int) Math.abs(date1.get(Calendar.YEAR) -
date2.get(Calendar.YEAR)
int birthdayModifier = 0; // we assume they've had their birthday

// we could probably do this with one if, checking DAY_OF_YEARs,
// but since problem spec mentioned months and days we should really
// pay attention to those
if (earlierDate.get(Calendar.MONTH) > laterDate.get(Calendar.MONTH)) {
// ie 8 (August) < 9 (September)
birthdayModifier = -1; // not had their birthday yet so take 1 year off difference
} else if (earlierDate.get(Calendar.MONTH) == laterDate.get(Calendar.MONTH)) {
if (earlierDate.get(Calendar.DAY_OF_MONTH) > laterDate.get(Calendar.DAY_OF_MONTH)) {
birthdayModifier = -1;
}
}

return (int) Math.abs(laterDate.get(Calendar.YEAR) -
earlierDate.get(Calendar.YEAR) +
birthdayModifier
);
}
}
@@ -222,7 +261,10 @@ class Lab4 extends Lab {
class Lab5 extends Lab {
public Lab5() {
super(5);
new five1();
// new five1();
// new five5();
// new five6();
new five7();
}

class five1 extends Exercise {
@@ -235,12 +277,97 @@ class Lab5 extends Lab {
*/
public five1() {
super(1);
System.out.println(String.format("TBC"));
// Scanner keyboard = new Scanner(System.in);
// System.out.println("Please enter your name, followed by the return key?");
// String userEntry = keyboard.nextLine();
// System.out.println("Hello " + userEntry);
// System.out.println(String.format("%s", keyboard));
Scanner keyboard = new Scanner(System.in);
System.out.println("Please enter your name, followed by the return key?");
String userEntry = keyboard.nextLine();
System.out.println("Hello " + userEntry);
System.out.println(String.format("%s", keyboard));

System.out.println("Please enter your age");
int userAge = keyboard.nextInt();
// keyboard.nextLine();
System.out.println("You are " + userAge + " years old");
System.out.println("What is your favourite colour?");
String col = keyboard.nextLine();
}
}

/**
* Prompt for input and summarise a circle
*
*/
class five5 extends Exercise {
public five5() {
super(5);
userCircles();
}

public void userCircles() {
Scanner kbIn = new Scanner(System.in);
System.out.println(String.format("Please supply a radius, that I might describe you a circle"));
double radius = kbIn.nextDouble();
kbIn.nextLine();
summariseCircle(radius);
kbIn.close();
}

public void summariseCircle(double radius) {
System.out.println(String.format("A circle with radius %.2f has circumference %.2f and radius %.2f",
radius, compCircumference(radius), compArea(radius)));
}

public double compCircumference(double radius) {
return (2 * Math.PI * radius);
}

public double compArea(double radius) {
return (Math.PI * Math.pow(radius, 2));
}
}

/**
* {@link Scanner} understanding - actually process input
*
*/
class five6 extends Exercise {
public five6() {
super(6);
getNameAndAge();
}

public void getNameAndAge() {
String userName;
int userAge;

Scanner kbIn = new Scanner(System.in);
System.out.println(String.format("Please enter your name and age separated by a space"));
String userInput = kbIn.nextLine();
Scanner inputOps = new Scanner(userInput);
userName = inputOps.next();
// TOASK -- is this what 'add an extra line that fixes this problem' was going for?
while (!inputOps.hasNextInt()) {
userName += " " + inputOps.next();
}
if (inputOps.hasNextInt()) {
userAge = inputOps.nextInt();
} else {
userAge = 0;
}

System.out.println(String.format("Your name is %s and your age is %d",
userName, userAge));
kbIn.close();
inputOps.close();
}
}

/**
* Get DOB from user input and calculate age using {@link Lab4.ex46()}
*
*/
class five7 extends Exercise {
public five7() {
super(7);
}
}
}


Yükleniyor…
İptal
Kaydet