How do you acomplish this assignment? Lab Polymorphism CSIS-1410 Learning Object
ID: 3886750 • Letter: H
Question
How do you acomplish this assignment?
Lab Polymorphism CSIS-1410 Learning Objective: Deepen your understanding of polymorphism Create an array of a superclass type Use the method getClass() to access the runtime class of the current object breed communicate() move +getBreed () Practice the use of the instanceof operator Instructions: +pullSled () + move () Download the starter project from Canvas and unzip it in a folder with the same name. (If you change the folder name you will need to adjust the package name) Import the extracted ( unzipped) code files into Eclipse You can do that like this: " Right-click the src folder that should include the new package > Import the Output O bark bark run bark bark Import dialog opens run " Select General> File System and click Next the Import from directory bark bark tightrope walking dialog opens *Use the Browse button to navigate to the folder lablnheritance and click OK Output 1: Select the checkbox next to the folder lablnheritance Dog: Greyhound bark bark run IMPORTANT: Select the checkbox next to Create top-level folder * Click Finish Run App to make sure that the file import worked as expected At this point the output should look like Output 0. SledDog: Husky bark bark run CircusDog: Terrier bark bark tightrope walkingExplanation / Answer
Dog.java
public class Dog {
//Declaring instance variables
private String breed;
//Parameterized constructor
public Dog(String breed) {
super();
this.breed = breed;
}
//Getter method and setter
public String getBreed() {
return breed;
}
public void setBreed(String breed) {
this.breed = breed;
}
//This method will display how the dog communicate
public void communicate() {
System.out.println("bark bark");
}
//This method will display how the dog move
public void move() {
System.out.println("run");
}
//toString method is used to display the contents of an object inside it
@Override
public String toString() {
return this.getClass().getSimpleName() + ": " + getBreed();
}
}
___________________
CircusDog.java
public class CircusDog extends Dog {
//Parameterized constructor
public CircusDog(String breed) {
super(breed);
}
@Override
public void move() {
System.out.println("tightrope walking");
}
//toString method is used to display the contents of an object inside it
@Override
public String toString() {
return super.toString();
}
}
___________________
SledDog.java
public class SledDog extends Dog {
//Parameterized constructor
public SledDog(String breed) {
super(breed);
}
public void pullSled() {
System.out.println("pulling the sled");
}
//toString method is used to display the contents of an object inside it
@Override
public String toString() {
return super.toString();
}
}
______________________
DogApp.java
public class DogApp {
public static void main(String[] args) {
System.out.println("Using an array :");
Dog myDog = new Dog("Greyhound");
SledDog mySledDog = new SledDog("Husky");
CircusDog myCircusDog = new CircusDog("Terrier");
Dog arra[] = new Dog[] {
myDog,
mySledDog,
myCircusDog
};
for (Dog d: arra) {
System.out.println(d.toString());
if (d instanceof SledDog) {
((SledDog) d).pullSled();
}
d.communicate();
d.move();
System.out.println();
}
}
}
_____________________
Output:
Using an array :
Dog: Greyhound
bark bark
run
SledDog: Husky
pulling the sled
bark bark
run
CircusDog: Terrier
bark bark
tightrope walking
_________________Thank You
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.