【问题标题】:Abstract Class Issue with Assignment分配的抽象类问题
【发布时间】:2014-04-27 18:07:10
【问题描述】:

我正在尝试对下面的课程执行以下操作...

修改您的超级 Vehicle 类以使其抽象。

修改 Motorized 类,为其提供一个名为“normalizedPower()”的抽象方法,该方法返回一个双精度值。

对于汽车和卡车,normalizedPower 的计算方式会有所不同。 对于汽车,归一化功率是马力除以 150。

对于卡车,标准化功率基于车轮数量。 对于少于 12 个车轮的卡车,标准化功率是马力除以 250。 对于 12 个或更多轮子的卡车,标准化功率是马力除以 400。

代码:

abstract class Vehicle implements Comparable<Vehicle> //changed to abstract
{
    String description;
    String idNumber;
    int numWheels;

    public Vehicle(String aDescription, String aIdNumber, int aNumWheels)
    {
        description = aDescription;
        idNumber = aIdNumber;
        numWheels = aNumWheels;
    }
    void setDescription (String aDescription)
    {
        description = aDescription;
    }
    void setIdNumber (String aIdNumber)
    {
        idNumber = aIdNumber;
    }
    void setNumWheels (int aNumWheels)
    {
        numWheels = aNumWheels;
    }
    public String getDescription()
    {
        return description;
    }
    public String getIdNumber()
    {
        return idNumber;
    }
    public int getNumWheels()
    {
        return numWheels;
    }
    public String toString()
    {
        String result= String.format("ID: %s Description: %s Wheels: %d",idNumber,description,numWheels);
        return result;
    }
    @Override
    public int compareTo(Vehicle other) {

        return description.compareTo(other.getDescription());
    }

}
class humanPowered extends Vehicle
{
    int calories;
    public humanPowered(String aDescription, String aIdNumber, int aNumWheels, int aCalories)
    {
        super(aDescription,aIdNumber,aNumWheels);
        calories = aCalories;
    }
    void setCalories (int aCalories)
    {
        calories = aCalories;
    }
    public int getCalories()
    {
        return calories;
    }
    public String toString()
    {
        String result= String.format("ID: %s Description: %s Wheels: %d Calories per Hour: %d",idNumber,description,numWheels, calories);
        return result;
    }
}
abstract class Motorized extends Vehicle // changed to abstract
{
    double horsepower;
    public Motorized(String aDescription, String aIdNumber, int aNumWheels, double aHorsepower)
    {
        super(aDescription,aIdNumber,aNumWheels);
        horsepower = aHorsepower;
    }
    public double getHorsepower()
    {
        return horsepower;
    }
    abstract double normalizedPower(); //abstract method

    void setHorsepower(double aHorsepower)
    {
        horsepower = aHorsepower;
    }
    public String toString()
    {
        String result= String.format("ID: %s Description: %s Wheels: %d Horsepower: %1.2f",idNumber,description,numWheels, horsepower);
        return result;
    }
}
class Automobile extends Motorized
{
    int passengers;
    double normalizedPower;
    public Automobile(String aDescription, String aIdNumber, int aNumWheels, double aHorsepower, int aPassenger)
    {
        super(aDescription,aIdNumber,aNumWheels, aHorsepower);
        passengers = aPassenger;
    }
    void setPassengers(int aPassengers)
    {
        passengers = aPassengers;
    }
    public int getPassengers()
    {
        return passengers;
    }

    double normalizedPower()
    {
        normalizedPower = horsepower/150;
        return normalizedPower;
    }
    public String toString()
    {
        String result= String.format("ID: %s Description: %s Wheels: %d Horsepower: %1.2f Passengers: %d Normalized Power: %1.2f",idNumber,description,numWheels, horsepower, passengers, normalizedPower);
        return result;
    }
}
class Truck extends Motorized
{
    double GVW;
    double normalizedPower;
    public Truck(String aDescription, String aIdNumber, int aNumWheels, double     aHorsepower, double aGVW)
    {
        super(aDescription,aIdNumber,aNumWheels, aHorsepower);
        GVW = aGVW;
    }
    public double getGVW()
    {
        return GVW;
    }
    void setGVW (int aGVW)
    {
        GVW = aGVW;
    }

    double normalizedPower()
    {
        if (numWheels < 12)
        {
            normalizedPower = horsepower/250;
        }
        else
        {
            normalizedPower = horsepower/400;
        }
        return normalizedPower;
    }
    public String toString()
    {
        String result= String.format("ID: %s Description: %s Wheels: %d Horsepower: %1.2f GVW: %1.2f Normalized Power: %1.2f",idNumber,description,numWheels, horsepower, GVW, normalizedPower);
        return result;
    }
}

输出:

ID: MDB753 Description: A Wheels: 4 Horsepower: 160.00 Passengers: 6 Normalized Power:     0.00
ID: 267533 Description: H Wheels: 2 Calories per Hour: 320
ID: Unicycle Description: H Wheels: 1 Calories per Hour: 370
ID: 267533 Description: H Wheels: 2 Calories per Hour: 320
ID: AHB343 Description: T Wheels: 6 Horsepower: 280.00 GVW: 18.30 Normalized Power: 0.00
ID: BBR 332 Description: T Wheels: 4 Horsepower: 230.00 GVW: 5.34 Normalized Power: 0.00
ID: 993 RFT Description: T Wheels: 18 Horsepower: 424.00 GVW: 78500.00 Normalized Power: 0.00

我无法弄清楚为什么所有归一化功率都打印为 0.00。第一次尝试实现抽象类和方法,不用说我有点迷茫。非常感谢任何和所有帮助。

主要:

public class Hmwk {

    public static void main(String[] args) throws FileNotFoundException {
        ArrayList<Vehicle> list = new ArrayList<Vehicle>();
        Scanner words = new Scanner(new File("input.txt"));
        while (words.hasNextLine())
        {
            String line = words.nextLine();
            String [] tokens;
            tokens = line.split("\t");
            String switchCase = tokens[0];
            char c = switchCase.charAt(0);
            switch (c)
            {
                case 'V':
                    break;
                case 'H':
                    String craftID2 = tokens[1];
                    int wheels2 = Integer.parseInt(tokens[2]);
                    int cals = Integer.parseInt(tokens[3]);
                    Vehicle aVehicle2 = new humanPowered (switchCase, craftID2, wheels2, cals);
                    list.add(aVehicle2);
                    break;
                case 'A':
                    String craftID3 = tokens[1];
                    int wheels3 = Integer.parseInt(tokens[2]);
                    double horses = Double.parseDouble(tokens[3]);
                    int passenger = Integer.parseInt(tokens[4]);
                    Vehicle aVehicle3 = new Automobile (switchCase, craftID3, wheels3, horses, passenger);
                    list.add(aVehicle3);
                    break;
                case 'T':
                    String craftID4 = tokens[1];
                    int wheels4 = Integer.parseInt(tokens[2]);
                    double horses2 = Double.parseDouble(tokens[3]);
                    double gvw = Double.parseDouble(tokens[4]);
                    Vehicle aVehicle4 = new Truck (switchCase, craftID4, wheels4, horses2, gvw);
                    list.add(aVehicle4);
                    break;
                default:
                    throw new IllegalArgumentException ("Invalid data.");


            }
            Collections.sort(list);

        }
        for (int i=0; i<list.size();i++)
        {
            System.out.println(list.get(i).toString());
        }


    }
}

【问题讨论】:

  • 你在调用normalizedPower()吗?
  • 对不起,你的意思是什么?
  • 从您的代码看来,normalizedPower 字段似乎在您调用 normalizedPower() 方法之前并未真正初始化。
  • 你应该分享你的主要方法...
  • 主要已添加。你的意思是我应该设置normalizedPower=0

标签: java methods abstract-class abstract


【解决方案1】:

normalizedPower 不应成为成员以避免数据不一致,并且您会在 toString() 实现中看到对 normalizedPower() 的缺失调用。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-14
    • 2015-09-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-30
    • 1970-01-01
    • 2015-04-06
    相关资源
    最近更新 更多