Consider the following class definitions. public class Computer { private String memory; public Computer) { memory = "RAM"; } public Computer (String m) { memory = m; } public String getMemory () { return memory; ) } public class Smartphone extends Computer private double screenWidth, screenHeight; public Smart Phone (double w, double h) super("flash"); screenWidth = w; screenHeight = h; } public double get ScreenWidth() { return screenWidth; } public double getScreenHeight() { } return screenHeight; } The following code segment appears in a class other than Computer or Smartphone. + Computer myPhone = new Smart Phone (2.55, 4.53); System.out.println("Device has memory: " + myPhone.getMemory () ", screen area: " + myPhone.getScreenWidth()* myPhone.getScreenHeight() + " square inches."); The code segment is intended to produce the following output. Device has memory: flash, screen area: 11.5515 square inches. Which of the following best explains why the code segment does not work as intended?A. An error occurs during compilation because a Smartphone object cannot be assigned to the Computer reference variable myPhone. B. An error occurs during compilation because the Smartphone class has no getMemory method. C. An error occurs during compilation because the getScreenWidth and getScreenHeight methods are not defined for the Computer object myPhone. D. An error occurs at runtime because the Smartphone class has no getMemory method. E. An error occurs at runtime because the getScreenWidth and getScreenHeight methods are not defined for the Computer object myPhone.

Respuesta :

Answer:

C.

Explanation:

The code provided in the question has many syntax errors. I have pasted a fixed version of the code below that fixes all the syntax errors but leaves the code as is. Using this code you can see that the error that is preventing the code from running properly is that

C. An error occurs during compilation because the getScreenWidth and getScreenHeight methods are not defined for the Computer object myPhone.

As you can see from the attached photo below, the code is failing to compile because the methods themselves are not able to be accessed by the myPhone object. This is happening during the compilation and thus causing the compilation to fail.

class Computer {

   private String memory;

   public Computer() {

       memory = "RAM";

   }

   public Computer(String m) {

       memory = m;

   }

   public String getMemory() {

       return memory;

   }

}

   class Smartphone extends Computer {

       private double screenWidth, screenHeight;

       public Smartphone(double w, double h) {

           super("flash");

           screenWidth = w;

           screenHeight = h;

       }

       public double getScreenWidth() {

           return screenWidth;

       }

       public double getScreenHeight() {

           return screenHeight;

       }

   }

   public class Main{

       //The following code segment appears in a class other than Computer or Smartphone

       

       public static void main(String[] args) {

           Computer myPhone = new Smartphone(2.55, 4.53);

           System.out.println("Device has memory: " + myPhone.getMemory() + ", screen area: " + (myPhone.getScreenWidth()*myPhone.getScreenHeight()) +" square inches.");

       }

   }

Ver imagen sandlee09

The choice is "The getScreenWidth and getScreenHeight methods again for Computer object myPhone aren't declared, resulting in an error during compilation".

Class Description:

  • In Options D and E errors would not occur since only compile-time errors, not run-time errors, would happen.

Computer myPhone = new Smartphone(2.55,4.55);

  • A computer class was referenced by myPhone in this line. As a consequence, it's solely useful for computer students.
  • Therefore, if you try to use the "getScreenwidth" and "getScreenHeight" functions of both the Smartphone class, which would be a sub-class of the Smartphone, you'll get an error.
  • So, because the scope of the myPhone is limited to the parent class, the computer, it complies.
  • Choices A and B will not produce an error since they are inside the scope of the myPhone.
  • As a result, option C is the best argument for why the code section isn't working as it should.

Find out more about the class here:

brainly.com/question/17001900