Can someone help? I am having trouble with making the wording. \"pulling the dog
ID: 3888714 • Letter: C
Question
Can someone help? I am having trouble with making the wording. "pulling the dogsled should come after "SledDog :Husky". Here's the code..
DogApp.java
package labPolymorphism;
public class DogApp
{
public static void main(String[] args)
{
Dog myDog = new Dog("Greyhound");
actAsDog(myDog);
SledDog mySledDog = new SledDog("Husky");
actAsDog(mySledDog);
SledDog SD = new SledDog("Husky");
CircusDog myCircusDog = new CircusDog("Terrier");
actAsDog(myCircusDog);
System.out.println("Using Array");
System.out.println();
Dog dogs[] = new Dog[3];
dogs[0] = myDog;
dogs[1] = mySledDog;
dogs[2] = myCircusDog;
for(Dog d : dogs)
{
if(d instanceof SledDog)
{
SledDog k = (SledDog)d;
k.pullSled();
}
actAsDog(d);
}
}
private static void actAsDog(Dog d)
{
System.out.println(d);
d.communicate();
d.move();
System.out.println();
}
}
----------------------------------------------------------------------------------------------------------------------------------------------------------------------
SledDog.java
package labPolymorphism;
public class SledDog extends Dog
{
public SledDog(String b)
{
super(b);
}
public void pullSled()
{
System.out.println("pulling the sled");
}
}
----------------------------------------------------------------------------------------------------------------------------------------------------------------------
Dog.java
package labPolymorphism;
import java.lang.Object;
public class Dog
{
private final String breed;
public String toString()
{
return this.getClass().getSimpleName() + ": " + breed;
}
public Dog(String b)
{
breed = b;
}
public void communicate()
{
System.out.println("bark bark");
}
public void move()
{
System.out.println("run");
}
public String getBreed()
{
return breed;
}
}
----------------------------------------------------------------------------------------------------------------------------------------------------------------------
CircusDog.java
package labPolymorphism;
public class CircusDog extends Dog
{
public CircusDog(String b)
{
super(b);
}
@Override
public void move()
{
System.out.println("tightrope walking");
}
}
Explanation / Answer
Dog.java
package labPolymorphism;
public class Dog
{
private final String breed;
public String toString()
{
return this.getClass().getSimpleName() + ": " + breed;
}
public Dog(String b)
{
breed = b;
System.out.println(toString());
}
public void communicate()
{
System.out.println("bark bark");
}
public void move()
{
System.out.println("run");
}
public String getBreed()
{
return breed;
}
}
_______________
SledDog.java
package labPolymorphism;
public class SledDog extends Dog
{
public SledDog(String b)
{
super(b);
}
public void pullSled()
{
System.out.println("pulling the sled");
}
}
_________________
CircusDog.java
package labPolymorphism;
public class CircusDog extends Dog
{
public CircusDog(String b)
{
super(b);
}
@Override
public void move()
{
System.out.println("tightrope walking");
}
}
_________________
DogApp.java
package labPolymorphism;
public class DogApp {
public static void main(String[] args) {
Dog myDog = new Dog("Greyhound");
actAsDog(myDog);
SledDog mySledDog = new SledDog("Husky");
actAsDog(mySledDog);
CircusDog myCircusDog = new CircusDog("Terrier");
actAsDog(myCircusDog);
System.out.println("Using Array");
System.out.println();
Dog dogs[] = new Dog[3];
dogs[0] = myDog;
dogs[1] = mySledDog;
dogs[2] = myCircusDog;
for (Dog d: dogs) {
System.out.println(d);
if (d instanceof SledDog) {
SledDog k = (SledDog) d;
k.pullSled();
}
actAsDog(d);
}
}
private static void actAsDog(Dog d) {
d.communicate();
d.move();
System.out.println();
}
}
___________________
Output:
Dog: Greyhound
bark bark
run
SledDog: Husky
bark bark
run
CircusDog: Terrier
bark bark
tightrope walking
Using Array
Dog: Greyhound
bark bark
run
SledDog: Husky
pulling the sled
bark bark
run
CircusDog: Terrier
bark bark
tightrope walking
_____________Could you rate me well.Plz .Thank You