Question

In: Computer Science

Create a java class with name Cat. Instructions for Cat class: This class is modeled after...

Create a java class with name Cat.

Instructions for Cat class:

This class is modeled after a Cat. You should have instance variables as follows:

  • The Cat’s name
  • The number of mice caught by the Cat.
  • Whether or not the Cat is secretly plotting to kill you

Note that you will need to choose both good types and meaningful identifiers for each of these instance variables. You may also assume that the Cat is not automatically always secretly plotting to kill you. Sometimes the plot is out in the open.

Your class should contain two constructor methods. A default constructor and a constructor that allows you to instantiate an object of the Cat class and choose initial values for each of the three instance variables.

Your class should also contain accessor and mutator methods for all three instance variables (Cat’s really don’t care what you call them, they won’t listen regardless, and it is potentially possible that it will stop plotting to kill you, although this is both unlikely and only temporary).

You will also need to override the toString method to work properly for the class. It should return a String containing the Cat’s name, the number of mice caught by the Cat, and whether or not the Cat is secretly plotting to kill you.

Solutions

Expert Solution

Thanks for the question.

Here is the completed code for this problem. 

Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. 

If you are satisfied with the solution, please rate the answer.


Thanks


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

public class Cat {

    private String catName;
    private int miceCaught;
    private boolean isAssassin;

    //A default constructor and a constructor that allows you
    // to instantiate an object of the Cat class and choose
    // initial values for each of the three instance variables.
    // default constructor
    public Cat() {
        this.catName = "Tammy";
        this.miceCaught = 21;
        this.isAssassin = false;
    }

    // overloaded constructor
    public Cat(String catName, int miceCaught, boolean isAssassin) {
        this.catName = catName;
        this.miceCaught = miceCaught;
        this.isAssassin = isAssassin;
    }

    //mutator methods
    //Your class should also contain accessor and mutator methods for all three instance variables
    public String getCatName() {
        return catName;
    }

    public int getMiceCaught() {
        return miceCaught;
    }

    public boolean isAssassin() {
        return isAssassin;
    }

    // acccessor methods
    //Your class should also contain accessor and mutator methods for all three instance variables 
    public void setCatName(String catName) {
        this.catName = catName;
    }

    public void setMiceCaught(int miceCaught) {
        this.miceCaught = miceCaught;
    }

    public void setAssassin(boolean assassin) {
        isAssassin = assassin;
    }

    //You will also need to override the toString method to work
    // properly for the class. It should return a String containing
    // the Cat’s name, the number of mice caught by the Cat, and whether
    // or not the Cat is secretly plotting to kill you.
    @Override
    public String toString() {
        if (isAssassin)
            return catName + ", caught " + miceCaught + " mice(s) and secretly plotting to kill you.";
        else
            return catName + ", caught " + miceCaught + " mice(s) and not secretly plotting to kill you.";

    }
}


Related Solutions

Create java class with name BaseballPlayer Instructions for BaseballPlayer class: The BaseballPlayer class is modeled after...
Create java class with name BaseballPlayer Instructions for BaseballPlayer class: The BaseballPlayer class is modeled after a BaseballPlayer and will contain methods to calculate various statistics based on the stats of a player. For this class, you will want to use a static variable for storing a DecimalFormat object. See the API document for the BaseballPlayer class for a list of methods you will write. API Document Constructors: Identifier: BaseballPlayer(String name, int number, int singles, int doubles, int triples, int...
Create java Class with name Conversion. Instructions for Conversion class: The Conversion class will contain methods...
Create java Class with name Conversion. Instructions for Conversion class: The Conversion class will contain methods designed to perform simple conversions. Specifically, you will be writing methods to convert temperature between Fahrenheit and Celsius and length between meters and inches and practicing overloading methods. See the API document for the Conversion class for a list of the methods you will write. Also, because all of the methods of the Conversion class will be static, you should ensure that it is...
THIS IS JAVA PROGRAMMING 1. Create a class named Name that contains the following: • A...
THIS IS JAVA PROGRAMMING 1. Create a class named Name that contains the following: • A private String to represent the first name. • A private String to represent the last name. • A public constructor that accepts two values and assigns them to the above properties. • Public methods named getProperty (e.g. getFirstName) to return the value of the property. • Public methods named setProperty ( e.g. setFirstName)to assign values to each property by using a single argument passed...
Program in Java Create a class and name it MyArray and implement following method. * NOTE:...
Program in Java Create a class and name it MyArray and implement following method. * NOTE: if you need more methods, including insert(), display(), etc. you can also implement those. Method name: getKthMin(int k) This method receives an integer k and returns k-th minimum value stored in the array. * NOTE: Items in the array are not sorted. If you need to sort them, you can implement any desired sorting algorithm (Do not use Java's default sorting methods). Example: Items...
Java: Create a class and name it MyArray and implement following method. * NOTE: if you...
Java: Create a class and name it MyArray and implement following method. * NOTE: if you need more methods, including insert(), display(), etc. you can also implement those Method name: getKthMin(int k) This method receives an integer k and returns k-th minimum value stored in the array. * NOTE: Items in the array are not sorted. If you need to sort them, you can implement any desired sorting algorithm (Do not use Java's default sorting methods). Example: Items in the...
Program in Java Create a class and name it MyArray and implement following method. * NOTE:...
Program in Java Create a class and name it MyArray and implement following method. * NOTE: if you need more methods, including insert(), display(), etc. you can also implement those. Method name: getKthMin(int k) This method receives an integer k and returns k-th minimum value stored in the array. * NOTE: Items in the array are not sorted. If you need to sort them, you can implement any desired sorting algorithm (Do not use Java's default sorting methods). Example: Items...
In java, create a class named Contacts that has fields for a person’s name, phone number...
In java, create a class named Contacts that has fields for a person’s name, phone number and email address. The class should have a no-arg constructor and a constructor that takes in all fields, appropriate setter and getter methods. Then write a program that creates at least five Contact objects and stores them in an ArrayList. In the program create a method, that will display each object in the ArrayList. Call the method to demonstrate that it works. Include javadoc...
IN JAVA PLEASE Create a class called Child with an instance data values: name and age....
IN JAVA PLEASE Create a class called Child with an instance data values: name and age. a. Define a constructor to accept and initialize instance data b. include setter and getter methods for instance data c. include a toString method that returns a one line description of the child
Create a Netbeans project with a Java main class. (It does not matter what you name...
Create a Netbeans project with a Java main class. (It does not matter what you name your project or class.) Your Java main class will have a main method and a method named "subtractTwoNumbers()". Your subtractTwoNumbers() method should require three integers to be sent from the main method. The second and third numbers should be subtracted from the first number. The answer should be returned to the main method and then displayed on the screen. Your main() method will prove...
in Java, Create a class called EMPLOYEE that includes three instance variables – a first name...
in Java, Create a class called EMPLOYEE that includes three instance variables – a first name (type String), a last name (type String) and a monthly salary (double). Provide a constructor that initializes the three instance variables. Provide a set and a get method for each instance variable. If the monthly salary is not positive, do not set its value. Write a test app names EmployeeTest that demonstrates class EMLOYEE’s capabilities. Create two EMPLOYEE objects and display each object’s yearly...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT