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

Make the following modification: In class Dog: Add an overridden toString method

ID: 3888519 • Letter: M

Question

Make the following modification:

In class Dog:

Add an overridden toString method. Print the type of the class plus the breed separated with a colon and blank. (e.g. Dog: Terrier )
Here is how you can implement that functionality:
Use getClass, a method of java.lang.Object. It returns the runtime class of the object. Then call the getSimpleName() method on the runtime class. Like this: this.getClass().getSimpleName()

This will return the type of the class, in our case Dog.
Then use the plus operator to add a colon with blank and the breed.

In class DogApp:

Every time you create a new instance of a class add a statement that prints the newly created instance (see Output 1 )
Hint: There is no need to call the toString method explicitly.
Compile and run. Your output should look like Output 1 on the right.

Still in DogApp at the end of main do the following:
Print the header “Using an Array:”. This is to make the output more clear to the user. Create an array of Dogs.
Use the array initializer to initialize the array with myDog, mySledDog, and myCircusDog Use a foreach loop to loop through all the dogs
In the body of the foreach loop do two things:

print the current instance of the dog followed by a new line

call the method actAsDog and pass the current instance of the dog as argument

Compile and run. Now your output should look like Output 2 on the right

Still in DogApp do the following:
Inside the foreach loop right before the actAsDog method call check whether the current dog is-a SledDog.
You can do that by using the instanceof operator
E.g.:
if (object1 instanceof Type1) {

If the current dog happens to be a SledDog then call the method pullSled.

Hint:

In order to be able to access the method pullSled the Dog object still needs to be cast into a SledDog object. This cast is safe because we just checked the type of the Dog object with the instanceof operator.
Once we have a SledDog the method pullSled can be called.

Compile and run. Now your output should look like Output 3 on the right

Output 2:

Output 3:

Explanation / Answer

// Your program was not complete though i tried to get maximum understanding from your question and tried to give the solution. Thanks :)

class Dog {

String breed;

public String getBreed() {

return breed;

}

public void setBreed(String breed) {

this.breed = breed;

}

public void actAsDog(){

System.out.println("bark bark");

}

@Override

public String toString(){

return this.getClass().getSimpleName() + ": " + this.breed;

}

}

class SledDog extends Dog{

public void pullsled(){

System.out.println("pulling the sled");

}

}

class CircusDog extends Dog{

}

public class DogApp{

public static void main(String args[]){

Dog myDog = new Dog();

myDog.setBreed("Greyhound ");

System.out.println(myDog);

SledDog sledDog = new SledDog();

sledDog.setBreed("Husky");

System.out.println(sledDog);

CircusDog circusDog= new CircusDog();

circusDog.setBreed("Terrier");

System.out.println(circusDog);

System.out.println("Using an Array:");

Dog [] dogArray = new Dog[3];

dogArray[0] = myDog;

dogArray[1] = sledDog;

dogArray[2] = circusDog;

for(Dog dog : dogArray){

System.out.println(dog);

if(dog instanceof SledDog){

((SledDog) dog).pullsled();

}

dog.actAsDog();

}

}

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote