In: Computer Science
In java program format
Submit your completed UML class diagram and Java file.
Part I: Create a UML diagram for this assignment
PartII: Create a program that implements a class called Dog that contains instance data that represent the dog's name and age. define the Dog constructor to accept and initialize instance data. create a method to compute and return the age of the dog in "person-years" (note: dog age in person-years is seven times a dog's age). Include a toString method that returns a one-line description of the dog Create a driver class called Kennel, whose main method instantiated and updates several Dog objects.
UML Class Diagram:
In the topmost block, we write the class name. Second block contains the instance variables of the class and their datatypes. The third block has the methods listed. In parentheses we write the datatype of parameters and the return type is defined after that. Constructor does not have any return type.
Code:
//class Dog
public class Dog {
// instance variables
public String name;
public int age;
//parameterized constructor
Dog(String _name,int _age){
this.name = _name;
this.age = _age;
}
//method to calculate age in years
int ageInPersonYears(){
return this.age*7;
}
//toString method to provide information about dog
@Override
public String toString() {
return name+" is "+age+" years old. Its age in person-years is: "+ageInPersonYears();
}
}
// Kennel class
class Kennel{
// main method to test the Dog class
public static void main(String[] args) {
// instantiating different dogs
Dog dog1 = new Dog("Tommy",5);
Dog dog2 = new Dog("Rickie",4);
Dog dog3 = new Dog("Berdy",6);
// printing the details of created dogs
System.out.println(dog1);
System.out.println(dog2);
System.out.println(dog3);
}
}
Screenshot for better understanding:
Output:
As there is no specified access modifier for the instance variables, we can make them public. Constructor takes in the parameters for both name and age. The ageInPersonYear() method simply returns the product of age with 7.