| | |
Age class and associated methods
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
•
•
Join Date: Mar 2008
Posts: 52
Reputation:
Solved Threads: 0
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.
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)
/** Ages Program */ public class Ages { //Instance variables private int age1; //holds 1st age private int age2; //holds 2nd age private int age3; //holds 3rd age //Constructor Method public Ages() { //Initialize ages to 0 age1=0; age2=0; age3=0; } public void setAges (int i, int age) { //Set i to age if (i==1) age1=age; else if (i==2) age2=age; else age3=age; } public int getAges(int i) { //Accessor Method to return ages if (i==1) return age1; else if (i==2) return age2; else return age3; } public int getAvg() { //Calcuate and return average of 3 ages int ageAvg; ageAvg=(int) Math.round((age1+age2+age3)/3.0); return ageAvg; } }
•
•
Join Date: Mar 2008
Posts: 52
Reputation:
Solved Threads: 0
Here's what I've completed for demo.
Can anyone tell me why my average displays as "0"????
Thanks in advance.
Can anyone tell me why my average displays as "0"????
Thanks in advance.
Java Syntax (Toggle Plain Text)
import java.util.Scanner; public class DemoAges { public static void main(String[] args) { //Declare/initialize Ages int age1, age2, age3; int avgAge=0; Scanner scan = new Scanner (System.in); //Input Ages System.out.println("Enter 1st Age: "); age1=scan.nextInt(); System.out.println("Enter 2nd Age: "); age2=scan.nextInt(); System.out.println("Enter 3rd Age: "); age3=scan.nextInt(); //Print average scores System.out.println("Average Age: " + avgAge); } }
•
•
Join Date: Apr 2007
Posts: 126
Reputation:
Solved Threads: 6
You need this:
Java Syntax (Toggle Plain Text)
public class DemoAges { private Ages ages = new Ages(); public static void main(String[] args) { //Declare/initialize Ages int age = 0; int type = 0; int avgAge=0; Scanner scan = new Scanner (System.in); //Input Ages System.out.println("Enter 1st Age: "); age=scan.nextInt(); System.out.println("Enter type: "); type=scan.nextInt(); ages.setAges(type, age); System.out.println("Enter 2nd Age: "); age=scan.nextInt(); System.out.println("Enter type: "); type=scan.nextInt(); ages.setAges(type, age); System.out.println("Enter 3rd Age: "); age=scan.nextInt(); System.out.println("Enter type: "); type=scan.nextInt(); ages.setAges(type, age); //Print average scores avgAge = ages.getAvg(); System.out.println("Average Age: " + avgAge); } }
![]() |
Other Threads in the Java Forum
- Previous Thread: BigDecimal problem
- Next Thread: Calculating float types
Views: 675 | Replies: 6
| Thread Tools | Search this Thread |
Tag cloud for Java
2dgraphics android api apple applet application arguments array arrays automation banking binary binarytree bluetooth chat chatprogramusingobjects class classes client code component database derby design draw eclipse encryption error event exception file fractal game givemetehcodez graphics gui helpwithhomework html ide if_statement image input integer interface j2me java javadesktopapplications javaprojects jlabel jmf jni jpanel julia linux list loop map method methods mobile multithreading netbeans newbie nullpointerexception number object open-source oracle print printing problem program programming project property recursion reference remove scanner screen server set size sms socket sort splash sql stop string swing test threads time transfer tree ui unicode validation windows






