954,506 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Age class and associated methods

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.

/**
	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;
	}
}
clueless101
Junior Poster in Training
52 posts since Mar 2008
Reputation Points: 10
Solved Threads: 0
 

tell us what you think is wrong, and we may tell you whether you're right.

jwenting
duckman
Team Colleague
8,392 posts since Nov 2004
Reputation Points: 1,662
Solved Threads: 337
 

Actually, I considered the 1st part done.

Am I wrong?

Thanks!

clueless101
Junior Poster in Training
52 posts since Mar 2008
Reputation Points: 10
Solved Threads: 0
 

Here's what I've completed for demo.

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

Thanks in advance.

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);
		}
}
clueless101
Junior Poster in Training
52 posts since Mar 2008
Reputation Points: 10
Solved Threads: 0
 

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.

Ezzaral
Posting Genius
Moderator
15,986 posts since May 2007
Reputation Points: 3,250
Solved Threads: 847
 

You need this:

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);
	}

}
new_2_java
Junior Poster
127 posts since Apr 2007
Reputation Points: 7
Solved Threads: 6
 

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

I'll keep trying.

clueless101
Junior Poster in Training
52 posts since Mar 2008
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You