Java 带赋值的抽象类问题
我正在尝试对下面的课程做以下操作 修改超级车辆类,使其抽象化 通过给摩托化类一个名为“normalizedPower()”的抽象方法来修改它,该方法返回一个double 对于轿车和卡车,标准化功率的计算将有所不同。 对于汽车,标准功率是马力除以150 对于卡车,标准化功率基于车轮数量。 对于小于12个车轮的卡车,标准化功率为马力除以250。 对于12个或更多车轮的卡车,标准化功率为马力除以400 代码: 我不明白为什么所有的标准功率都打印为0.00。第一次尝试实现抽象类和方法,不用说我有点迷路了。非常感谢您的任何帮助 主要内容:Java 带赋值的抽象类问题,java,methods,abstract-class,abstract,Java,Methods,Abstract Class,Abstract,我正在尝试对下面的课程做以下操作 修改超级车辆类,使其抽象化 通过给摩托化类一个名为“normalizedPower()”的抽象方法来修改它,该方法返回一个double 对于轿车和卡车,标准化功率的计算将有所不同。 对于汽车,标准功率是马力除以150 对于卡车,标准化功率基于车轮数量。 对于小于12个车轮的卡车,标准化功率为马力除以250。 对于12个或更多车轮的卡车,标准化功率为马力除以400 代码: 我不明白为什么所有的标准功率都打印为0.00。第一次尝试实现抽象类和方法,不用说我有点迷路了
公共类Hmwk{
公共静态void main(字符串[]args)引发FileNotFoundException{
ArrayList=新建ArrayList();
扫描器字=新扫描器(新文件(“input.txt”);
while(words.hasNextLine())
{
字符串行=words.nextLine();
字符串[]标记;
令牌=行分割(“\t”);
字符串switchCase=tokens[0];
char c=开关盒charAt(0);
开关(c)
{
案例“V”:
打破
案例“H”:
字符串craftID2=令牌[1];
int wheels2=Integer.parseInt(令牌[2]);
int cals=Integer.parseInt(标记[3]);
Vehicle aVehicle2=新型人力动力(开关箱、craftID2、车轮2、CAL);
列表。添加(aVehicle2);
打破
案例“A”:
字符串craftID3=令牌[1];
int wheels3=Integer.parseInt(令牌[2]);
double-horses=double.parseDouble(令牌[3]);
int passenger=Integer.parseInt(令牌[4]);
Vehicle aVehicle3=新汽车(开关箱、手工艺3、车轮3、马、乘客);
列表。添加(aVehicle3);
打破
案例“T”:
字符串craftID4=令牌[1];
int wheels4=Integer.parseInt(令牌[2]);
double horses2=double.parseDouble(令牌[3]);
double gvw=double.parseDouble(令牌[4]);
车辆aVehicle4=新卡车(开关箱、工艺ID4、车轮4、马2、gvw);
列表。添加(aVehicle4);
打破
违约:
抛出新的IllegalArgumentException(“无效数据”);
}
集合。排序(列表);
}
for(int i=0;inormalizedPower
不应成为成员以避免数据不一致,并且您将在toString()
实现中看到缺少对normalizedPower()
的调用。您正在调用normalizedPower()
?很抱歉,但是你这么说是什么意思?从你的代码来看,normalizedPower
字段似乎在你调用normalizedPower()
方法之前没有实际初始化。你应该共享你的main方法…main已经添加。你的意思是我应该设置normalizedPower=0
?
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
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());
}
}
}