Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

1. Write a new class, Cat, derived from PetRecord Add an additional attribute to store the number of lives remaining: numLives Write a constructor that

1. Write a new class, Cat, derived from PetRecord Add an additional attribute to store the number of lives remaining: numLives Write a constructor that initializes numLives to 9 and initializes name, age, & weight based on formal parameter values

2. Write a main method to test your Cat constructor & call the inherited writeOutput() method

3. Write a new writeOutput method in Cat that uses super to print the Cats name, age, weight & numLives Does this method overload or override PetRecord.writeOutput()? (include the answer to this question as a comment in your code)

4. Instantiate 2 Cat objects with identical arguments to the constructor and compare them using .equals In your code, add a comment as to what happens

5. Add the following equals method (next slide) to your Cat class. Complete the method to return true if the calling cat object and the parameter object have the same name and age, and false otherwise.

Cat equals method // TO DO: Does this equals method override or overload the Object equals method? Refer to the Java docs to see the signature of the Object equals method: https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html. Add a comment in your code with the answer. public boolean equals(Object otherObj) { if (otherObj == this) return true; if (!(otherObj instanceof Cat)) { return false; } Cat otherCat = (Cat) otherObj; // TO DO: Add comparison code that returns true if the cats // have the same name and age, false otherwise // (note that you can do this in one long line of code).

/*************************************************** * Class for basic pet records: name, age, and weight. ***************************************************/ public class PetRecord { // member variables are protected for direct access in inherited classes protected String name; protected int age;//in years protected double weight;//in pounds

public void writeOutput() { System.out.println("Name: " + name); System.out.println("Age: " + age + " years"); System.out.println("Weight: " + weight + " pounds"); }

public PetRecord(String initialName, int initialAge, double initialWeight) { name = initialName; if ((initialAge < 0) || (initialWeight < 0)) { System.out.println("Error: Negative age or weight."); System.exit(0); } else { age = initialAge; weight = initialWeight; } }

public void set(String newName, int newAge, double newWeight) { name = newName; if ((newAge < 0) || (newWeight < 0)) { System.out.println("Error: Negative age or weight."); System.exit(0); } else { age = newAge; weight = newWeight; } }

public PetRecord(String initialName) { name = initialName; age = 0; weight = 0; }

public void set(String newName) { name = newName; //age and weight are unchanged. }

public PetRecord(int initialAge) { name = "No name yet."; weight = 0; if (initialAge < 0) { System.out.println("Error: Negative age."); System.exit(0); } else age = initialAge; }

public void set(int newAge) { if (newAge < 0) { System.out.println("Error: Negative age."); System.exit(0); } else age = newAge; //name and weight are unchanged. }

public PetRecord(double initialWeight) { name = "No name yet"; age = 0; if (initialWeight < 0) { System.out.println("Error: Negative weight."); System.exit(0); } else weight = initialWeight; }

public void set(double newWeight) { if (newWeight < 0) { System.out.println("Error: Negative weight."); System.exit(0); } else weight = newWeight; //name and age are unchanged. }

public PetRecord() { name = "No name yet."; age = 0; weight = 0; }

public String getName() { return name; }

public int getAge() { return age; }

public double getWeight() { return weight; } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions

Question

What are the skills of management ?

Answered: 1 week ago