Created
December 31, 2012 06:17
-
-
Save jsc230/4417796 to your computer and use it in GitHub Desktop.
This is from exercise 13-2 (using dates) in Murach's Java Programming 4. I have the program written but the age comes out incorrect. If you use my birthday Feb 17, 1982 it gives an age of 662. I checked the math by hand and it should give the correct result of 30. If you look at the calculateAge() function I have outputs for each step of the cal…
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.*; | |
import java.text.DateFormat; | |
public class AgeCalculatorApp | |
{ | |
private static Date today = new Date(); | |
private static GregorianCalendar gregBirthDate = new GregorianCalendar(); | |
private static Date birthDate = new Date(); | |
private static DateFormat formattedDate = DateFormat.getDateInstance(DateFormat.SHORT); | |
public static void main(String[] args) | |
{ | |
//Get the user's birthdate | |
System.out.println("Welcome to the age calculator."); | |
Scanner sc = new Scanner(System.in); | |
int birthMonth = Validator.getInt(sc, "Enter the month you were born (1 to 12): ", | |
0, 13) - 1; | |
int birthDay = Validator.getInt(sc, "Enter the day of the month you were born: ", | |
0, 32); | |
int birthYear = Validator.getInt(sc, "Enter the year you were born (four digits): ", | |
1874, 2012); | |
long age = calculateAge(birthMonth, birthDay, birthYear); | |
String todaysDate = formattedDate.format(today); | |
String formattedBDay = formattedDate.format(birthDate); | |
System.out.println("Your birth date is: " + formattedBDay); | |
System.out.println("Today's date is: " + todaysDate); | |
System.out.println("Your age is: " + age); | |
} | |
public static long calculateAge(int birthMonth, int birthDay, int birthYear){ | |
long age = 0; | |
gregBirthDate.set(birthYear, birthMonth, birthDay); | |
birthDate = gregBirthDate.getTime(); | |
long birthDateMS = birthDate.getTime(); | |
long todayMS = today.getTime(); | |
long elapsedMS = todayMS - birthDateMS; | |
age = elapsedMS / (1000 * 60 * 60 * 24 * 365); | |
//checking the math one step at a time | |
System.out.println(elapsedMS); | |
System.out.println(elapsedMS / 1000); | |
System.out.println(elapsedMS /(1000 * 60)); | |
System.out.println(elapsedMS /(1000 * 60 * 60)); | |
System.out.println(elapsedMS /(1000 * 60 * 60 * 24)); | |
System.out.println(elapsedMS /(1000 * 60 * 60 * 24 * 365)); | |
return age; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Scanner; | |
public class Validator | |
{ | |
public static String getString(Scanner sc, String prompt) | |
{ | |
System.out.print(prompt); | |
String s = sc.next(); // read the first string on the line | |
sc.nextLine(); // discard any other data entered on the line | |
return s; | |
} | |
public static String getLine(Scanner sc, String prompt) | |
{ | |
System.out.print(prompt); | |
String s = sc.nextLine(); // read the whole line | |
return s; | |
} | |
public static int getInt(Scanner sc, String prompt) | |
{ | |
boolean isValid = false; | |
int i = 0; | |
while (isValid == false) | |
{ | |
System.out.print(prompt); | |
if (sc.hasNextInt()) | |
{ | |
i = sc.nextInt(); | |
isValid = true; | |
} | |
else | |
{ | |
System.out.println("Error! Invalid integer value. Try again."); | |
} | |
sc.nextLine(); // discard any other data entered on the line | |
} | |
return i; | |
} | |
public static int getInt(Scanner sc, String prompt, | |
int min, int max) | |
{ | |
int i = 0; | |
boolean isValid = false; | |
while (isValid == false) | |
{ | |
i = getInt(sc, prompt); | |
if (i <= min) | |
System.out.println( | |
"Error! Number must be greater than " + min); | |
else if (i >= max) | |
System.out.println( | |
"Error! Number must be less than " + max); | |
else | |
isValid = true; | |
} | |
return i; | |
} | |
public static double getDouble(Scanner sc, String prompt) | |
{ | |
boolean isValid = false; | |
double d = 0; | |
while (isValid == false) | |
{ | |
System.out.print(prompt); | |
if (sc.hasNextDouble()) | |
{ | |
d = sc.nextDouble(); | |
isValid = true; | |
} | |
else | |
{ | |
System.out.println("Error! Invalid decimal value. Try again."); | |
} | |
sc.nextLine(); // discard any other data entered on the line | |
} | |
return d; | |
} | |
public static double getDouble(Scanner sc, String prompt, | |
double min, double max) | |
{ | |
double d = 0; | |
boolean isValid = false; | |
while (isValid == false) | |
{ | |
d = getDouble(sc, prompt); | |
if (d <= min) | |
System.out.println( | |
"Error! Number must be greater than " + min); | |
else if (d >= max) | |
System.out.println( | |
"Error! Number must be less than " + max); | |
else | |
isValid = true; | |
} | |
return d; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Here's a three year necrobump for you, probs too late to help with what you were doing.
Here's the line that was messing you up:
AgeCalculatorApp
age = elapsedMS / (1000 * 60 * 60 * 24)/ (365);
Java doesn't like huge multiply strings, so if you collapse all the numbers into one (ie: 86400000), then divide by 365, you'll get the correct output.