Question

1. Write a new class, Cat, derived from PetRecord – Add an additional attribute to store...

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 Cat’s 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;
}
}

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

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 setName(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 setAge(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 setWeight(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;

}

}

class Cat extends PetRecord

{

private int numLives;

public Cat(String initialName, int initialAge,double initialWeight)

{

super(initialName,initialAge,initialWeight);

numLives = 9;

}

public void writeOutput() // override base class writeOutput() method

{

super.writeOutput(); // call to base class writeOutput() method

System.out.println("Number of Lives : "+numLives);

}

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

if(this.name == otherCat.name && this.age == otherCat.age )

return true;

else

return false;

}

}

public class Test

{

public static void main (String[] args)

{

Cat c = new Cat("Pussy",2,4.8);

c.writeOutput();

Cat c1 = new Cat("Pussy",2,5.9);

if(c.equals(c1))

System.out.println("Both cats are identical");

else

System.out.println("Cats are different");

}

}

Output:

Name: Pussy
Age: 2 years
Weight: 4.8 pounds
Number of Lives : 9
Both cats are identical

Do ask if any doubt. Please upvote.

Add a comment
Know the answer?
Add Answer to:
1. Write a new class, Cat, derived from PetRecord – Add an additional attribute to store...
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
  • (JAVA) Use the Pet.java program from the original problem (down below) Compile it. Create a text...

    (JAVA) Use the Pet.java program from the original problem (down below) Compile it. Create a text file named pets10.txt with 10 pets (or use the one below). Store this file in the same folder as your “class” file(s). The format is the same format used in the original homework problem. Each line in the file should contain a pet name (String), a comma, a pet’s age (int) in years, another comma, and a pet’s weight (double) in pounds. Perform the...

  • In Java, Write a class encapsulating a restaurant,which inherits from Store. A restaurant has the following...

    In Java, Write a class encapsulating a restaurant,which inherits from Store. A restaurant has the following additional attributes: how many people are served every year and the average price per person. code the constructor, accessors, mutators, toString and equals method of the new subclass; also code a method returning the average taxes per year. You also need to include a client class to test your code for both the parent class and the subclass. Code for Store below(Super class aka...

  • public class Item implements Comparable { private String name; private double price; /** * Constructor for...

    public class Item implements Comparable { private String name; private double price; /** * Constructor for objects of class Item * @param theName name of item * @param thePrice price of item */ public Item(String theName, double thePrice) { name = theName; price = thePrice; }    /** * gets the name * @return name name of item */ public String getName() { return name; }    /** * gets price of item * @return price price of item */...

  • Using your Dog class from earlier this week, complete the following: Create a new class called...

    Using your Dog class from earlier this week, complete the following: Create a new class called DogKennel with the following: Instance field(s) - array of Dogs + any others you may want Contructor - default (no parameters) - will make a DogKennel with no dogs in it Methods: public void addDog(Dog d) - which adds a dog to the array public int currentNumDogs() - returns number of dogs currently in kennel public double averageAge() - which returns the average age...

  • How to solve this problem? Consider the following class : public class Store Public final double...

    How to solve this problem? Consider the following class : public class Store Public final double SALES TAX_RATE = 0.063B private String name; public Store(String newName) setName ( newName); public String getName () { return name; public void setName (String newName) { name = newName; public String toString() return "name: “ + name; Write a class encapsulating a web store, which inherits from Store. A web store has the following additional attributes: an Internet Address and the programming language in...

  • Assignment (to be done in Java): Person Class: public class Person extends Passenger{ private int numOffspring;...

    Assignment (to be done in Java): Person Class: public class Person extends Passenger{ private int numOffspring; public Person() {    this.numOffspring = 0; } public Person (int numOffspring) {    this.numOffspring = numOffspring; } public Person(String name, int birthYear, double weight, double height, char gender, int numCarryOn, int numOffspring) {    super(name, birthYear, weight, height, gender, numCarryOn);       if(numOffspring < 0) {        this.numOffspring = 0;    }    this.numOffspring = numOffspring; } public int getNumOffspring() {   ...

  • Write in java and the class need to use give in the end copy it is...

    Write in java and the class need to use give in the end copy it is fine. Problem Use the Plant, Tree, Flower and Vegetable classes you have already created. Add an equals method to Plant, Tree and Flower only (see #2 below and the code at the end). The equals method in Plant will check the name. In Tree it will test name (use the parent equals), and the height. In Flower it will check name and color. In...

  • In Java Code Needed is Below the Question Using Program 3: Encapsulating Dogs, modify this program...

    In Java Code Needed is Below the Question Using Program 3: Encapsulating Dogs, modify this program to create a database of dogs of your own. You will also include File I/O with this program. You will create 3 classes for this assignment. The first class: Create a Class representing the information that is associated with one item (one dog) of your database. Name this class Dog. Using Lab 3, this will include the information in regard to one dog. The...

  • import java.util.Scanner; public class StudentClient {       public static void main(String[] args)    {   ...

    import java.util.Scanner; public class StudentClient {       public static void main(String[] args)    {        Student s1 = new Student();         Student s2 = new Student("Smith", "123-45-6789", 3.2);         Student s3 = new Student("Jones", "987-65-4321", 3.7);         System.out.println("The name of student #1 is ");         System.out.println("The social security number of student #1 is " + s1.toString());         System.out.println("Student #2 is " + s2);         System.out.println("the name of student #3 is " + s3.getName());         System.out.println("The social security number...

  • Step 1 Develop the following class: Class Name: Bag Access Modifier: public Instance variables Name: name...

    Step 1 Develop the following class: Class Name: Bag Access Modifier: public Instance variables Name: name Access modifier: private Data Type: String Name: currentWeight Access modifier: private Data Type: double Name: maximumWeight Access modifier: private Data Type: double Constructors Name: Bag Access modifier: public Parameters: none (default constructor) Task: sets the value of the instance variable name to the empty string sets the value of the instance variable currentWeight to 0.0 sets the value of the instance variable maximumWeight to...

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