Answered step by step
Verified Expert Solution
Link Copied!

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, e.g. "Breed: Russell Terrier, Age: 15"
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 -1 if the first Dog object is younger than the second one, 1 if the first Dog object is older than the second one, and 0 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: 4
Enter Dog breed: Russell Terrier
Enter age: 15
Enter Dog breed: Labrador
Enter age: 10
Enter Dog breed: Labrador
Enter age: 7
Enter Dog breed: Yorkshire
Enter age: 8
Average age: 10.0
Oldest Dog -> Breed: Russell Terrier, Age: 15

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions

Question

What do Dimensions represent in OLAP Cubes?

Answered: 1 week ago