The Artima Developer Community
Sponsored Link

Java Answers Forum
Arithmetic java operators

3 replies on 1 page. Most recent reply: Nov 21, 2002 10:05 PM by Daniel Ray

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 3 replies on 1 page
sean foley

Posts: 3
Nickname: foleysf
Registered: Nov, 2002

Arithmetic java operators Posted: Nov 14, 2002 11:14 AM
Reply to this message Reply
Advertisement
could someonw please give the solution to the following problem - you must use the 6 integers to calculate the persons present age, much appriecated foleysf.

must use if-else and nested else-if structures as well as the arithmetic, relational and logical operators.

. Read in three integers representing today's date (day, month, year), followed by three integers representing a person's birth-date, and use these 6 values to calculate the person's age in years.


Daniel Ray

Posts: 53
Nickname: budoray
Registered: Oct, 2002

Re: Arithmetic java operators Posted: Nov 14, 2002 12:12 PM
Reply to this message Reply
if(todaysYear > birthYear){
age = todaysYear - birthYear;

if(todaysMonth = birthMonth){

if(todaysDay < birthDay){
age--;

}


} else if(todaysMonth < birthMonth){
age--;

}

} else if(todaysYear == birthYear){
if(todaysMonth == birthMonth){
if(todaysDay == birthDay){
// Print person was born today.

}

}

} else {
// Print person is born in the future.

}

Haven't tested this code and it's basically brute force and procedural. I would choose to use a GregorianCalendar object or Calendar object, but my guess is that you are learning control statements.

Ray

sean foley

Posts: 3
Nickname: foleysf
Registered: Nov, 2002

Re: Arithmetic java operators Posted: Nov 14, 2002 1:28 PM
Reply to this message Reply
//set2_11
/*this program reads in a variables representing todays date
and reads in a variables to repersent a persons age and uses the else if statement*/

public class set2_11 {
public static void main(String args[])
{

int todaysYear,birthYear,todaysMonth,birthMonth,todaysDay,birthDay;

System.out.print("please enter todays calender day:");
todaysDay=UserInput.getInt();

System.out.print("\nplease enter the month of year it is:");
todaysMonth=UserInput.getInt();

System.out.print("\nPlease enter the year it is:");
todaysYear=UserInput.getInt();

System.out.print("\n\nplease enter the calender day you were born:");
birthDay=UserInput.getInt();

System.out.print("\n\nPlease enter the month you were born:");
birthMonth=UserInput.getInt();

System.out.print("\n\nPlease enter the year you were born:");
birthYear=UserInput.getInt();

if(todaysYear > birthYear){
age = todaysYear - birthYear;
System.out.println("the age of the person is:" + (age));
}


}


}
thanks for that reply ray , I can't seem to get it to compile without errors this stuff is new to me if you could intrepret the code into this program it would be great. regards sf.

Daniel Ray

Posts: 53
Nickname: budoray
Registered: Oct, 2002

Re: Arithmetic java operators Posted: Nov 21, 2002 10:05 PM
Reply to this message Reply
Sorry about just getting back to this. Real world is a pain. Where's a good MUD when you need one? Anyhow, the code was really just pseudo code and is incomplete. If you still need help with a working example .. Hit me with an email.

Ray

Flat View: This topic has 3 replies on 1 page
Topic: Java Cerification Previous Topic   Next Topic Topic: open pdf files  from java application with out using a browser

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use