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

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

Actually, I considered the 1st part done.

Am I wrong?

Thanks!

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

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.

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

}
commented: Writing the solution for homework questions is generally frowned upon. -1

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

I'll keep trying.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.