943,682 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Marked Solved
  • Views: 839
  • Java RSS
Apr 11th, 2008
0

Age class and associated methods

Expand Post »
I need to design an Ages class with fields to hold 3 ages and includes a constructor, accessor and mutator method. Also needed is a method that returns the average of the ages.

I also need to write a demo class that creates an instance of Ages class. The demo needs to ask the user to enter 3 ages to be stored in the Ages object. Then the demo displays the average of the ages, as reported by the ages object.

I've written the the 1st program, which compiles o.k. But, I'm not sure it is correct.

Can someone take a look at my 1st class to see if I've set it up properly? Also, can someone give me some direction on how to write the demo program?

Thanks in advance.
Java Syntax (Toggle Plain Text)
  1. /**
  2. Ages Program
  3. */
  4. public class Ages
  5. {
  6. //Instance variables
  7. private int age1; //holds 1st age
  8. private int age2; //holds 2nd age
  9. private int age3; //holds 3rd age
  10.  
  11. //Constructor Method
  12. public Ages()
  13. {
  14. //Initialize ages to 0
  15. age1=0;
  16. age2=0;
  17. age3=0;
  18. }
  19.  
  20. public void setAges (int i, int age)
  21. {
  22. //Set i to age
  23. if (i==1) age1=age;
  24. else if (i==2) age2=age;
  25. else age3=age;
  26. }
  27.  
  28. public int getAges(int i)
  29. {
  30. //Accessor Method to return ages
  31. if (i==1) return age1;
  32. else if (i==2) return age2;
  33. else return age3;
  34. }
  35. public int getAvg()
  36. {
  37. //Calcuate and return average of 3 ages
  38. int ageAvg;
  39. ageAvg=(int) Math.round((age1+age2+age3)/3.0);
  40. return ageAvg;
  41. }
  42. }
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
clueless101 is offline Offline
52 posts
since Mar 2008
Apr 11th, 2008
0

Re: Age class and associated methods

tell us what you think is wrong, and we may tell you whether you're right.
Team Colleague
Reputation Points: 1658
Solved Threads: 331
duckman
jwenting is offline Offline
7,719 posts
since Nov 2004
Apr 11th, 2008
0

Re: Age class and associated methods

Actually, I considered the 1st part done.

Am I wrong?

Thanks!
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
clueless101 is offline Offline
52 posts
since Mar 2008
Apr 11th, 2008
0

Re: Age class and associated methods

Here's what I've completed for demo.

Can anyone tell me why my average displays as "0"????

Thanks in advance.


Java Syntax (Toggle Plain Text)
  1. import java.util.Scanner;
  2.  
  3. public class DemoAges
  4. {
  5. public static void main(String[] args)
  6. {
  7. //Declare/initialize Ages
  8. int age1, age2, age3;
  9. int avgAge=0;
  10.  
  11. Scanner scan = new Scanner (System.in);
  12. //Input Ages
  13. System.out.println("Enter 1st Age: ");
  14. age1=scan.nextInt();
  15. System.out.println("Enter 2nd Age: ");
  16. age2=scan.nextInt();
  17. System.out.println("Enter 3rd Age: ");
  18. age3=scan.nextInt();
  19.  
  20. //Print average scores
  21. System.out.println("Average Age: " + avgAge);
  22. }
  23. }
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
clueless101 is offline Offline
52 posts
since Mar 2008
Apr 11th, 2008
0

Re: Age class and associated methods

The demo class needs to create "Ages" objects and use the methods you have defined. As written your demo just defines some variables and prints the "avgAge" - which is still 0 because you haven't done anything with it.
Moderator
Featured Poster
Reputation Points: 3239
Solved Threads: 838
Posting Genius
Ezzaral is offline Offline
6,757 posts
since May 2007
Apr 11th, 2008
-1

Re: Age class and associated methods

You need this:
Java Syntax (Toggle Plain Text)
  1. public class DemoAges
  2. {
  3. private Ages ages = new Ages();
  4.  
  5. public static void main(String[] args)
  6. {
  7. //Declare/initialize Ages
  8. int age = 0;
  9. int type = 0;
  10. int avgAge=0;
  11.  
  12. Scanner scan = new Scanner (System.in);
  13. //Input Ages
  14. System.out.println("Enter 1st Age: ");
  15. age=scan.nextInt();
  16. System.out.println("Enter type: ");
  17. type=scan.nextInt();
  18. ages.setAges(type, age);
  19.  
  20. System.out.println("Enter 2nd Age: ");
  21. age=scan.nextInt();
  22. System.out.println("Enter type: ");
  23. type=scan.nextInt();
  24. ages.setAges(type, age);
  25.  
  26. System.out.println("Enter 3rd Age: ");
  27. age=scan.nextInt();
  28. System.out.println("Enter type: ");
  29. type=scan.nextInt();
  30. ages.setAges(type, age);
  31.  
  32. //Print average scores
  33. avgAge = ages.getAvg();
  34.  
  35. System.out.println("Average Age: " + avgAge);
  36. }
  37.  
  38. }
Reputation Points: 7
Solved Threads: 6
Junior Poster
new_2_java is offline Offline
127 posts
since Apr 2007
Apr 11th, 2008
0

Re: Age class and associated methods

Thanks for your suggestion, but it resulted in several errors.

I'll keep trying.
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
clueless101 is offline Offline
52 posts
since Mar 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: BigDecimal problem
Next Thread in Java Forum Timeline: Calculating float types





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC