Answered step by step
Verified Expert Solution
Link Copied!
Question
1 Approved Answer

In Java, how to apply ArrayList to print out the sample output for animal details. Is there any input for public void greetings() ? (In

In Java, how to apply ArrayList to print out the sample output for animal details. Is there any input for public void greetings() ? (In Bold)

class Animal { // Superclass (parent)

String name; //attributes

int age;

String color;

Animal(){ //default constructor

System.out.println("In animal constructor");

}

Animal(String name, int age, String color){ //parameterized constructor

System.out.println("In animal constructor");

this.name = name;

this.age = age;

this.color = color;

}

public void greetings() {

}

public void printInfo() {

//Prints the subclass details

System.out.print(this.name + " is " + this.age + " years old and is of " + this.color + " color ");

}

}

class Cat extends Animal{

Cat(String n, int a, String c) { //parameterized constructor

super(n, a, c);

System.out.println("In cat constructor");

}

public void greetings() {

super.greetings(); // Call the superclass method

System.out.print("Meow ");

}

public void printInfo(){

super.printInfo(); // Call the superclass method

}

}

class Dog extends Animal{

Dog(){ //default constructor

System.out.println("In dog constructor");

}

Dog(String n,int a, String c) { //parameterized constructor

super(n, a, c);

System.out.println("In dog constructor");

}

// Subclass (child)

public void greetings() {

super.greetings(); // Call the superclass method

System.out.println("Woof woof");

}

public void printInfo(){

super.printInfo(); // Call the superclass method

}

}

class Duck extends Animal{

Duck(String n, int a, String c) { //parameterized constructor

super(n, a, c);

System.out.println("In duck constructor");

}

public void greetings() {

super.greetings(); // Call the superclass method

System.out.print("Quack ");

}

public void printInfo(){

super.printInfo(); // Call the superclass method

}

}

class Pig extends Animal{

Pig(String n, int a, String c) { //parameterized constructor

super(n, a, c);

System.out.println("In pig constructor");

}

public void greetings() {

super.greetings(); // Call the superclass method

System.out.print("Oink ");

}

public void printInfo(){

super.printInfo(); // Call the superclass method

}

}

**Help with the below codes to use ArrayList**

class Main {

public static void main(String[] args) {

Dog myDog = new Dog("Dog Milo", 2,"Brown"); //Creates an object for Dog class

Cat myCat = new Cat("Cat Whiskey", 3, "Brown"); //Creates an object for Cat class

Duck myDuck = new Duck("Duck Do", 1, "White");

Pig myPig = new Pig("Pig Po", 10, "Pink");

myDog.printInfo(); //Prints Dog details

myDog.greetings(); //Prints sound of Dog

myCat.printInfo(); //Prints Cat details

myCat.greetings(); //Prints sound of Cat

myDuck.printInfo(); //Prints Duck details

myDuck.greetings(); //Prints sound of Duck

myPig.printInfo(); //Prints Pig details

myPig.greetings(); //Prints sound of Pig

}

}

Sample output:

In animal constructor In dog constructor In animal constructor In cat constructor In animal constructor In duck constructor In animal constructor In pig constructor Dog Milo is 2 years old and is of Brown color Woof woof Cat Whiskey is 3 years old and is of Brown color Meow Duck Do is 1 years old and is of White color Quack Pig Po is 10 years old and is of Pink color Oink

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_2

Step: 3

blur-text-image_3

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

Introduction To Constraint Databases

Authors: Peter Revesz

1st Edition

1441931554, 978-1441931559

More Books

Students explore these related Databases questions