Question

In: Computer Science

What to submit: your answers to exercises 1, 2, and 3 and separate the codes to...

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.

Solutions

Expert Solution

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!!!


Related Solutions

Your Answers: Type your answers in the table and submit this worksheet. Use what you have...
Your Answers: Type your answers in the table and submit this worksheet. Use what you have learned about the time value of money to analyze each of the following decisions: Decision #1: Which set of Cash Flows is worth more now? Assume that your grandmother wants to give you generous gift. She wants you to choose which one of the following sets of cash flows you would like to receive: Option A: Receive a one-time gift of $10,000 today. Option...
Answer the following questions: 1. - 3 separate answers. For the three options presented by the...
Answer the following questions: 1. - 3 separate answers. For the three options presented by the brothers, what are the pros and cons of each? 2. Which would you choose from #1 above and why? -------------------- Bringing an Innovative Razor to the Masses By MICKEY MEECEAPRIL 28, 2010 L.P.I. Consumer Productsmakes and distributes patented ShaveMateall-in-one razors that feature shaving cream dispensed from the handle. The company, which has been in business since 1987, has been developing its line of razors...
a) Submit a copy of your dataset along with a file that contains your answers to...
a) Submit a copy of your dataset along with a file that contains your answers to all of the following questions. b) What the mean and Standard Deviation (SD) of the Close column in your data set? c) If a person bought 1 share of Google stock within the last year, what is the probability that the stock on that day closed at less than the mean for that year? Hint: You do not want to calculate the mean to...
In regard to herbal medicine marketing, what is the implication of 1. legislation, 2.regulations 3. codes...
In regard to herbal medicine marketing, what is the implication of 1. legislation, 2.regulations 3. codes of ethics . what are the legislation, regulations and codes of ethics to consider when doing the marketing of herbal medicine(400 words)
Create an Excel spreadsheet to organize your answers to the following problem, and submit your Excel...
Create an Excel spreadsheet to organize your answers to the following problem, and submit your Excel file as an attachment by clicking on the appropriate button on this page. A firm that is in the 35% tax bracket forecasts that it can retain $4 million of new earnings plans to raise new capital in the following proportions: 60% from 30-year bonds with a flotation cost of 4% of face value. Their current bonds are selling at a price of 91...
create an Excel spreadsheet to organize your answers to the following problem, and submit your Excel...
create an Excel spreadsheet to organize your answers to the following problem, and submit your Excel file as an attachment by clicking on the appropriate button on this page. A company is evaluating the purchase of a machine to improve product quality and output levels. The new machine would cost $1.6 million and would be depreciated for tax purposes using the straight-line method over an estimated six-year life to its expected salvage value of $100,000. The new machine would require...
Create an Excel spreadsheet to organize your answers to the following problem, and submit your Excel...
Create an Excel spreadsheet to organize your answers to the following problem, and submit your Excel file as an attachment by clicking on the appropriate button on this page. A company with EBIT of $6,000,000 is considering two financing alternatives. The first alternative would have interest expense of $2,000,000 and 1,000,000 common shares outstanding, whereas the second would have interest expense of $3,800,000 but only 750,000 shares outstanding. The company is in the 35% tax bracket. Part 1: Construct the...
Create an Excel spreadsheet to organize your answers to the following problem, and submit your Excel...
Create an Excel spreadsheet to organize your answers to the following problem, and submit your Excel file as an attachment by clicking on the appropriate button on this page. A firm had the following abbreviated income statement for 2020 and abbreviated balance sheets at the end of 2020 and 2021. 2020 Sales $600,000 Less Cost of goods sold 320,000 Gross Profit 280,000 Less Operating expenses 190,000 Less Depreciation 30,000 Operating Income (EBIT) 60,000 Less Interest expense 20,000 Earnings Before Taxes...
Create an Excel spreadsheet to organize your answers to the following problem, and submit your Excel...
Create an Excel spreadsheet to organize your answers to the following problem, and submit your Excel file as an attachment by clicking on the appropriate button on this page. A company with EBIT of $6,000,000 is considering two financing alternatives. The first alternative would have $25 million of bonds at 8% interest and 1,000,000 common shares outstanding, whereas the second would have $47.5 million of bonds at 8% interest and only 750,000 shares outstanding. The company is in the 35%...
Instructions: Answer the following questions. Submit your answers to questions 1-5 as a Rich Text Format...
Instructions: Answer the following questions. Submit your answers to questions 1-5 as a Rich Text Format file (.rtf), Word document (.doc), or ASCII text file (.txt). For problem 6 submit an excel sheet containing your chart. 4. (40 points) Determine the number of statement executions (precise big-Oh) for each of the following sample code, as described in the lecture. Your answers should be in the form of a Big-Oh polynomial (e.g., O(3N2 + 7N + 6)). Sample #1: for (int...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT