Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

PLEASE DO IN JAVA Doc Comments: All public classes, constructors, and methods should have doc comments. Exceptions: no need to add a doc comment to

  • PLEASE DO IN JAVA

  • Doc Comments: All public classes, constructors, and methods should have doc comments. Exceptions: no need to add a doc comment to the overridden toString methods. If you don't, the original doc comments that were specified in class Object are kept. Who should be listed as author? For classes, that were modified but not newly created by you, list "CSIS + yourName" Where youName is your actual name. Quotes should not be included.

  • In class Dog: Add an overridden toString method. It should return a string of the following format: {name of the class}: {breed} Where {name of the class} is the name of the class and {breed} is the value of the field breed. E.g.: Dog: Terrier Rather than hard-coding the name of the class, obtain it dynamically (at runtime). Here is how this can be done: 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, a 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 below ) Hint: There is no need to call the toString method explicitly.

Step 3:

  • class DogApp: Still in DogApp at the end of the main method, do the following: Print the header Using an Array:. This helps to make the code more clear and easier to read. 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:
    1. print the current instance of the dog followed by a new line
    2. call the method actAsDog and pass the current instance of the dog as argumentCompile and run.

Step 4:

  • 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) { // do something } 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.

HERE ARE THE PRELOADED CLASSES:

DogApp

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); }

private static void actAsDog(Dog d) { d.communicate(); d.move(); System.out.println(); } }

CircusDog

public class CircusDog extends Dog { public CircusDog(String b) { super(b); }

@Override public void move() { System.out.println("tightrope walking"); } }

Dog

public class Dog { private final String 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; } }

SledDog

public class SledDog extends Dog { public SledDog(String b) { super(b); }

public void pullSled() { System.out.println("pulling the sled"); } }

OUTPUT EXPECTED: THIS IS WHAT THE FINAL OUTPUT SHOULD LOOK LIKE

Dog: Greyhound bark bark run SledDog: Husky bark bark run CircusDog: Terrier bark bark tightrope walking Using an Array: Dog: Greyhound bark bark run SledDog: Husky pulling the sled bark bark run CircusDog: Terrier bark bark tightrope walking

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions