我遇到问题的第一段代码是 DemoSquare - 当我运行它时它会崩溃。错误是:
Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - cannot find symbol
symbol: variable area
location: class Square
at Square.computeSurfaceArea(DemoSquare.java:57)
at DemoSquare.main(DemoSquare.java:23)
Java Result: 1
我的代码是 -
// package demosquare;
import java.util.*;
public class DemoSquare {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Please enter the height of the square/rectangle");
double sqHeight = input.nextDouble();
System.out.println("Please enter the width of the square/rectangle");
double sqWidth = input.nextDouble();
System.out.println("Please enter the height of the Cube/Rectangular Prism");
double cuHeight = input.nextDouble();
System.out.println("Please enter the width of the Cube/Rectangular Prism");
double cuWidth = input.nextDouble();
System.out.println("Please enter the depth of the Cube/Rectangular Prism");
double cuDepth = input.nextDouble();
// Create a square and print out the information about it
Square square = new Square(sqHeight, sqWidth);
System.out.println("A Square with dimensions " + square.getHeight()
+ " by " + square.getWidth() + " has a surface area of "
+ square.computeSurfaceArea());
// Create a cube and print out the information about it.
Cube cube = new Cube(cuHeight, cuWidth, cuDepth);
System.out.println("A Cube with dimensions " + cube.getHeight()
+ " by " + cube.getWidth() + " by " + cube.getDepth() + " has a surface area of "
+ cube.computeSurfaceArea());
} // end main method
} //end class DemoSquare
class Square {
// enter the code to create the square class here
double sqHeight = 0;
double sqWidth = 0;
public Square(double height, double width) {
sqHeight = height;
sqWidth = width;
}
public double getWidth() {
return sqWidth;
}
public double getHeight() {
return sqHeight;
}
public double computeSurfaceArea() {
double surfaceArea = sqHeight * sqWidth;
surfaceArea = (getHeight() * getWidth());
return area;
}
}
class Cube extends Square {
double sqDepth = 0.00;
// enter the cube class code here
public Cube(double height, double width, double depth) {
super(height, width);
sqDepth = depth;
}
@Override
public double getWidth() {
return sqWidth;
}
@Override
public double getHeight() {
return sqHeight;
}
public double getDepth() {
return sqDepth;
}
@Override
public double computeSurfaceArea() {
//Surface Area = 2hw + 2wd + 2dh
double tsa = (2 * sqHeight * sqWidth) + (2 * sqWidth * sqDepth) + (2 * sqDepth * sqHeight);
return tsa;
}
}
我在这段代码中做错了什么?