0

我正在尝试设计和实现一个 Java 类来表示 3-D 几何形状。该类应包含一个构造函数、适当的数据字段和方法来返回形状的体积、形状的表面积以及任何其他似乎对您的形状有意义的方法。

但是,我在 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
4

1 回答 1

0

简单 - 当您将其保存到 .java 文件中时,删除“package cube;”行 并保存。它现在应该可以工作了——“包”用于您的 IDE,而不是记事本/cmd 提示符。将文件另存为 RandyGilmanHW4.java

于 2014-02-22T06:09:38.900 回答