Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

My teacher assigned the following assignment but I\'m really struggling to grasp

ID: 3590818 • Letter: M

Question

My teacher assigned the following assignment but I'm really struggling to grasp polymorphism.

Assignment:

-Create a new method called displayAll, that takes an ArrayList (of your base class) as a parameter, and doesn't return anything

-The displayAll method will loop through the ArrayList, and call the display (or toString) method on each object

-In the main method, create an ArrayList containing objects of both your base class and subclass. (You should have at least 4 objects)  

-In the main method, make a call to the displayAll method, passing your ArrayList of objects

The current code needed to update starts below this line

//Dogs.java

//DogsSubclass.java

//Demo.java

Explanation / Answer

import java.util.*;

class Dogs {

private int numDogs;

private String colorFur;

private boolean goodDog;

public Dogs() {

numDogs = 0;

colorFur = null;

goodDog = false;

}

public Dogs(int numDogsPark, String dogFurColor, boolean goodDoggy) {

numDogs = numDogsPark;

colorFur = dogFurColor;

goodDog = goodDoggy;

}

public void setNum(int numDogsPark) {

numDogs = numDogsPark;

return;

}

public void setColor(String dogFurColor) {

colorFur = dogFurColor;

return;

}

public void setBehavior(boolean goodDoggy) {

goodDog = goodDoggy;

return;

}

public int getNum() {

return numDogs;

}

public String getColor() {

return colorFur;

}

public boolean getBehavior() {

return goodDog;

}

public void Display() {

System.out.println(numDogs);

System.out.println(colorFur);

System.out.println(goodDog);

}

}

class DogsSubclass extends Dogs {

private String dogType;

public DogsSubclass() {

//call the default constructor of Dogs class using super() with no arguments

super();

dogType=null;

}

public DogsSubclass(int numDogsPark, String dogFurColor, boolean goodDoggy, String typeDoggy) {

//call the parameterized constructor of Dogs class using super() with the arguments

super(numDogsPark, dogFurColor, goodDoggy);

dogType = typeDoggy;

}

public void setDogType(String coolDogType){

dogType = coolDogType;

return;

}

public String getDogType() {

return dogType;

}

public void Display(){

//As no instruction to call display method of super class is given, display all values in ths function

System.out.println("Number of Dogs : "+ getNum());

System.out.println("Fur color : "+ getColor());

System.out.println("Behavior : "+ getBehavior());

System.out.println("Dogtype : "+ dogType);

}

public static void DisplayAll(ArrayList Al)

{

DogsSubclass dog;

Dogs Bdog;

for(int i=0;i<Al.size();i++)

{

/* Name of the class is stored and compared to know whether it is a parent class or child class*/

String s=Dogs.class.getSimpleName();

if(s=="DogsSubclass")

{

dog=(DogsSubclass)Al.get(i);

dog.Display();

}

if(s=="Dogs")

{

Bdog=(Dogs)Al.get(i);

Bdog.Display();

}

}

}

}

public class Demo

{

public static void main(String[] args) {

Dogs Bdog1=new Dogs(1,"Black",true);

Dogs Bdog2=new Dogs(3,"Golden",true);

Dogs Bdog3=new Dogs(2,"Silver",true);

Dogs Bdog4=new Dogs(4,"Maroon",true);

DogsSubclass dog3 = new DogsSubclass(2, "yellow", true,"pug");

DogsSubclass dog2 = new DogsSubclass(3, "red", true,"German Shepherd");

DogsSubclass dog4 = new DogsSubclass(3, "red", true,"Bulldog");

DogsSubclass dog5 = new DogsSubclass(3, "red", true,"Poodle");

DogsSubclass d=new DogsSubclass();

System.out.println("Details of dog2 are displayed below: ");

/* ArrayList is Created*/

ArrayList Al=new ArrayList();

/*Objects are added*/

Al.add(Bdog1);

Al.add(Bdog2);

Al.add(Bdog3);

Al.add(Bdog4);

Al.add(dog2);

Al.add(dog3);

Al.add(dog4);

Al.add(dog5);

/*DisplayAll Function is called*/

d.DisplayAll(Al);

}

}