Question

In: Computer Science

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 = new Bay(); 
   Lake var5 = new Bay();
    
  // the following lines of code are causing wither syntax or runtime error
    
   // var4.method2();  error 1
 
  // ((Ocean) var5).method1();  error 2

}
}

Answer the following question.

Identify the cause of the error is it run time error or syntax error.

Explain why the error is happening

provide code to fix the problem. Do not change the declaration or the instantiation to fix the issue.

Solutions

Expert Solution


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();
   }
}

public class Driver {
   public static void main(String[] args) {

       Object var4 = new Bay();
       Lake var5 = new Bay();

       // the following lines of code are causing wither syntax or runtime error
      
       // Compile time error because here var4 is reference of class Object so method4 does not have declaration in class Object
       // so we will get compilation error
         
       ((Bay)var4).method2();

       // Here the var5 is pointing to Object of class Bay but we are trying to cast it to Ocean here it is referencing with Lake so there is no relationship so
       // it will give ClassCastException
       ((Bay)var5).method1();
   }
}

NOTE : PLEASE COMMENT BELOW IF YOU HAVE CONCERNS.

I AM HERE TO HELP YOUIF YOU LIKE MY ANSWER PLEASE RATE AND HELP ME IT IS VERY IMP FOR ME


Related Solutions

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"); }...
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...
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...
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...
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?
Task 2/2: Java program Based upon the following code: public class Main {   public static void...
Task 2/2: Java program Based upon the following code: public class Main {   public static void main( String[] args ) {     String alphabet = "ABCDEFGHIJKLMNMLKJIHGFEDCBA";     for( <TODO> ; <TODO> ; <TODO> ) {       <TODO>;     } // Closing for loop   } // Closing main() } // Closing class main() Write an appropriate loop definition and in-loop behavior to determine if the alphabet string is a palindrome or not. A palindrome is defined as a string (or more generally, a token) which...
DESCRIBE WHAT THE FOLLOWING JAVA CODE DOES: public class Main{ public static void main(String[] args) {...
DESCRIBE WHAT THE FOLLOWING JAVA CODE DOES: public class Main{ public static void main(String[] args) { new MyFrame(); } } import javax.swing.*; public class MyFrame extends JFrame{ MyPanel panel; MyFrame(){ panel = new MyPanel(); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.add(panel); this.pack(); this.setLocationRelativeTo(null); this.setVisible(true); } } import java.awt.*; import javax.swing.*; public class MyPanel extends JPanel{ //Image image; MyPanel(){ //image = new ImageIcon("sky.png").getImage(); this.setPreferredSize(new Dimension(500,500)); } public void paint(Graphics g) { Graphics2D g2D = (Graphics2D) g; //g2D.drawImage(image, 0, 0, null); g2D.setPaint(Color.blue); g2D.setStroke(new BasicStroke(5)); g2D.drawLine(0, 0, 500,...
Analyze the following code: public class Test {   public static void main(String[] args) { xMethod(5, 500L);...
Analyze the following code: public class Test {   public static void main(String[] args) { xMethod(5, 500L);   }   public static void xMethod(int n, long l) {     System.out.println("int, long");   }   public static void xMethod(long n, long l) {     System.out.println("long, long");   } } Group of answer choices The program prints 2 lines: int, long long, long The program contains a compile error because Java can't tell which method to invoke. The program prints long, long The program prints int, long
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT