In: Computer Science
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 method instantiates and updates several Dog objects.
Program Code Screenshot Dog.java
Program Code Screenshot Kennel.java
Program Sample Console Input/Output Screenshot
Program Code to Copy Dog.java
public class Dog {
// instance variables
public String name;
public int age;
// constructor
public Dog(String name, int age) {
this.name = name;
this.age = age;
}
// getter and setter methods
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return this.age;
}
public void setAge(int age) {
this.age = age;
}
// method to return age od dog in person year
public int personYears() {
return age * 7;
}
// to string method
public String toString() {
return "Name: " + name + ", Age: " + age + ", PersonYears: " + personYears();
}
}
Program Code to copy Kennel.java
public class Kennel {
// testing in main
public static void main(String[] args) {
Dog d1 = new Dog("Tom", 4);
Dog d2 = new Dog("Gary", 9);
Dog d3 = new Dog("Lam", 2);
Dog d4 = new Dog("Dub", 6);
// print dog's info
System.out.println("Printing dog Info");
System.out.println(d1);
System.out.println(d2);
System.out.println(d3);
System.out.println(d4);
System.out.println();
}
}