Test the Baby class below 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: a. Enter details for each baby (name and age) and thus populate the Baby array b. Output the details of each baby from the array (name and age) c. Calculate and display the average age of all babies in the array d. 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). For part d above, a nested loop is required. import java.util.Scanner; public class Baby { private String name; private int age; //default constructor public Baby(){ name="abc"; age=3; } //second constructor taking two //parameters, a string to set the name and an integer to set the age public Baby(String name,int age){ this.name=name; this.age=age; } // Setter Method public void setName(String newName) { if(newName != null && !newName.matches(" ")){ name = newName; } } // Getter Method public String getName() { return name; } public void setAge(int newAge){ if(newAge>=1 && newAge<=4){ age=newAge; } } public int getAge(){ return age; } public void isEquals(){ Baby baby1= new Baby("hello",2); Baby baby2= new Baby("hello",2); boolean bool = (baby1.name).equals(baby2.name); System.out.println(bool); } } class MyClass{ //start of main function public static void main(String[] args) { Baby myObj = new Baby(); Scanner scanner=new Scanner(System.in); System.out.println("Enter Name :"); String string1 =scanner.nextLine(); myObj.setName(string1); System.out.println("Enter Age :"); int age= scanner.nextInt(); myObj.setAge(age); Baby myObj1 = new Baby(myObj.getName(),myObj.getAge()); System.out.println("Name is : "+myObj1.getName()); System.out.println("Age is : "+myObj1.getAge()); myObj.isEquals(); }//end of the main function }

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

Test the Baby class below 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:
a. Enter details for each baby (name and age) and thus populate the
Baby array
b. Output the details of each baby from the array (name and age)
c. Calculate and display the average age of all babies in the array
d. 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).
For part d above, a nested loop is required.

import java.util.Scanner;

public class Baby {
private String name;
private int age;
//default constructor
public Baby(){
name="abc";
age=3;
}
//second constructor taking two
//parameters, a string to set the name and an integer to set the age
public Baby(String name,int age){
this.name=name;
this.age=age;
}

// Setter Method
public void setName(String newName) {
if(newName != null && !newName.matches(" ")){
name = newName;
}
}
// Getter Method
public String getName() {
return name;
}
public void setAge(int newAge){
if(newAge>=1 && newAge<=4){
age=newAge;
}
}
public int getAge(){
return age;
}

public void isEquals(){
Baby baby1= new Baby("hello",2);
Baby baby2= new Baby("hello",2);


boolean bool = (baby1.name).equals(baby2.name);
System.out.println(bool);
}
}

class MyClass{

//start of main function
public static void main(String[] args) {

Baby myObj = new Baby();
Scanner scanner=new Scanner(System.in);

System.out.println("Enter Name :");
String string1 =scanner.nextLine();
myObj.setName(string1);

System.out.println("Enter Age :");
int age= scanner.nextInt();
myObj.setAge(age);

Baby myObj1 = new Baby(myObj.getName(),myObj.getAge());
System.out.println("Name is : "+myObj1.getName());
System.out.println("Age is : "+myObj1.getAge());
myObj.isEquals();

}//end of the main function
}

Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 2 steps

Blurred answer
Knowledge Booster
Array
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education