In: Computer Science
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.
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();
}
}