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.
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,...
Create a new Java file, containing this code public class DataStatsUser { public static void main...
Create a new Java file, containing this code public class DataStatsUser { public static void main (String[] args) { DataStats d = new DataStats(6); d.append(1.1); d.append(2.1); d.append(3.1); System.out.println("final so far is: " + d.mean()); d.append(4.1); d.append(5.1); d.append(6.1); System.out.println("final mean is: " + d.mean()); } } This code depends on a class called DataStats, with the following API: public class DataStats { public DataStats(int N) { } // set up an array (to accept up to N doubles) and other member...
CODE: C# using System; public static class Lab6 { public static void Main() { // declare...
CODE: C# using System; public static class Lab6 { public static void Main() { // declare variables int hrsWrked; double ratePay, taxRate, grossPay, netPay=0; string lastName; // enter the employee's last name Console.Write("Enter the last name of the employee => "); lastName = Console.ReadLine(); // enter (and validate) the number of hours worked (positive number) do { Console.Write("Enter the number of hours worked (> 0) => "); hrsWrked = Convert.ToInt32(Console.ReadLine()); } while (hrsWrked < 0); // enter (and validate) the...
Correct the code: import java.util.Scanner; public class Ch7_PrExercise5 { public static void main(String[] args) {   ...
Correct the code: import java.util.Scanner; public class Ch7_PrExercise5 { public static void main(String[] args) {    Scanner console = new Scanner(System.in);    double radius; double height; System.out.println("This program can calculate "+ "the area of a rectangle, the area "+ "of a circle, or volume of a cylinder."); System.out.println("To run the program enter: "); System.out.println("1: To find the area of rectangle."); System.out.println("2: To find the area of a circle."); System.out.println("3: To find the volume of a cylinder."); System.out.println("-1: To terminate the...
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