4. Consider the following classes: public class Animal { public Animal(String itsNane) { name public void setName (String itsName) { name public String getName () { return name; } itsNane; } = itsName; %3D } www private String name; public class Dog extends Animal public Dog(String itsName) { super(itsName + "Dog"); sound = "Bark!"; { } public void setSound (String itsSound) { sound = public String getsound () { return (getNane (0) + sound); public String getName,(int choice) { if(choice > 0) return super getName() ; else itsSound; } wwww wA } wwww "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 al = 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. a) al.getName () i) d1 instanceof Animal j) al instanceof Dog k) al.getSound () b) a2.getName () c) a2.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
4. Consider the following classes:
public class Animal
{
public Animal (String itsNane) { name =
public void setName (String itsName) { name = itsName;
public String getName ()
it&Nane;
}
}
{ return name;
}
private String name;
}
public class Dog extends Animal
public Dog (String itsName)
{ super (itsName + "Dog");
sound = "Bark!";
min
}
public void setSound (String itsSound) { sound
public String getsewnd ()
public String getName (int choice)
{ if(choice > 0) return super getName ();
else return "AnyDog" ;
public String getName ()
itsSound;
+ sound);
}
{ return (gstNane (0)
}
}
{ 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[] axge)
new Animal("Tweety");
new Dog ("Rover");
= new Dog ("Spot");
{
Animal al
Animal a2 =
Dog dl
}
Give the values of the following expressions. If an expression is erroneous, state why.
a)
al.getName ()
i) d1 instanceof Animal
j) al instanceof Dog
k) al.getSound()
1) a2.getSound ()
b) a2.getName()
c) a2.getName(0)
(new Animal()).getName ()
d)
e)
d1.getName ()
m) d1.getSound()
( (Dog)a2).getsound
( (Animal) d1).getSound ()
f) d1.getName(1)
n)
()
( (Animal)d1).getName()
( (Dog)a1).getName ()
g)
o)
h)
Transcribed Image Text:4. Consider the following classes: public class Animal { public Animal (String itsNane) { name = public void setName (String itsName) { name = itsName; public String getName () it&Nane; } } { return name; } private String name; } public class Dog extends Animal public Dog (String itsName) { super (itsName + "Dog"); sound = "Bark!"; min } public void setSound (String itsSound) { sound public String getsewnd () public String getName (int choice) { if(choice > 0) return super getName (); else return "AnyDog" ; public String getName () itsSound; + sound); } { return (gstNane (0) } } { 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[] axge) new Animal("Tweety"); new Dog ("Rover"); = new Dog ("Spot"); { Animal al Animal a2 = Dog dl } Give the values of the following expressions. If an expression is erroneous, state why. a) al.getName () i) d1 instanceof Animal j) al instanceof Dog k) al.getSound() 1) a2.getSound () b) a2.getName() c) a2.getName(0) (new Animal()).getName () d) e) d1.getName () m) d1.getSound() ( (Dog)a2).getsound ( (Animal) d1).getSound () f) d1.getName(1) n) () ( (Animal)d1).getName() ( (Dog)a1).getName () g) o) h)
Expert 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