Question

In: Computer Science

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 variable, and one that prints all the variables. Finally, write a main method to test the methods in the class. The three variables must be private.

Then design and implement a class called CatOwner. Each CatOwner will have three private variables: a String representing the owner name, an integer representing how much cat food is left, and a cat; each cat should be an instance of the Cat class. The constructor can name the CatOwner anything you want, and the supply of food should be a random number greater than or equal to 10 and less than 50. Each cat’s name should be set to “Tinkles.” The constructor for CatOwner should take no parameters. Write “get” and “set” methods for the owner’s name, the food supply, and all the properties of the cat; also write a feedCat method that decreases the food supply by 1 and prints that the cat was fed. Next, write a method to print all class variables. Finally, write a main method to test your methods.

Design a driver class which instantiates two CatOwner objects. Next, perform the following five steps:

1. Change the name of the first cat owner to “John Doe” and his dog’s name to “Whiskers.” Change the name of the second cat owner to “George Washington.”
2. Increase the meowing loudness of the first cat owner’s cat by .1, and set the speed of the second cat owner’s cat to 5.
3. Double the food supply of the first cat owner, and set the food supply of the second to 25.
4. Have the first cat owner feed his cat three times, and have the second cat owner feed his cat twice.
5. Print out all the information for both cat owners.

Solutions

Expert Solution

Cat.java

public class Cat {

    private int speed;

    private double meowingLoudness;

    private String name;

   

    Cat() {

        speed = (int)Math.round(Math.random()*9.49);

        meowingLoudness = Math.random()*10;

        name = "newCat";

    }

    public int getSpeed() {

        return speed;

    }

    public void setSpeed(int speed) {

        this.speed = speed;

    }

    public double getMeowingLoudness() {

        return meowingLoudness;

    }

    public void setMeowingLoudness(double meowingLoudness) {

        this.meowingLoudness = meowingLoudness;

    }

    public String getName() {

        return name;

    }

    public void setName(String name) {

        this.name = name;

    }

   

    public void disp(){

        System.out.println("Cat name: "+ name

                + "\n speed: " + speed

                + "\nMeowing Loudness: " + meowingLoudness);

    }

   

    public static void main(String args[]){

        Cat c1 = new Cat();

        c1.disp();

        c1.setName("Alpha");

        c1.setSpeed(c1.getSpeed()-1);

        c1.disp();

    }

}

CatOwner.java

public class CatOwner {

    String name;

    int foodLeft;

    Cat ownersCat;

   

    public CatOwner() {

        name = "newOwner";

        foodLeft = (int)Math.round(Math.random()*40 + 10);

        ownersCat = new Cat();

        ownersCat.setName("Tinkles");

    }

    public String getName() {

        return name;

    }

    public void setName(String name) {

        this.name = name;

    }

    public int getFoodLeft() {

        return foodLeft;

    }

    public void setFoodLeft(int foodLeft) {

        this.foodLeft = foodLeft;

    }

   

    public int getCatSpeed() {

        return ownersCat.getSpeed();

    }

    public void setCatSpeed(int speed) {

        ownersCat.setSpeed(speed);

    }

    public double getCatMeowingLoudness() {

        return ownersCat.getMeowingLoudness();

    }

    public void setCatMeowingLoudness(double meowingLoudness) {

        ownersCat.setMeowingLoudness(meowingLoudness);

    }

    public String getCatName() {

        return ownersCat.getName();

    }

    public void setCatName(String name) {

        ownersCat.setName(name);

    }

   

    public void feedCat(){

        foodLeft--;

        System.out.println("Cat was fed");

    }

   

    public void disp(){

        System.out.println("Cat name: "+ name

                + "\n Food Left: " + foodLeft

                + "\nCat details:");

        ownersCat.disp();

    }

   

    public static void main(String[] args){

        CatOwner co1 = new CatOwner();

        co1.disp();

        co1.setCatMeowingLoudness(11);

        co1.setName("Beta");

        co1.disp();

    }

}

MainClass.java

public class MainClass {

    public static void main(String[] args) {

        CatOwner co1 = new CatOwner();

        CatOwner co2 = new CatOwner();

       

        co1.setName("John Doe");

        co1.setCatName("Whisker");

        co2.setName("George Washington");

       

        co1.setCatMeowingLoudness(co1.getCatMeowingLoudness()+1);

        co2.setCatSpeed(5);

       

        co1.setFoodLeft(co1.getFoodLeft()*2);

        co2.setFoodLeft(25);

       

        co1.feedCat();

        co1.feedCat();

        co1.feedCat();

        co2.feedCat();

        co2.feedCat();

       

        co1.disp();

        co2.disp();

    }

}


Related Solutions

JAVA Specify, design, and implement a class called PayCalculator. The class should have at least the...
JAVA Specify, design, and implement a class called PayCalculator. The class should have at least the following instance variables: employee’s name reportID: this should be unique. The first reportID must have a value of 1000 and for each new reportID you should increment by 10. hourly wage Include a suitable collection of constructors, mutator methods, accessor methods, and toString method. Also, add methods to perform the following tasks: Compute yearly salary - both the gross pay and net pay Increase...
JAVA - Design and implement a class called Flight that represents an airline flight. It should...
JAVA - Design and implement a class called Flight that represents an airline flight. It should contain instance data that represent the airline name, the flight number, and the flight’s origin and destination cities. Define the Flight constructor to accept and initialize all instance data. Include getter and setter methods for all instance data. Include a toString method that returns a one-line description of the flight. Create a driver class called FlightTest, whose main method instantiates and updates several Flight...
in java Design and implement a class called Dog that contains instance data that represents the...
in java Design and implement a class called Dog that contains instance data that represents the dog’s name and age. Define the Dog constructor to accept and initialize instance data. Include getter and setter methods for the name and age. Include a method to compute and return the age of the dog in “person years” (seven times the dog’s age). Include a toString method that returns a one-line description of the dog. Create a Tester class called Kennel, whose main...
JAVA PROGRAM Question : Design and implement two classes called InfixToPostfix and PostFixCalculator. The InfixToPrefix class...
JAVA PROGRAM Question : Design and implement two classes called InfixToPostfix and PostFixCalculator. The InfixToPrefix class converts an infix expression to a postfix expression. The PostFixCalculator class evaluates a postfix expression. This means that the expressions will have already been converted into correct postfix form. Write a main method that prompts the user to enter an expression in the infix form, converts it into postfix, displays the postfix expression as well as it's evaluation. For simplicity, use only these operators,...
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...
Assignment 4: Objects that contain objects In this assignment, you will implement a Java application, called...
Assignment 4: Objects that contain objects In this assignment, you will implement a Java application, called BankApplication, that can be used to manage bank accounts. The application is to be implemented using three classes, called Account, Customer, and BankDriver. The description of these classes is below. Problem Description class Account The Account class keeps track of the balance in a bank account with a varying annual interest rate. The Account class is identified as follows: • two double instance variables,...
Suppose we have a Java class called Person.java public class Person { private String name; private...
Suppose we have a Java class called Person.java public class Person { private String name; private int age; public Person(String name, int age) { this.name = name; this.age = age; } public String getName(){return name;} public int getAge(){return age;} } (a) In the following main method which lines contain reflection calls? import java.lang.reflect.Field; public class TestPerson { public static void main(String args[]) throws Exception { Person person = new Person("Peter", 20); Field field = person.getClass().getDeclaredField("name"); field.setAccessible(true); field.set(person, "Paul"); } }...
Java Q1: Create a class named Triangle, the class must contain: Private data fields base and...
Java Q1: Create a class named Triangle, the class must contain: Private data fields base and height with setter and getter methods. A constructor that sets the values of base and height. A method named toString() that prints the values of base and height. A method named area() that calculates and prints the area of a triangle. Draw the UML diagram for the class. Implement the class. Q2: Write a Java program that creates a two-dimensional array of type integer...
Programming Exercise Implement the following class design: class Tune { private:    string title; public:   ...
Programming Exercise Implement the following class design: class Tune { private:    string title; public:    Tune();    Tune( const string &n );      const string & get_title() const; }; class Music_collection { private: int number; // the number of tunes actually in the collection int max; // the number of tunes the collection will ever be able to hold Tune *collection; // a dynamic array of Tunes: "Music_collection has-many Tunes" public: // default value of max is a conservative...
Design and implement a class called circle_location to keep track of the position of a single...
Design and implement a class called circle_location to keep track of the position of a single point that travels around a circle. An object of this class records the position of the point as an angle, measured in a clockwise direction from the top of the circle. Include these public member functions: • A default constructor to place the point at the top of the circle. • Another constructor to place the point at a specified position. • A function...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT