Age class and associated methods

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Mar 2008
Posts: 52
Reputation: clueless101 is an unknown quantity at this point 
Solved Threads: 0
clueless101 clueless101 is offline Offline
Junior Poster in Training

Age class and associated methods

 
0
  #1
Apr 11th, 2008
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.
  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. }
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 6,143
Reputation: jwenting is just really nice jwenting is just really nice jwenting is just really nice jwenting is just really nice 
Solved Threads: 212
Team Colleague
jwenting's Avatar
jwenting jwenting is offline Offline
duckman

Re: Age class and associated methods

 
0
  #2
Apr 11th, 2008
tell us what you think is wrong, and we may tell you whether you're right.
As people are clearly allowed to attack me but I'm not allowed to defend myself, I no longer post to this site.
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 52
Reputation: clueless101 is an unknown quantity at this point 
Solved Threads: 0
clueless101 clueless101 is offline Offline
Junior Poster in Training

Re: Age class and associated methods

 
0
  #3
Apr 11th, 2008
Actually, I considered the 1st part done.

Am I wrong?

Thanks!
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 52
Reputation: clueless101 is an unknown quantity at this point 
Solved Threads: 0
clueless101 clueless101 is offline Offline
Junior Poster in Training

Re: Age class and associated methods

 
0
  #4
Apr 11th, 2008
Here's what I've completed for demo.

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

Thanks in advance.


  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. }
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 4,515
Reputation: Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future 
Solved Threads: 523
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster

Re: Age class and associated methods

 
0
  #5
Apr 11th, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Apr 2007
Posts: 126
Reputation: new_2_java is an unknown quantity at this point 
Solved Threads: 6
new_2_java new_2_java is offline Offline
Junior Poster

Re: Age class and associated methods

 
-1
  #6
Apr 11th, 2008
You need this:
  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. }
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 52
Reputation: clueless101 is an unknown quantity at this point 
Solved Threads: 0
clueless101 clueless101 is offline Offline
Junior Poster in Training

Re: Age class and associated methods

 
0
  #7
Apr 11th, 2008
Thanks for your suggestion, but it resulted in several errors.

I'll keep trying.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Other Threads in the Java Forum


Views: 675 | Replies: 6
Thread Tools Search this Thread



Tag cloud for Java
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC