分配的抽象类问题

时间:2014-04-27 18:07:11

标签: java methods abstract-class abstract

我试图在下面的课程中执行以下操作...

修改您的超级汽车类以使其抽象化。

修改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。第一次尝试实现抽象类和方法,所以不用说我有点迷失。非常感谢任何和所有帮助。

MAIN:

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


    }
}

1 个答案:

答案 0 :(得分:0)

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