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...
Create java class with name MyClass1 Instructions for MyClass1 For this portion of the assignment you...
Create java class with name MyClass1 Instructions for MyClass1 For this portion of the assignment you will need to create a java class of your own to model some objects in the real world or your own imagination. your classes should have at least three instances variables, accessor and mutator methods for each of those instance variables, one constructor method of any type, as well as an overridden toString method that will display every field value as part of a...
in java, write code that defines a class named Cat The class should have breed, name...
in java, write code that defines a class named Cat The class should have breed, name and weight as attributes. include setters and getters for the methods for each attribute. include a toString method that prints out the object created from the class, and a welcome message. And use a constructor that takes in all the attributes to create an object.
In Java, design and implement a class called Cat. Each Cat class will contain three private...
In Java, design and implement a class called Cat. Each Cat class will contain three private variables - an integer representing its speed, a double representing its meowing loudness, and a String representing its name. Using the Random class, the constructor should set the speed to a random integer from 0 to 9, the meowing loudness to a random double, and the name to anything you want; the constructor should take no parameters. Write “get” and “set” methods for each...
Write a program that meets the following requirements: Cat Class Create a class called Cat which...
Write a program that meets the following requirements: Cat Class Create a class called Cat which has only the following instance variables: - name - breed - number of legs - year born Create the no-argument constructor Create the constructor which uses all fields as parameters Write the getter and setter methods for all instance variables Override the toString method using the example shown above There should be NO main method in the Cat class. CatTester Class Create a class...
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...
Create a Java class with the name (identifier) MyProgram and place it in the Questions package....
Create a Java class with the name (identifier) MyProgram and place it in the Questions package. You will need to create a main method for this class and place all of your code inside of that method. Your program should complete the following steps: - Prompt the user to enter a name for a Student. - Use the Scanner class to read in the user input and store the result in a variable. - Use the Random class to generate...
create a java class with name ModernArt: For the first part of the assignment, you will...
create a java class with name ModernArt: For the first part of the assignment, you will be extending the JApplet class and creating a work of modern using the Graphics class to draw various shapes to the applet window. As part of your masterpiece, you should incorporate the following elements: At least 1 non-solid rectangle At least 1 non-solid oval At least 1 solid oval At least 1 non-solid Polygon At least 1 solid Polygon At least 3 lines At...
Create a Java program. The class name for the program should be 'EncryptText'. In the main...
Create a Java program. The class name for the program should be 'EncryptText'. In the main method you should perform the following: You should read a string from the keyboard using the Scanner class object. You should then encrypt the text by reading each character from the string and adding 1 to the character resulting in a shift of the letter entered. You should output the string entered and the resulting encrypted string. Pseudo flowchart for additional code to be...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT