Consider the following classes: public class Animal { public Animal(String itsName) { name = itsName; } public void setName(String itsName) { name = itsName; } public String getName() { return name; } private String name; } public class Dog extends Animal { public Dog(String itsName) { super(itsName + “Dog”); sound = “Bark!”; } public void setSound(String itsSound) { sound = itsSound; } public String getSound() { return (getName(0) + sound); } public String getName(int choice) { if(choice > 0) return super.getName(); else return “AnyDog”; } public String getName() { return super.getName() + “OK”; } private String sound; } In addition, the following lines of code has been placed in a main class public static void main(String[] args) { Animal a1 = new Animal(“Tweety”); Animal a2 = new Dog(“Rover”); Dog d1 = new Dog(“Spot”); . . . . } Give the values of the following expressions. If an expression is erroneous, state why. getName() getName() getName(0)

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
icon
Related questions
Question
  1. Consider the following classes:

public class Animal

{     public Animal(String itsName) { name = itsName;  }

      public void setName(String itsName) { name = itsName;  }

      public String getName() { return name; }

private String name;

}

 

public class Dog extends Animal

{     public Dog(String itsName)

{ super(itsName + “Dog”);

  sound = “Bark!”;

}

public void setSound(String itsSound) { sound = itsSound;   }

public String getSound() { return (getName(0) + sound);     }

public String getName(int choice)

{ if(choice > 0) return super.getName();

  else return “AnyDog”; }

public String getName() { return super.getName() + “OK”; }

 

      private String sound;

}

 

In addition, the following lines of code has been placed in a main class

public static void main(String[] args)

  {  Animal a1 = new Animal(“Tweety”);

      Animal a2 = new Dog(“Rover”);

Dog d1 = new Dog(“Spot”);

                  . . . . }

 

Give the values of the following expressions. If an expression is erroneous, state why.

 

 

  1. getName()
  2. getName()
  3. getName(0)
  4. (new Animal()).getName()
  5. getName()
  6. getName(1)
  7. ((Animal)d1).getName()
  8. ((Dog)a1).getName()
  9. d1 instanceof Animal
  10. a1 instanceof Dog
  11. getSound()
  12. getSound()
  13. getSound()
  14. ((Dog)a2).getSound()
  15. ((Animal) d1).getSound()
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 3 steps with 1 images

Blurred answer
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY