Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

1. Create a new class called Cat: i. A Cat has a name (String) and an age in years (int). ii. The constructor should accept

1. Create a new class called Cat: i. A Cat has a name (String) and an age in years (int).

ii. The constructor should accept a parameter for each instance variable. If the user tries to create a Cat with a negative age, give the new Cat an age of zero. If the user tries to create a Cat with a blank name or no name, call the cat Cleo.

iii. A cat needs an accessor for each instance variable.

iv. A Cats name will not change. Make the instance variable that stores the name final, and dont create a mutator for it. You should create a mutator for the Cats age.

v. Create a toString for the cat.

2. Create a new class called CatHotel:

i. A CatHotel has an ArrayList of Cat to store the guests, and a String for the hotels name.

ii. The Constructor should accept a String for the name of the hotel and assign it to the instance variable. The constructor should instantiate the ArrayList of Cat as well.

iii. The CatHotel needs a public method called addCat. The method accepts a Cat as its parameter, and adds the Cat to the ArrayList of guests. There is no return vaue.

iv. The CatHotel needs a public method called removeAllGuests(). It should remove (clear) all the guests from the hotel. We can do this by using one of the helpful ArrayList methods (check out the ArrayList Java doc!).

v. The CatHotel needs a public method called guestCount() which should return the number of guests currently in the CatHotel.

vi. The CatHotel needs a public method called removeOldGuests. This method should accept an integer. Use an iterator to iterate through the guests ArrayList and check each guests age. If the guest is older than the integer passed to the method, remove it from the CatHotel. This method should return the number of Cats that were evicted from the CatHotel. To create an iterator that iterates over an ArrayList, you can use the Iterator return from the collections iterator method like this:

 Iterator catIterator = catList.iterator(); while (catIterator.hasNext()) { 
 Cat temp = catIterator.next(); 
 // Add your code here 

}

i. The CatHotel needs a public printGuestList method that should print the name of the CatHotel and then use a loop to print the toString method for each Cat currently in the CatHotel. Hint: use a for-each loop!

3. Create a Driver file that contains a main method. Inside the main method, create an instance of the CatHotel class. Use a Random object to add some cats with random ages to your CatHotel. Test the methods you implemented to make sure they work.

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

Recommended Textbook for

Beyond Big Data Using Social MDM To Drive Deep Customer Insight

Authors: Martin Oberhofer, Eberhard Hechler

1st Edition

0133509796, 9780133509793

More Books

Students also viewed these Databases questions