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...
import java.util.Scanner; import java.text.value; public class Rectangle { public static void main(String[]args){ System.out.println("Find the area of...
import java.util.Scanner; import java.text.value; public class Rectangle { public static void main(String[]args){ System.out.println("Find the area of a rectangle"); System.out.println("Find the perimeter of a rectangle"); System.out.println("Enter a number of choice"); } public static void main(String[]args){ int choice = //the user choice double value :0 // the value returned from the method double length; // the length of rectangle double width;// the width of rectangle Scanner scan = new Scanner(System.in); System.out.println("Enter length: "); double length = scan.nextDouble(); //the length of rectangle System.out.println("Enter...
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...
Consider a class ‘A’ with a method public static void show( ). A class ‘Derived’ is...
Consider a class ‘A’ with a method public static void show( ). A class ‘Derived’ is the subclass of ‘A’. Is it possible to define the same method public static void show( ) in the Derived class. Why give reasons? Is it run time polymorphism? (0.5 Marks) Is it possible to define the method public void show( ) (non static method) in the Derived class. What will happen and why give reasons?
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 code: import java.util.Scanner; public class Main {    public static void main(String[] args)...
Consider the following code: import java.util.Scanner; public class Main {    public static void main(String[] args) {    Scanner in = new Scanner(System.in); String input = ""; System.out.println("Enter the first number: ");    input = in.nextLine(); int a = 0; int b = 0;    a = Integer.parseInt(input);    System.out.println("Enter the second number: "); input = in.nextLine();    b = Integer.parseInt(input);    int result = 0; result = a/b;    System.out.println("a/b : " + result);    } Copy the code...
QUESTION 5 Consider the following Die class: class Die { public:    void setValue(int x);// assign...
QUESTION 5 Consider the following Die class: class Die { public:    void setValue(int x);// assign x to num int getValue(); // return num void roll(); // set num with a random number ranging 1-6 Die();   // constructor private: int num; }; Which of the following C++ code segment will correctly find that how many times a Die object will be rolled until the value '6' is obtained. Die myDie; int dieValue, countTil6 = 0; myDie.roll(); dieValue = myDie.getValue(); while...
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.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT