Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Please write this Java code and explain each step. What to submit: Dog.java, TestDog.java, screenshot of program output for TestDog.java Define a class named Dog
Please write this Java code and explain each step. What to submit: Dog.java, TestDog.java, screenshot of program output for TestDog.java
Define a class named Dog that represents a dog, and it contains:
An int data field named age that stores the age of the Dog object
A String data field named breed that represents the breed of the Dog object
A constructor with parameters for initializing age and breed
The getter and setter methods for all data
A toString method that returns Dog information, including the Dogs breed and age, eg "Breed: Russell Terrier, Age:
The equals method that returns true if two Dog objects have the same breed and false otherwise
The compareTo method that compares two Dogs and returns if the first Dog object is younger than the second one, if the first Dog object is older than the second one, and if both Dog objects have the same age
Write an application TestDog that prompts the user for the number of dogs in the kennel, called numDogs. For each of the numDogs dogs, the program prompts the user for its breed and age, creates a Dog object and maintain two statistics about the Dog objects:
the average age of all Dog objects created
the oldest dog.
The program will lastly print these two statistics on the screen
HINT: This program MUST make use of the compareTo method in order to find the oldest Dog object.
Below is a sample program run:
Number of dogs in the kennel:
Enter Dog breed: Russell Terrier
Enter age:
Enter Dog breed: Labrador
Enter age:
Enter Dog breed: Labrador
Enter age:
Enter Dog breed: Yorkshire
Enter age:
Average age:
Oldest Dog Breed: Russell Terrier, Age:
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started