Computer Networking: A Top-Down Approach (7th Edition)
Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN: 9780133594140
Author: James Kurose, Keith Ross
Publisher: PEARSON
Bartleby Related Questions 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
expand button
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
Check Mark
Knowledge Booster
Background pattern image
Similar questions
Recommended textbooks for you
Text book image
Computer Networking: A Top-Down Approach (7th Edi...
Computer Engineering
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:PEARSON
Text book image
Computer Organization and Design MIPS Edition, Fi...
Computer Engineering
ISBN:9780124077263
Author:David A. Patterson, John L. Hennessy
Publisher:Elsevier Science
Text book image
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:9781337569330
Author:Jill West, Tamara Dean, Jean Andrews
Publisher:Cengage Learning
Text book image
Concepts of Database Management
Computer Engineering
ISBN:9781337093422
Author:Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:Cengage Learning
Text book image
Prelude to Programming
Computer Engineering
ISBN:9780133750423
Author:VENIT, Stewart
Publisher:Pearson Education
Text book image
Sc Business Data Communications and Networking, T...
Computer Engineering
ISBN:9781119368830
Author:FITZGERALD
Publisher:WILEY