Dogs Bark 6 points Let’s Go! Not all dogs like to bark

Dogs Bark 6 points Let’s Go! Not all dogs like to bark, but some like to make a lot of noise! In this exercise we have a Dog superclass and a Loud Dog subclass. You do not need to modify the Dog class. Your task is to write two override methods in the Loud Dog class. You will override the speak method to return BARK! . You will then override the two String so that it returns Clover is loud and likes to BARK! where Clover is replaced by the name variable. Create and print at least one Dog and one Loud Dog to test. Status: Not Submitted 9.3.6: Dogs Bark Save Submit + Continue iii FILES DogTester.java Dog.java LoudDog.java 1 public class Dog 2 – { 3 private String name; 4 5 public Dog(String name) { 6 this.name = name; 7 } 8 9 public String getName() { 10 return name; 11 } 12 13 public String speak() { 14 return “Bark!”; } 16 17 public String tostring() { 18 return name + likes to ” 19 } 20 } + speak(); Status: Not Submitted 9.3.6: Dogs Bark FILES 1 public class Loud Dog extends Dog 2 – { 3 4. public Loud Dog(String name) { 5 super (name); 6 } 7 8 // override the speak method here 9 10 11 //override the tostring here. 1 //Remember, you can access the name using super.getName() 13 } 14 0 DogTester.java Dog.java LoudDog.java

The correct answer and explanation is:

Here is the corrected LoudDog.java implementation:

public class LoudDog extends Dog {
    
    public LoudDog(String name) {
        super(name);
    }

    // Override the speak method
    @Override
    public String speak() {
        return "BARK!";
    }

    // Override the toString method
    @Override
    public String toString() {
        return super.getName() + " is loud and likes to BARK!";
    }
}

Explanation

The LoudDog class extends the Dog class, meaning it inherits all the properties and methods from the Dog superclass. However, the LoudDog has special behaviors that we need to define.

  1. Constructor:
    • The LoudDog class has a constructor that takes a String name as a parameter.
    • It uses super(name); to call the constructor of the Dog class, which initializes the name variable.
  2. Overriding speak() method:
    • The original Dog class has a speak() method that returns "Bark!".
    • In LoudDog, we override speak() to return "BARK!" in all capital letters to indicate that LoudDogs bark louder than normal dogs.
  3. Overriding toString() method:
    • The original Dog class has a method toString(), but it was incorrectly written. The proper override in LoudDog should return a string in the format: name + " is loud and likes to BARK!"
    • Since name is private in Dog, we access it using super.getName().

Testing (DogTester.java)

Here is a test class to create and print a Dog and a LoudDog:

public class DogTester {
    public static void main(String[] args) {
        Dog normalDog = new Dog("Buddy");
        LoudDog loudDog = new LoudDog("Clover");

        System.out.println(normalDog.speak()); // Output: Bark!
        System.out.println(loudDog.speak());  // Output: BARK!

        System.out.println(normalDog.toString()); // Output: Buddy likes to Bark!
        System.out.println(loudDog.toString());  // Output: Clover is loud and likes to BARK!
    }
}

Summary:

  • LoudDog is a subclass of Dog.
  • It overrides speak() to return "BARK!".
  • It overrides toString() to return "Clover is loud and likes to BARK!".
  • The DogTester class creates and tests both classes.

Now, generating an image of a loud barking dog for illustration.

I’ve provided the LoudDog.java implementation, a DogTester.java for testing, and an illustration of a loud dog barking energetically. Let me know if you need any modifications! 🚀🐶

Scroll to Top