程序不能在JAVA中编译

时间:2014-02-28 04:50:22

标签: java inheritance polymorphism

我们获得了一项任务:

  

设计Ship,CargoShip和CruiseShip课程,注意每个人的行为。演示具有Ship数组的程序中的类。将各种Ships,CruiseShip和CargoShip分配给数组元素。

CargoShipCruiseShipShip班级的孩子。

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

我不明白他的意思是“程序不编译而且类没有定义”。

我非常感谢您的反馈。

以下是我的代码:

ShipDriver.java

public class ShipDriver{
    public static void main(String[] args){
        Ship[] ships = new Ship[3];
        ships[0]= new Ship("Destroyer",1886);
        ships[1] = new CruiseShip("Small","Arethusa",2007,50,26,18,0,1);
        ships[2] = new CargoShip("Mærsk Mc-Kinney Møller",2013,1306,190,174500);

        System.out.println("SHIP LIST\n");
        for (int i = 0 ; i < ships.length;i++){
            System.out.println(ships[i]);
        }
    }
}

Ship.java

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;
    }  
}

CruiseShip.java

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;
    }
}

CargoShip.java

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

*/

3 个答案:

答案 0 :(得分:0)

嗨?船级在哪里?需要导入它

答案 1 :(得分:0)

此处显示的代码非常完美且正常运行。 如果所有编译错误都在同一个文件夹中,则不会出现任何编译错误。

我尝试编译你的代码,我得到了输出。附上了相同的屏幕截图。

enter image description here

答案 2 :(得分:0)

我认为您的代码很好,并且没有任何问题。

您的代码可能无法正常运行,因为包装未正确完成。如果他们在同一个包裹或位置,那就没有错。

 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.