我正在尝试设计和实现一个Java类来表示三维几何形状。该类应包含一个构造函数,适当的数据字段和方法,以返回形状的体积,形状的表面区域以及任何其他似乎对您的形状有意义的方法。
但是,我在Net Beans中可以正常工作,但当我尝试在命令提示符下运行它时,我收到:
error package Cube doesn't exist
error cannot find symbol
这两个错误都是指类多维数据集
我的代码如下。
package cube;
public class Cube {
private double side = 0.0;
public Cube(){//begin constructor
side = 1.0;
}//end constructor
public void setSide (double length) {//begin method
side = length;
}//end method
public double getSide () {//begin method
return side;
}//end method
public double calculateVolume() {
double volume2 = side * side * side;
return volume2;
} // end method
public double calculateSurfaceArea() {
double area = 6 * (side * side);
return area;
} // end method
}//end class
package randygilmanhw4;
import java.util.Scanner;
import cube.Cube;//imports class Cube
public class RandyGilmanHW4 {
public static void main(String[]args) {//begin main
//Display welcome message
System.out.println("Hello Welcome to Randy's Cube");
System.out.println(" Calculator Program");
System.out.println("");
Cube one = new Cube();
//declare variables within main
double area;
double volume2;
double side1;
Scanner input = new Scanner(System.in);
System.out.println("Please enter a length of the side of the cube in cm: ");
side1 = input.nextDouble();
one.setSide(side1);
volume2 = one.calculateVolume();
System.out.printf("Cube's volume is: %4.2f cm^3", volume2);// OUTPUT
System.out.println("\n");
one.setSide(side1);
area = one.calculateSurfaceArea();
System.out.printf("Cube's surface area is: %4.2f cm^2 ", area);// OUTPUT
} // end main
}//end class
答案 0 :(得分:0)
简单 - 当您将其保存到.java文件中时,删除行“package cube;”并保存。它现在应该工作 - “package”用于您的IDE,而不是使用notepad / cmd提示符。将您的文件另存为RandyGilmanHW4.java