Write a main method that will accomplish the following: You may assume that the
ID: 3829841 • Letter: W
Question
Write a main method that will accomplish the following:
You may assume that the class includes all appropriate set methods (also known as mutator methods) and get methods (also known as accessor methods).
The output can be completed using console output or GUI output.
• Create an instance of the Cat class named cat1 that is a Persian weighing 15.5 pounds owned by Ali Khan.
• Create an instance of the Cat class named cat2 that is a Siamese owned by Kim Lee. Use the overloaded constructor that takes two arguments.
• Change the weight of cat1 to 18.3; • Print out the owner of cat2 including text identifying which value you are printing and also the value itself.
Explanation / Answer
CatMain.java
public class CatMain {
public static void main(String[] args) {
Cat cat1 = new Cat("Ali Khan", 15.5);
Cat cat2 = new Cat("Kim Lee");
cat1.setWeight(18.3);
System.out.println("Cat2 owner: "+cat2.getOwner());
}
}
Cat.java
public class Cat {
private String owner;
private double weight;
public Cat(String owner, double weight){
this.owner = owner;
this.weight = weight;
}
public Cat(String owner){
this.owner = owner;
}
public String getOwner() {
return owner;
}
public void setOwner(String owner) {
this.owner = owner;
}
public double getWeight() {
return weight;
}
public void setWeight(double weight) {
this.weight = weight;
}
@Override
public String toString() {
return "Cat [owner=" + owner + ", weight=" + weight + "]";
}
}
Output:
Cat2 owner: Kim Lee
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.