The Pet class should also have the following methods:
2. Write a test class to test your Pet class. Make sure you have three different types of pets. (three Pet objects)
public class Pet {
private String name;
private String animal;
private int age;
public void setName(String name) {
this.name = name;
}
public void setAnimal(String animal) {
this.animal = animal;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public String getAnimal() {
return animal;
}
public int getAge() {
return age;
}
@Override
public String toString() {
return name+" "+animal+" "+age;
}
}
public class PetTest {
public static void main(String[] args) {
Pet cat = new Pet();
cat.setName("Tom");
cat.setAnimal("Cat");
cat.setAge(3);
Pet dog = new Pet();
dog.setName("Goofy");
dog.setAnimal("Dog");
dog.setAge(7);
Pet bird = new Pet();
bird.setName("Tweety");
bird.setAnimal("Bird");
bird.setAge(2);
System.out.println(cat);
System.out.println(dog);
System.out.println(bird);
}
}
Comments
Leave a comment