Logo 
Search:

Java Answers

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds
  Question Asked By: Lorraine Stephens   on Mar 05 In Java Category.

  
Question Answered By: Zachary Larson   on Mar 05

Your program  is similar to the following. Perhaps you can see how it works
and modify your code.
Unless you put  the text of your code in your email, we can't see it either.
This has a
try{

}catch(){
}
block.
String s = "111";
int n = -1
try{
n = Integer.parseInt(s);
}catch(NumberFormatException nfe){
System.err.println(nfe.getMessage());
}
block.


If you are using the Date class  for your time,
Date myDate = new Date();
long timeInMilliseconds = myDate.getTime();

If you are using the Calendar class for your time,
Calendar myCalendar = Calendar .getInstance();
Date myDate = myCalendar .getTime();
long timeInMilliseconds = myDate.getTime();


/* AgeProcessor.java
* March 6, 2003
*/

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
import java.util.Calendar;

public class AgeProcessor{

/* Retirement age is 65. */
private int retirementAge = 65;

/* Constructor. */
public AgeProcessor(){

}

/** Performs the following age calculations:
* How long it is until you retire assuming you are 30 now.
* How long it is until you retire.
* Given your year of bith, how old you are in years
* The combined ages of all of your family
* The average age of all your family members
* Uses console for user input and output.
*/
public static void main(String[] args){

AgeProcessor processor = new AgeProcessor();

/* Output how long it is until you retire if your age is 30. */
int yearsToRetirement = processor.getYearsToMyRetirement(30);
processor.displayMessage("You have "
+ String.valueOf(yearsToRetirement)
+ " years until you retire if you are 30 years old.");

/* Output how long it is until you retire. */
int yearsToMyRetirement = processor.getYearsToMyRetirement();
processor.displayMessage("You have "
+ String.valueOf(yearsToMyRetirement)
+ " years until you retire.");

/* Given your year of bith, output how old you are in years. */
int yearOfBirth = processor.getYearOfBirth();
int monthOfBirth = processor.getMonthOfBirth();
int dayOfBirth = processor.getDayOfBirth();
int age = processor.getAge(yearOfBirth, monthOfBirth, dayOfBirth);
processor.displayMessage("You are "
+ String.valueOf(age)
+ " years old.");

/* Output combined ages of all of your family. */
int combinedFamilyAge = processor.getCombinedFamilyAge();
processor.displayMessage("The combined ages of all of your "
+ " family is "
+ String.valueOf(combinedFamilyAge)
+ ".");

/* Output average age of all your family members. */
int averageFamilyAge = processor.getAverageFamilyAge();
processor.displayMessage("The average age of all your "
+ " family members is "
+ String.valueOf(averageFamilyAge)
+ ".");
}

/** Calculates years to retirment based on the
* cuurent age input.
*/
public int getYearsToMyRetirement(int currentAge){
return retirementAge - currentAge;
}

/** Calculates years to retirment based on the
* age determined by user input.
*/
public int getYearsToMyRetirement(){
int n = -1;
while (n == -1){
n = getInput("Enter your age:");
}
return getYearsToMyRetirement(n);
}


/** Obtains year of birth from user user input.
*/
public int getYearOfBirth(){
int n = -1;
while (n == -1){
n = getInput("Enter year of birth.");
}
return n;
}

/** Obtains month of birth from user user input.
*/
public int getMonthOfBirth(){
int n = -1;
while (n == -1){
n = getInput("Enter month of birth: (1 = January 12 = December");
}
return n;
}
/** Obtains day of birth from user user input.
*/
public int getDayOfBirth(){
int n = -1;
while (n == -1){
n = getInput("Enter day of month of birth:");
}
return n;
}

/** Calculates age based on todays date and the
* input year of birth.
*/
public int getAge(int yearOfBirth, int monthOfBirth, int dayOfBirth){
Calendar now = Calendar.getInstance();
int currentYear = now.get(Calendar.YEAR);
Calendar birth = Calendar.getInstance();
/* Months start at 0 in Calendar. */
birth.set(yearOfBirth, monthOfBirth -1, dayOfBirth);
int years = currentYear - yearOfBirth;
int age = years;
birth.roll(Calendar.YEAR, age);
if (birth.after(now)) age = age - 1;
return age;
}

/** Calculates the combined age of all family members
* based upon usewr input.
*/
public int getCombinedFamilyAge(){
int n = -1;
int combinedAge = 0;
while (n == -1){
n = getInput("Enter the number of family members.");
}
int familyMembers = n;
for (int i = 0; i < familyMembers; i++){
n = -1;
while (n == -1){
n = getInput("Enter the age of family member: "
+ String.valueOf(i + 1) + ".");
}
combinedAge = combinedAge + n;
}
return combinedAge;
}

/** Calculates the average age of all family members
* based upon usewr input.
*/
public int getAverageFamilyAge(){
int n = -1;
int combinedAge = 0;
while (n == -1){
n = getInput("Enter the number of family members.");
}
int familyMembers = n;
for (int i = 0; i < familyMembers; i++){
n = -1;
while (n == -1){
n = getInput("Enter the age of family member: "
+ String.valueOf(i + 1) + ".");
}
combinedAge = combinedAge + n;
}
int averageAge = 0;
if (familyMembers != 0) averageAge = combinedAge/familyMembers;
return averageAge;
}

/** Displays the input message on the console to
* prompt the user for input. Returns a line of
* text entered from the console.
*/
private int getInput(String message){
int n = -1;
try{
System.out.println(message);
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr);
String text = br.readLine();
n = toInt(text);
}catch(IOException ioe){
System.err.println("IOException: " + ioe.getMessage());
}
return n;
}

/** Converts a string to an int value. Returns -1 if
* conversion fails.
*/
private int toInt(String s){
int n = -1;
try{
n = Integer.parseInt(s);
}catch(NumberFormatException nfe){
System.err.println("NumberFormatException: " +
nfe.getMessage());
}
return n;
}

/** Displays the input message to the console.
*/
private void displayMessage(String message){
System.out.println(message);
}
}

Share: 

 

This Question has 1 more answer(s). View Complete Question Thread

 
Didn't find what you were looking for? Find more on Help in java program Or get search suggestion and latest updates.


Tagged: