Question: public class Dog { private String name; // the dog's name private String sound = woof; public Dog( String nm ) { name = nm;

public class Dog { private String name; // the dog's name private String sound = "woof"; public Dog( String nm ) { name = nm; } public String speak() { return sound; } public void changeSound( String s ) { sound = s; } @Override public String toString() { return "Dog: " + name; } }
public class Main { public static void main(String[] args) { Dog g = new Dog("Sammy"); System.out.println( g.speak() ); // woof g.changeSound( "arg" ); System.out.println( g.speak() ); // arf BarkingDog bd = new BarkingDog( "Lassie", 3 ); System.out.println( bd.speak() ); // woofwoofwoof bd.changeSound( "Yip" ); System.out.println( bd.speak() ); // YipYipYip System.out.println( bd ); // Barking Dog: Lassie; Barks: 3 } }
/* Write a subclass of Dog named BarkingDog. The BarkingDog class has the following characteristics: - It has one private instance variable, an int num. - Its constructor has two parameters; the first is the dog's name, the second is an int to initialize the instance variable. If num is less than 1, change it to 1. - Override the speak method so it returns a string formed by calling the Dog Class's speak method num times and concatenating the returned values. - Override the toString method so that the returned String follows this format. Barking Dog: (dog's name); Barks: (num) Do NOT override any other methods of the Dog class. */
public class Dog Write a subclass of Dog named BarkingDog. The BarkingDog class has the following characteristics: { private String name; // the dog's name private String sound = "woof"; public Dog( String nm) { name = nm; It has one instance variable, an int num. Its constructor has two parameters; the first is the dog's name, the second is an int to initialize the instance variable. If num is less than 1, change it to 1. Override the speak method so it returns a string formed by calling the Dog Class's speak method num times and concatenating the returned values. Override the toString method Do NOT override any other methods of the Dog class. } public String speak() { return sound; } public void changeSound( Strings) { sound = 5; } @Override public String toString() { return "Dog:" + name; }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
