Question

In: Computer Science

Consider the following classes: public class Clock extends Bear { public void method3() { System.out.println("Clock 3");...

Consider the following classes:

public class Clock extends Bear {
  public void method3()      {
    System.out.println("Clock 3");
  }
}

public class Lamp extends Can    {
  public void method1()      {
    System.out.println("Lamp 1");
  }

  public void method3()      {
    System.out.println("Lamp 3");
  }
}

public class Bear extends Can    {
  public void method1()      {
   System.out.println("Bear 1");
  }
  
  public void method3()      {
    System.out.println("Bear 3");
    super.method3();
  }
}

public class Can {
  public void method2()      {
    System.out.println("Can 2");
    method3();
  }

  public void method3()      {
    System.out.println("Can 3");
  }
}

---and that the following variables are defined:---

Object var1 = new Bear();
Can var2 = new Can();
Can var3 = new Lamp();
Bear var4 = new Clock();
Object var5 = new Can();
Can var6 = new Clock();

For each statement below , match the output produced in the right-hand column by the statement in the left-hand column. If the statement produces more than one line of output, the line breaks are indicated with slashes as in "a/b/c" to indicate three lines of output with "a" followed by "b" followed by "c". If the statement causes an error, choose in the right-hand column with either the phrase "compiler error" or "runtime error" to indicate when the error would be detected.

Note: Multiple statements can have the same output (ex: Compiler Error or Runtime Error)

var1.method2();

var2.method2();

var3.method2();

var4.method2();

var5.method2();

var1.method3();

var2.method3();

var3.method3();

var6.method3();

((Lamp)var6).method1();

((Can)var1).method1();

((Can)var1).method2();

((Bear)var1).method3();

((Clock)var1).method1();

---The choices for all methods are:---

Clock 3

Can 2/Can 3

Runtime Error

Compiler Error

Can 2/Bear 2/Can 3

Can 2/Clocl 3

Can 3

Bear3/Can 3

Solutions

Expert Solution

- Below is the code with the output.
- We need to add some cast to correct the compile time error
- There is a runtime error. I have added comments for the same
- Runtime Exception at line 30 of below image due to wrong reference as there is no method1 for clock class
- Compile time error at 3 places. added comments

Kindly upvote if this helped





CODE:

- Created a main class for testing the results.
- Output is in above image

package search;

public class DummyTest {
    public static void main(String[] args) {
        Object var1 = new Bear();
        Can var2 = new Can();
        Can var3 = new Lamp();
        Bear var4 = new Clock();
        Object var5 = new Can();
        Can var6 = new Clock();
        ((Can) var1).method2(); // compile time error - Add a cast here


        var2.method2();

        var3.method2();

        var4.method2();

        ((Can) var5).method2(); // compile time error - Add a cast here

        ((Bear) var1).method3(); // compile time error - Add a cast here

        var2.method3();

        var3.method3();

        var6.method3();

        ((Lamp) var6).method1(); // runtime error here due to wrong cast as method1 is in Lamp class . There is no method1 in clock class. as object is of class clock , so runtime error

        ((Bear) var1).method1(); // compile time error - change the cast here

        ((Can) var1).method2();

        ((Bear) var1).method3();

        ((Clock) var1).method1();
    }
}
class Clock extends Bear {
    public void method3() {
        System.out.println("Clock 3");
    }
}

class Lamp extends Can {
    public void method1() {
        System.out.println("Lamp 1");
    }

    public void method3() {
        System.out.println("Lamp 3");
    }
}

class Bear extends Can {
    public void method1() {
        System.out.println("Bear 1");
    }

    public void method3() {
        System.out.println("Bear 3");
        super.method3();
    }
}

class Can {
    public void method2() {
        System.out.println("Can 2");
        method3();
    }

    public void method3() {
        System.out.println("Can 3");
    }
}

Related Solutions

Consider the following code: public class Bay extends Lake { public void method1() { System.out.println("Bay 1");...
Consider the following code: public class Bay extends Lake { public void method1() { System.out.println("Bay 1"); super.method2(); } public void method2() { System.out.println("Bay 2"); } } //********************************* class Pond { public void method2() { System.out.println("Pond 2"); } } //************************** class Ocean extends Bay { public void method2() { System.out.println("Ocean 2"); } } //********************************* class Lake extends Pond { public void method3() { System.out.println("Lake 3"); method2(); } } //**************************** class Driver { public static void main(String[] args) { Object var4 =...
public class Mammal extends SeaCreature { public void method1() { System.out.println("warm-blooded"); } } public class SeaCreature...
public class Mammal extends SeaCreature { public void method1() { System.out.println("warm-blooded"); } } public class SeaCreature { public void method1() { System.out.println("creature 1"); } public void method2() { System.out.println("creature 2"); } public String toString() { return "ocean-dwelling"; } } public class Whale extends Mammal { public void method1() { System.out.println("spout"); } public String toString() { return "BIG!"; } } public class Squid extends SeaCreature { public void method2() { System.out.println("tentacles"); } public String toString() { return "squid"; } } What...
How can the classes be modified to satisfy the comments: public class InvalidIntegerException extends Exception{    ...
How can the classes be modified to satisfy the comments: public class InvalidIntegerException extends Exception{     // Write a class for an InvalidIntegerException here     //Constructor that takes no arguments public InvalidIntegerException (){ super(); } //Constructor that takes a string message public InvalidIntegerException (String message){ super(message); } } import java.io.InputStream; import java.io.IOException; public class Parser { private InputStream in; public static final int CHAR_ZERO = (int) '0'; public Parser (InputStream in) { this.in = in; } // Complete the following...
1. Consider the following code: public class Widget implements Serializable { private int x; public void...
1. Consider the following code: public class Widget implements Serializable { private int x; public void setX( int d ) { x = d; } public int getX() { return x; } writeObject( Object o ) { o.writeInt(x); } } Which of the following statements is true? I. The Widget class is not serializable because no constructor is defined. II. The Widget class is not serializable because the implementation of writeObject() is not needed. III. The code will not compile...
Consider the following code: public class Example { public static void doOp(Op op) { boolean result...
Consider the following code: public class Example { public static void doOp(Op op) { boolean result = op.operation(true, false); System.out.println(result); } public static void main(String[] args) { doOp(new AndOperation()); doOp(new OrOperation()); } } main's output: false true Define any interfaces and/or classes necessary to make this output happen. Multiple answers are possible. You may not modify any of the code in Example.
Consider the following interface: public interface Car{ public String getMake(); public void setMake(); public void honk();...
Consider the following interface: public interface Car{ public String getMake(); public void setMake(); public void honk(); public void crash(); public void drive(); } public interface Boat{ public String getMake (); public void setMake (); public void blast_horn(); public void sink(); public void move(); } 1. Create a concrete FamilyCar class from the Car interface.
If the class Parrot extends the class Bird, what can't be said about the classes? Select...
If the class Parrot extends the class Bird, what can't be said about the classes? Select one answer: A. The Parrot class has access to the Parrot methods B. The Parrot class has access to the Bird methods C. The Bird class has access to the Bird methods D. The Bird class has access to the Parrot methods
Consider this program: public class Main { public static void main(String[] args) { String s1 =...
Consider this program: public class Main { public static void main(String[] args) { String s1 = "hello"; String s2 = "hello"; String s3 = new String("hello"); System.out.println(s1 == s2); System.out.println(s2 == s3); System.out.println(s2.equals(s3)); } } When we run the program, the output is: true false true Explain why this is the output, using words and/or pictures.
Study the following class definition: class Car { public: Car(double speed); void start(); void accelerate(double speed);...
Study the following class definition: class Car { public: Car(double speed); void start(); void accelerate(double speed); void stop(); double get_speed() const; private: double speed; }; Which of the following options would make logical sense in the definition of the void accelerate(double speed)function? Group of answer choices this->speed = this->speed; this->speed = speed; this.speed = speed; speed1 = this->speed; Flag this Question Question 131 pts The Point class has a public function called display_point(). What is the correct way of calling...
write the following programs: Ram.java, Truck.java, LandVehicle.java and Vehicle.java. public class Ram extends Truck { public...
write the following programs: Ram.java, Truck.java, LandVehicle.java and Vehicle.java. public class Ram extends Truck { public static void main(String[] args) { // your implementation } public Ram() { // your implementation } } public class Truck extends LandVehicle { public Truck() { // your implementation } public Truck(String s) { // your implementation } } public class LandVehicle extends Vehicle { public LandVehicle() { // your implementation } } public class Vehicle { public Vehicle() { // your implementation
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT