Test your Baby class by writing a client program which uses an array to store information about 4 babies.

QUESTION

Test your Baby class by writing a client program which uses an array to store

information about 4 babies. That is, each of the four elements of the array

must store a Baby object

If you have an array for baby names and another array for baby ages,

then you have missed the point of the exercise and therefore not met

the requirement of this exercise.

A Baby class object stores the required information about a Baby. So

each Baby object will have its own relevant information, and thus each

object must be stored in one element of the array. The client program should:

  1. Enter details for each baby (name and age) and thus populate the Baby array
  2. Output the details of each baby from the array (name and age)
  3. Calculate and display the average age of all babies in the array
  4. Determine whether any two babies in the array are the same

As the required information for these tasks is stored in the Baby array, you will need to use a loop to access each array element (and use the dot notation to access the appropriate set and get methods to assign/retrieve the information).

 

SUMMARY

A class named Baby is implemented having name and age as its members with getter and setter functions. The equals function is used to check if two babies are equal or not. The class is checked whether it works well or not in the main method.

 

EXPLANATION

An array of size 4 of type baby is declared. In each index of the Baby array, details of the baby such as the name of the baby and its age are passed as a constructor. The array is traversed and all the Baby object details is printed. The average age of the baby is also calculated and printed as the output. The method equals() defined in the Baby class checks if any two babies are same.

 

CODE

class Baby
{
private String name;
private int age;
Baby()
{
name = "xyz";
age = 3;
}
//parameterized constructor
Baby(String n,int ag){
name = n;
age = ag;
}
public void setname(String n){
if(n.isEmpty())
name = "xyz";
else
name = n;
}
public void setage(int ag){
if(!(ag>=1 && ag<=4))
age = 2;
else
age = ag;
}
public String getname(){
return name;
}
public int getage(){
return age;
}
public boolean equals(Baby ba){
if (age==ba.getage() && name.equalsIgnoreCase(ba.getname()))
return true;
else
return false;
}
}
class Main
{
public static void main(String[] args)
{
    /* array of size 4 */
    Baby[] babyarr;
    babayrr=new Baby[4];
    babyarr[0]=new Baby("Elena",8);
    babyarr[1]=new Baby("Monika",2);
    babyarr[2]=new Baby("Sam",3);
    babyarr[3]=new Baby("Rey",8);
    System.out.println("Baby info:");
    for(int i=0;i<4;i++)
    {
        System.out.print("Name: "+babyarr[i].getname()+"    Age: "+babyarr[i].getage()+"\n");
    }
    int sum=0;
    for(int j=0;j<4;j++)
    {
        sum+=babyarr[j].getage();
    }
    float avgAge=sum/4.0f;
    System.out.println("\nAverage age of babies = "+avgAge+"\n");
    /* checking if any two babies are same */
    for(int i=0;i<4;i++)
    {
        for(int j=i+1;j<4;j++)
        {
            if(babyarr[i].equals(babyarr[j]))
            {
                System.out.println("Babies in the index "+i+" and "+j+" are same");
            }
        }
    }
}
}

 

 

OUTPUT

 

Also Read: Answer the following mcqs.

 

Share this post

Leave a Reply

Your email address will not be published. Required fields are marked *