Question

In: Computer Science

Animal class Create a simple class called Animal instantiated with a name and a method toString...

Animal class

Create a simple class called Animal instantiated with a name and a method toString which returns the name.

Cat class

Create a simple class Cat which extends Animal, but adds no new instance variable or methods.

RedCat class

Create a simple class RedCat which extends Cat, but adds no new instance variable or methods.

WildCardTester class

Create a class with the main method and methods addCat, deleteCat and printAll as follows.

addCat method

Has two parameters, an ArrayList with a lower bound of RedCat, and a String with the name of a Cat. Adds the identified Cat reference to the ArrayList, and prints out a line as shown in the sample output below.

deleteCat method

Has two parameters, an ArrayList with an upper bound of Cat, and a String with the name of a Cat to be deleted. Deletes the identified Cat reference to the ArrayList, and prints out a line as shown in the sample output below.

printAll method

Has a single parameter of an ArrayList with a wildcard type. Prints out all the items in the ArrayList.

main method

Create two ArrayLists, one for Animal, and one for RedCat.

Use addCat to add “Tiger” to the animal ArrayList.

Use addCat to add “Tom”, “Siamese” and “Tiger” to the redCats ArrayList.

Use printAll to print the list in the animal ArrayList.

Use printAll to print the list in the redCat ArrayList.

Use deleteCat to delete the first item in the redCat ArrayList.

Use printAll to print the list in the redCat ArrayList.

Sample Output:

Cat Added named Tiger was added

Cat Added named Tom was added

Cat Added named Siamese was added

Cat Added named Tiger was added

The list of animals:

Tiger

The list of redCats:

Tom Siamese Tiger

Removed cat named Tom

The list of redCats after a deletion:

Siamese Tiger

Solutions

Expert Solution

Program Screenshots:

Animal.java

Cat.java

RedCat.java

WildCardTester.java

Program Code :

Animal.java

//Create a simple class called Animal instantiated with a name and a method toString which returns the name.
public class Animal {
   String name;

   public Animal(String name) {
       this.name = name;
   }

   @Override
   public String toString() {
       return name;
   }

}

Cat.java

//Create a simple class Cat which extends Animal, but adds no new instance variable or methods.
public class Cat extends Animal {

   public Cat(String name) {
       super(name);
      
   }

}

RedCat.java

//Create a simple class RedCat which extends Cat, but adds no new instance variable or methods.
public class RedCat extends Cat {

   public RedCat(String name) {
       super(name);
      
   }

}

WildCardTester.java

//Create a class with the main method and methods addCat, deleteCat and printAll as follows
import java.util.ArrayList;
import java.util.List;

public class WildCardTester {

   public static void main(String[] args) {
      
//       Create two ArrayLists, one for Animal, and one for RedCat.
       List<Animal> animal = new ArrayList<Animal>();
       List<RedCat> redCat = new ArrayList<RedCat>();
       addCat(animal,"Tiger");
       addCat(redCat,"Tom");
       addCat(redCat,"Siamese");
       addCat(redCat,"Tiger");
       System.out.println("The list of animals:");
       for(Animal a : animal) {
           System.out.print(a + " ");
       }
       System.out.println();
       System.out.println("The list of redCats:");
       for(RedCat r : redCat) {
           System.out.print(r + " ");
       }
       System.out.println();
       deleteCat(redCat, "Tom");
       printAll(redCat);
   }
//   printAll method:Has a single parameter of an ArrayList with a wildcard type. Prints out all the items in the ArrayList.
   private static void printAll(List<? super RedCat> lists) {
       System.out.println("The list of redCats after a deletion:");
       for(Object r : lists) {
           System.out.print(r + " ");
       }
      
   }
  
   /*deleteCat method:Has two parameters, an ArrayList with an upper bound of Cat,
   and a String with the name of a Cat to be deleted.
   Deletes the identified Cat reference to the ArrayList, and prints out a line as shown in the sample output below.*/

   private static void deleteCat(List<RedCat> lists1, String name1) {
       System.out.println("Removed cat named " + name1);
       for(RedCat rc : lists1) {
           if(rc.name.equals(name1)) {
               lists1.remove(rc);
               break;
           }
       }
      
   }
  
   /*addCat method :Has two parameters, an ArrayList with a lower bound of RedCat, and a String with the name of a Cat.
   Adds the identified Cat reference to the ArrayList, and prints out a line as shown in the sample output below.*/
   private static void addCat(List<? super RedCat> lists, String name1) {
       lists.add(new RedCat(name1));
       System.out.println("Cat Added named " + name1 + " was added");
      
      
   }

}

Sample Output:


Related Solutions

Suppose that Account class has a method called toString, which will be inherited by Checking and...
Suppose that Account class has a method called toString, which will be inherited by Checking and Savings class. The toString method will perform according to account type. It will return a string that describes the account information. So, the late bind is needed. Declare the toString method in Account class. The method should take no argument and return a string that describes the account information. IT IS C++
Suppose that Account class has a method called toString, which will be inherited by Checking and...
Suppose that Account class has a method called toString, which will be inherited by Checking and Savings class. The toString method will perform according to account type. It will return a string that describes the account information. So, the late bind is needed. Declare the toString method in Account class. The method should take no argument and return a string that describes the account information. c++
Exercise #1: Create an abstract class called GameTester. The GameTester class includes a name for the...
Exercise #1: Create an abstract class called GameTester. The GameTester class includes a name for the game tester and a boolean value representing the status (full-time, part-time). Include an abstract method to determine the salary, with full-time game testers getting a base salary of $3000 and part-time game testers getting $20 per hour. Create two subclasses called FullTimeGameTester, PartTimeGameTester. Create a console application that demonstrates how to create objects of both subclasses. Allow the user to choose game tester type...
Create a class called Student which stores • the name of the student • the grade...
Create a class called Student which stores • the name of the student • the grade of the student • Write a main method that asks the user for the name of the input file and the name of the output file. Main should open the input file for reading . It should read in the first and last name of each student into the Student’s name field. It should read the grade into the grade field. • Calculate the...
In java: -Create a class named Animal
In java: -Create a class named Animal
JAVA Program Create a class called SoccerPlayer Create 4 private attributes: First Name, Last Name, Games,...
JAVA Program Create a class called SoccerPlayer Create 4 private attributes: First Name, Last Name, Games, and Goals Have two constructors Constructor 1 – default constructor; all values to "NONE" or zero Constructor 2 – accepts input of first name, last name, games and goals. Create get and set methods for each of the four attributes Create a method the returns a double that calculates the average goals per game This method checks for zero games played: If there are...
Create a new class called Account with a main method that contains the following: • A...
Create a new class called Account with a main method that contains the following: • A static variable called numAccounts, initialized to 0. • A constructor method that will add 1 to the numAccounts variable each time a new Account object is created. • A static method called getNumAccounts(). It should return numAccounts. Test the functionality in the main method of Account by creating a few Account objects, then print out the number of accounts
Name: __________________________________________ Specifications: Create a class called WordMaker that is passed characters one at a time...
Name: __________________________________________ Specifications: Create a class called WordMaker that is passed characters one at a time and assembles them into words. As long as the characters passed to it are letters, it builds a word by placing each character on the end of a string. When a character that is not part of a word (i.e., is not a letter) is encountered, the class signals that the word is complete, and makes a string containing the word available to its...
Static methods can be called directly from the name of the class that contains the method....
Static methods can be called directly from the name of the class that contains the method. Static methods are usually created to do utility operations. For example: public class Test{ public static int timesTwo(int value){ return value * value; } } public class TestDriver{ public static void main(String[] args){ int var = Test.timesTwo(5); System.out.println(var); } } Create a class called ManyLinkedLists. It will contain a static method called createLinkedList(). That method takes an argument that is a constant defined in...
Create a method on the Iterator class called forEach, which appropriate behavior. This method should use...
Create a method on the Iterator class called forEach, which appropriate behavior. This method should use next(), and should not use this._array.forEach! Take note of what should happen if you call forEach twice on the same Iterator instance. The estimated output is commented below In javascript please class Iterator { constructor(arr){ this[Symbol.iterator] = () => this this._array = arr this.index = 0 } next(){ const done = this.index >= this._array.length const value = this._array[this.index++] return {done, value} } //YOUR WORK...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT