In: Computer Science
What to submit: your answers to exercises 1, 2, and 3 and separate the codes to each question..
1. Create a UML diagram to help design the class described in exercise 3 below.
Do this exercise before you attempt to code the solution. Think about what instance variables will be required to describe a Baby class object; should they be private or public? Determine what class methods are required; should they be private or public?
2. Write Java code for a Baby class. A Baby has a name of type String and an age of type integer.
Supply two constructors: one will be the default constructor, that just sets default values for the name and age; the second constructor will take two parameters, a string to set the name and an integer to set the age. Also, supply methods for setting the name, setting the age, getting the name and getting the age.
Give Java code for an equals method for the Baby class. Babies count as being the same (i.e. equal) if their names and their ages are exactly identical (names should not be case sensitive). The method will take a Baby type parameter and use the calling object (thus comparing these two objects via name and age); it should return Boolean - true or false as appropriate. Remember, if comparing Strings, you must use String comparison methods.
. 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:
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.
UML diagram
JAVA code
Baby.java
class Baby {
// Instance variables required
private String name;
private int age;
// Default constructor
public Baby() {
name = "";
age = 0;
}
// Parameterized constructor
public Baby(String n, int a) {
name = n;
age = a;
}
// Setters and Getters
public void setName(String n) {
name = n;
}
public void setAge(int a) {
age = a;
}
public int getAge() {
return age;
}
public String getName() {
return name;
}
// equals method to check both name and age of a baby
public boolean equals(Baby b) {
return (this.name.equalsIgnoreCase(b.name) && this.age
== b.age);
}
}
Client.java
import java.util.Scanner;
public class Client {
public static void main(String[] args) {
// Creating an array of 4 babies
Baby[] babies = new Baby[4];
// Object of Scanner class to take input
Scanner input = new Scanner(System.in);
// (a) Populating babies array
for (int i=0; i<babies.length; i++) {
System.out.println("--- Baby#" + (i+1) + "---");
System.out.print("Enter name of baby: ");
String name = input.next();
System.out.print("Enter age of baby: ");
int age = input.nextInt();
babies[i] = new Baby(name, age);
}
// (b) Output details of each baby
System.out.println("\nDetails of All Babies");
for (int i=0; i<babies.length; i++) {
System.out.println("Baby #" + (i+1) + " = Name: " +
babies[i].getName() + ", " +
"Age: " + babies[i].getAge());
}
// (c) Calculating and displaying average age of all babies
int totalAge = 0;
for (int i=0; i<babies.length; i++) {
totalAge += babies[i].getAge();
}
double averageAge = totalAge / 4.0;
System.out.println("\nThe average age of all babies in the array is " + averageAge);
// (d) Determining whether any two babies are same in
array
// A boolean variable to determine if there is any same
boolean isSame = false;
for (int i=0; i<babies.length; i++) {
for (int j=i+1; j<babies.length; j++) {
if(babies[i].equals(babies[j])){
isSame = true;
break;
}
}
}
if(isSame)
System.out.println("Yes, there are two same babies in the
array");
else
System.out.println("No two babies in the array are same");
}
}
SAMPLE OUTPUT
-------------------------------------------------------------------------------------------------------------
COMMENT DOWN FOR ANY QUESTIONS...........
HIT A THUMBS UP IF YOU DO LIKE IT!!!