Question: Write a subclass of Cat named SmartCat. The SmartCat class has the following characteristics: It has one instance variable, the owner's name Its constructor has

Write a subclass of Cat named SmartCat. The SmartCat class has the following characteristics:

  • It has one instance variable, the owner's name
  • Its constructor has two String parameters; the first is the cat's name, the second is the owner's name.
  • Override the speak method so it returns the String "Hello (owner's name)". There must be exactly one space between Hello and the owner's name.
  • Override the toString method so that the returned String follows this format:

Smart Cat: (cat's name); Owner: (Owner's name)

public class Cat

{

private String name; // the cat's name

public Cat( String nm )

{

name = nm;

}

public String speak()

{

return "meow";

}

@Override

public String toString()

{

return "Cat: " + name;

}

}

SmartCat sc = new SmartCat( "Shadow", "Doug" );

System.out.println( sc.speak() ); // Hello Doug

System.out.println( sc ); // Smart Cat: Shadow; Owner: Doug

Cat.Java:

public class Cat { private String name; // the cat's name public Cat( String nm ) { name = nm; } public String speak() { return "meow"; } @Override public String toString() { return "Cat: " + name; } }

Main.Java:

public class Main { public static void main(String[] args) { Cat c = new Cat("Monkey"); System.out.println( c.speak() ); // meow System.out.println( c ); // Cat: Monkey SmartCat sc = new SmartCat( "Shadow", "Doug" ); System.out.println( sc.speak() ); // Hello Doug System.out.println( sc ); // Smart Cat: Shadow; Owner: Doug } }

SmartCat.Java:

/* Write a subclass of Cat named SmartCat. The SmartCat class has the following characteristics: - It has one private instance variable, the owner's name - Its constructor has two String parameters; the first is the cat's name, the second is the owner's name. - Override the speak method so it returns the String "Hello (owner's name)". There must be exactly one space between Hello and the owner's name. - Override the toString method so that the returned String follows this format. Smart Cat: (dog's name); Owner: (Owner's name)

Be sure to get the spacing and capitalization correct on the strings. There is one space between Smart and Cat in the string that toString returns.

Do NOT change the Cat class. */

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!