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:**

```java
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() + 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 have been placed in a main class:**

```java
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.**

a) `a1.getName()`
- **Output:** `Tweety`

b) `a2.getName()`
- **Output:** `RoverDogOK`

c) `a2.getName(0)`
- **Error:** Method `getName(int)` is not available in `Animal`

d) `(new Animal()).getName()`
- **Error:** Constructor `Animal` requires a `String` argument

e) `d1.getName()`
- **Output:** `SpotDogOK`

f) `d1.getName(1)`
- **Output:** `SpotDog`

g) `((Animal)d1).getName()`
- **Output:** `SpotDogOK`

h) `((Dog)a1).getName()`
- **Error:** `Animal` cannot be cast to `Dog`

i) `d1 instanceof Animal`
- **Output:** `true`

j) `a1 instanceof Dog`
- **Output:** `false`

k) `a1.getSound()`
- **Error:** Method `getSound()` is not available in `Animal`

l) `a2.get
Transcribed Image Text:**4. Consider the following classes:** ```java 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() + 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 have been placed in a main class:** ```java 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.** a) `a1.getName()` - **Output:** `Tweety` b) `a2.getName()` - **Output:** `RoverDogOK` c) `a2.getName(0)` - **Error:** Method `getName(int)` is not available in `Animal` d) `(new Animal()).getName()` - **Error:** Constructor `Animal` requires a `String` argument e) `d1.getName()` - **Output:** `SpotDogOK` f) `d1.getName(1)` - **Output:** `SpotDog` g) `((Animal)d1).getName()` - **Output:** `SpotDogOK` h) `((Dog)a1).getName()` - **Error:** `Animal` cannot be cast to `Dog` i) `d1 instanceof Animal` - **Output:** `true` j) `a1 instanceof Dog` - **Output:** `false` k) `a1.getSound()` - **Error:** Method `getSound()` is not available in `Animal` l) `a2.get
Expert Solution
steps

Step by step

Solved in 3 steps with 1 images

Blurred answer
Similar questions
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