Question

Create a UML diagram to help design the class described in exercise 3 below. Do this...

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?

3. 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.

The class should not contain I/O methods; input of values to the instance

variables must be done with a set method or constructor, output of values from

the instance variables must be done with get methods. The set method for the

name instance variable should ensure that the input is not empty or contain

whitespaces (otherwise set a default value). The set method for the age

instance variable should validate the input to be between 1 and 4 inclusive

(otherwise set a default value).

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.

4. 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.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

// UML Diagrm of Baby class:

Baby name : String age : int + <<constructor>> Baby () + <<constructor>> Baby (name : String , age : int) + getName(): String

===========================================

// Baby.java

public class Baby {
   private String name;
   private int age;

   public Baby() {
       this.name = "";
       this.age = 0;
   }

   /**
   * @param name
   * @param age
   */
   public Baby(String name, int age) {
       this.name = name;
       this.age = age;
   }

   /**
   * @return the name
   */
   public String getName() {
       return name;
   }

   /**
   * @param name
   * the name to set
   */
   public void setName(String name) {
       int flag = 0;
       for (int i = 0; i < name.length(); i++) {
           if (name.charAt(i) == ' ') {
               flag = 1;
               break;
           }
       }
       if (!name.isEmpty() && flag != 1)
           this.name = name;
       else
           this.name = "";
   }

   /**
   * @return the age
   */
   public int getAge() {
       return age;
   }

   /**
   * @param age
   * the age to set
   */
   public void setAge(int age) {
       if (age >= 1 && age <= 4)
           this.age = age;
       else
           this.age = 0;
   }

   public boolean equals(Baby other) {

       if (age != other.age)
           return false;
       if (name == null) {
           if (other.name != null)
               return false;
       } else if (!name.equals(other.name))
           return false;
       return true;
   }

}

==========================================

3 public class Baby { 4 private String name; 5 private int age; 6 7e public Baby () { 8 this.name = ; 9 this. .age = 0; 10

/** * @param name the name to set */ public void setName (String name) { int flag = 0; for (int i = 0; i < name.length(); i++

===========================================

// Test.java

import java.util.Scanner;

public class Test {

   public static void main(String[] args) {
       String name;
       int age;
       /*
       * Creating an Scanner class object which is used to get the inputs
       * entered by the user
       */
       Scanner sc = new Scanner(System.in);

       // Creating Baby class array of size 4
       Baby babies[] = new Baby[4];

       for (int i = 0; i < babies.length; i++) {
           // Getting the input entered by the user
           System.out.print("Enter name of Baby#" + (i + 1) + ":");
           name = sc.next();
           System.out.print("Enter age:");
           age = sc.nextInt();
           // Creating an instance of Baby class
           Baby b = new Baby(name, age);
           // Assign the Baby class instance to the array
           babies[i] = b;
       }

       // Calling the method
       display(babies);
       double avgAge = calcAvgAgeOfAllBabies(babies);
       // Displaying the average age of all babies
       System.out.println("Average age of all babies :" + avgAge);
       boolean b = areAnyBabiesAreSame(babies);
       if (b) {
           System.out.println("All babies are not unique");
       } else {
           System.out.println("All babies are unique");
       }
   }

   // This method will check whether any babies are same or not
   private static boolean areAnyBabiesAreSame(Baby[] babies) {
       for (int i = 0; i < babies.length; i++) {
           for (int j = i + 1; j < babies.length; j++) {
               if (babies[i].equals(babies[j])) {
                   return true;
               }
           }
       }
       return false;
   }

   // This method will calculate the average age of all babies
   private static double calcAvgAgeOfAllBabies(Baby[] babies) {
       double sum = 0;
       for (int i = 0; i < babies.length; i++) {
           sum += babies[i].getAge();
       }
       return sum / babies.length;
   }

   // This method will display each baby info
   private static void display(Baby[] babies) {
       for (int i = 0; i < babies.length; i++) {
           System.out.println("\nBaby#" + (i + 1) + ":");
           System.out.println("Name :" + babies[i].getName() + ", Age :"+ babies[i].getAge());
       }

   }

}

==========================================

import java.util.Scanner; public class Test { public static void main(String[] args) { String name; int age; * Creating an Sc// Calling the method display (babies); double avgAge = calcAvgAgeOfAllBabies (babies); // Displaying the average age of all

// This method will display each baby info private static void display (Baby[] babies) { for (int i = 0; i < babies.length; i

==============================================

Output:

<terminated > Test (438) [Java Application) C:\Program Files\ava\jre 1.8.0_181\bin\javaw.exe (Sep 11, 2020 10:00:47 AM) Enter

=====================Could you plz rate me well.Thank You

Add a comment
Know the answer?
Add Answer to:
Create a UML diagram to help design the class described in exercise 3 below. Do this...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Create a UML diagram to help design the class described in exercise 3 below. Do this...

    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? Write Java code for a Baby class. A Baby has a name of type String and an age of type integer. Supply two constructors:...

  • Java Object Array With 2 Elements In 1 Object

    1. Create a UML diagram to help design the class described in Q2 below. Do this exercise before you attempt to code the solution. Think about what instance variables will be required to describe a Person 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 Person class. A Person has a name of type String and an age of type integer.Supply two constructors: one will be...

  • Hello. I need help writing the following Java Program. Thank you Develop a class encapsulating the...

    Hello. I need help writing the following Java Program. Thank you Develop a class encapsulating the concept of a college course, assuming that a course has following attributers: code (for instance COSC1337), a description, and a number of credits (for instance 3). Include a constructor, the accessors, mutators and methods ‘toString’, ‘equals’, and ‘finalize’. Write a client class to test the behavior of the class and its methods. The outline of the class is given as follows: public class Course...

  • Write a Java class named Employee to meet the requirements described in the UML Class Diagram...

    Write a Java class named Employee to meet the requirements described in the UML Class Diagram below: Note: Code added in the toString cell are not usually included in a regular UML class diagram. This was added so you can understand what I expect from you. If your code compiles cleanly, is defined correctly and functions exactly as required, the expected output of your program will solve the following problem: “An IT company with four departments and a staff strength...

  • C# programming 50 pts Question 2:2 1- Create the base class Book that has the following instance variables, constructor, and methods title (String) isbn (String) authors (String) publisher (Strin...

    C# programming 50 pts Question 2:2 1- Create the base class Book that has the following instance variables, constructor, and methods title (String) isbn (String) authors (String) publisher (String) edition ( int) published year (int) Constructor that takes all of the above variables as input parameters. set/get methods ToString method// that return sting representation of Book object. 2-Create the sub class New_Book that is derived from the base class Book and has the following instance variables, constructor, and methods: title...

  • Code in JAVA UML //TEST HARNESS //DO NOT CHANGE CODE FOR TEST HARNESS import java.util.Scanner; //...

    Code in JAVA UML //TEST HARNESS //DO NOT CHANGE CODE FOR TEST HARNESS import java.util.Scanner; // Scanner class to support user input public class TestPetHierarchy { /* * All the 'work' of the process takes place in the main method. This is actually poor design/structure, * but we will use this (very simple) form to begin the semester... */ public static void main( String[] args ) { /* * Variables required for processing */ Scanner input = new Scanner( System.in...

  • Exercise #3: Create the “MathTest” class. It will have two class variables: 1) a question and...

    Exercise #3: Create the “MathTest” class. It will have two class variables: 1) a question and 2) the answer to that question. Exercise #4: Please create an accessor and mutator method for both of those variables, without which we would not be able to see or change the question or the answer. Exercise #5: There should be a constructor method. We will also have a “ToString” method, which will print the question followed by its answer. The constructor method has...

  • Please help me with this code. Thank you Implement the following Java class: Vehicle Class should...

    Please help me with this code. Thank you Implement the following Java class: Vehicle Class should contain next instance variables: Integer numberOfWheels; Double engineCapacity; Boolean isElectric, String manufacturer; Array of integers productionYears; Supply your class with: Default constructor (which sets all variables to their respective default values) Constructor which accepts all the variables All the appropriate getters and setters (you may skip comments for this methods. Also, make sure that for engineCapacity setter method you check first if the vehicle...

  • Create an abstract class “Appointment” that represents an event on a calendar. The “Appointment” class will...

    Create an abstract class “Appointment” that represents an event on a calendar. The “Appointment” class will have four instance variables: • An instance variable called “year” which will be of type int • An instance variable called “month” which will be of type int • An instance variable called “day” which will be of type int • An instance variable called “description” which will be of type String The “Appointment” class must also implement the following methods: • A getter...

  • Java file Name Dog Classes and Methods Create a constructor that incorporates the type, breed, and...

    Java file Name Dog Classes and Methods Create a constructor that incorporates the type, breed, and name variables (do not include topTrick). Note: The type refers to what the breed typically does; for example, a corgi would be a “cattle herding dog.” A Shiba Inu would be a “hunting dog.” Create the setTopTrick() mutator method Dog is parent class Corgi and Driver are subclasses Complete the Corgi class: Using the UML Class diagram, declare the instance variables. Create the two...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT