程序不能用JAVA编译

程序不能用JAVA编译,java,inheritance,polymorphism,Java,Inheritance,Polymorphism,我们被分配了一项任务: 设计船舶、货船和邮轮等级时,要注意每一类的行为。演示具有Ship数组的程序中的类。将各种船舶、巡洋舰和货船分配给数组元素 货船和巡洋舰是船级的产物 请告诉我我的代码有什么问题。我的教授的评论:程序不会编译,因为您没有定义CruiseShip、CargoShip等类,但您正在程序中使用它们 我不明白他所说的程序不编译和类没有定义是什么意思 我非常感谢您的反馈 下面是我的代码: ShipDriver.java CruiseShip.java CargoShip.java 喂?

我们被分配了一项任务:

设计船舶、货船和邮轮等级时,要注意每一类的行为。演示具有Ship数组的程序中的类。将各种船舶、巡洋舰和货船分配给数组元素

货船和巡洋舰是船级的产物

请告诉我我的代码有什么问题。我的教授的评论:程序不会编译,因为您没有定义CruiseShip、CargoShip等类,但您正在程序中使用它们

我不明白他所说的程序不编译和类没有定义是什么意思

我非常感谢您的反馈

下面是我的代码:

ShipDriver.java

CruiseShip.java

CargoShip.java


喂?船的级别在哪里?需要导入它

此处显示的代码非常完美且正在运行。 若所有编译错误都在同一个文件夹中,那个么就不可能出现任何编译错误

我试着编译你的代码,得到了结果。附上了相同的截图


我认为你的代码很好,没有问题

您的代码可能工作不正常,因为打包没有正确完成。如果它们在同一个包或位置,那么就没有问题

 If you put them in the same location then they will work fine and it is guaranteed. At max it might be possible your professor is wrong.

我检查了你的代码,它运行良好,似乎你的教授在课堂上遇到了一些问题,或者他在编译时没有提供所有的课程。定义类意味着创建类,因为您已经创建了名为CruiseShip和Cargoship的类。我不明白他说的不编译是什么意思,因为我相信我的程序正在运行。非常感谢您的评论。我将与我的教授交谈。公共类船舶{String Ship;int yearbuild;公共船舶{this.Ship=Ship;this.yearbuild=yearbuild;}公共船舶{返回船舶\n船舶名称:+Ship+\n建造:+yearbuild;}您需要将其导入另一个.java文件您的意思是我的Ship类不应位于单独的java文件中吗?@EdHeal他对所有类使用默认包,并且所有类都位于同一文件夹中,他不需要导入任何类。
public class Ship{
    String ship;
    int yearBuilt;

    public Ship(String ship, int yearBuilt){
        this.ship = ship;
        this.yearBuilt = yearBuilt;
    }

    public String toString(){
        return "SHIP\nShip Name: " + ship + 
                "\nBuilt: " + yearBuilt;
    }  
}
import java.text.DecimalFormat;

public class CruiseShip extends Ship{
    String category;
    int passengers;
    int cabins;
    int cabinsWithBalconies;
    int swimmingPools;
    int restaurants;

    CruiseShip(String category, String ship, int yearBuilt, int passengers, int cabins, int cabinsWithBalconies, 
            int swimmingPools, int restaurants){
        super(ship, yearBuilt);
        this.category = category;
        this.passengers = passengers;
        this.cabins = cabins;
        this.cabinsWithBalconies = cabinsWithBalconies;
        this.swimmingPools = swimmingPools;
        this.restaurants = restaurants;
    }

    public String toString(){
        DecimalFormat df = new DecimalFormat("#,###");
        return "\nCRUISE SHIP\nCategory: " + category +
                "\nShip Name: " + ship + 
                "\nBuilt: " + yearBuilt +
                "\nPassengers: " + df.format(passengers) + 
                "\nNumber of Cabins: " + cabins +
                "\nNumber of Swimming Pools: " + swimmingPools +
                "\nNumber of Restaurants: " + restaurants;
    }
}
import java.text.DecimalFormat;

public class CargoShip extends Ship{
    int length, beam, maxCapacity;

    CargoShip(String ship, int yearBuilt, int length, int beam, int maxCapacity){
        super(ship, yearBuilt);
        this.length = length;
        this.beam = beam;
        this.maxCapacity = maxCapacity;
    }

    public String toString(){
        DecimalFormat df = new DecimalFormat("#,###");
        return "\nCARGO SHIP\nShip Name: " + ship + 
                "\nBuilt: " + yearBuilt +
                "\nLength Overall (ft): " + df.format(length) +
                "\nBeam (ft): " + beam +
                "\nGross Tonnage: " + df.format(maxCapacity);
    }
}

/** OUTPUT

SHIP LIST

SHIP
Ship Name: Destroyer
Built: 1886

CRUISE SHIP
Category: Small
Ship Name: Arethusa
Built: 2007
Passengers: 50
Number of Cabins: 26
Number of Swimming Pools: 0
Number of Restaurants: 1

CARGO SHIP
Ship Name: Mærsk Mc-Kinney Møller
Built: 2013
Length Overall (ft): 1,306
Beam (ft): 190
Gross Tonnage: 174,500

*/
 If you put them in the same location then they will work fine and it is guaranteed. At max it might be possible your professor is wrong.