我一直在为这个问题苦苦挣扎数周。我无法得到计算结果。我有我的Calculating类中的所有方法,并在Main类中有用户输入。另外,在我的Main类的末尾,我创建了一个对象,用于继承Calculating类中的所有计算,以根据输入获得结果。这是行不通的。任何建议都非常感谢。
//计算班级
public class Calculating { //Calculation class
//Declaring fields - Inputs
public double totalImpulse ;
public double averageImpulse;
public double timeEjectionChargeFires;
public double massEmptyVehicle;
public double engineMass;
public double fuelMass;
//Declaring variables for outputs
public double theAverageMassOfTheVehicle;
public Calculating() { //Constructor (inputs)
this.totalImpulse = totalImpulse;
this.averageImpulse = averageImpulse;
this.timeEjectionChargeFires =timeEjectionChargeFires;
this.massEmptyVehicle = massEmptyVehicle;
this.engineMass = engineMass;
this.fuelMass = fuelMass;
}
//Accessors and Mutators
//Methods used to calculate Average mass of the vehicle
public double theAverageMassOfTheVehicle() {
return massEmptyVehicle + ((engineMass + (engineMass - fuelMass) )/ 2); //Formula to calculate Average mass
}
//Setters
public void setTheAverageMassOfTheVehicle(double theAverageMassOfTheVehicle) {
this.theAverageMassOfTheVehicle = theAverageMassOfTheVehicle;
}//method
//Getters
public double getTheAverageMassOfTheVehicle() {
return theAverageMassOfTheVehicle;
}
}
//大师班
public class Master extends Calculating{ //Master class
public static void main( String args[] ) //Standard header for main method
{
UserEntry input = new UserEntry(); //Creating object from UserEntry class
//User entry for Total Impulse with exception handling
Double totalImpulse = null;
while(totalImpulse==null){
System.out.print("\nPlease enter Total impulse delivered: "); //System print line for input
try {
totalImpulse= input.gettotalImpulse(); //Instantiates totalImpulse from UserEntry scanner
}
catch(Exception e){ //There was something wrong with the input.
System.out.println("\nInvalid entry, please enter a number !"); //Error message upon wrong input
}
}
//User entry for Average Impulse with exception handling
Double averageImpulse = null;
while(averageImpulse==null){
System.out.print("Please enter Average Impulse delivered: "); //System print line for input
try {
averageImpulse= input.getaverageImpulse(); //Instantiates averageImpulse from UserEntry scanner
}
catch(Exception e){ //There was something wrong with the input.
System.out.println("\nInvalid entry, please enter a number !"); //Error message upon wrong input
}
}
//User entry for Time Ejection charge fires with exception handling
Double timeEjectionChargeFires = null;
while(timeEjectionChargeFires==null){
System.out.print("Please enter Time ejection charge fires: "); //System print line for input
try {
timeEjectionChargeFires= input.gettimeEjectionChargeFires(); //Instantiates timeEjectionChargeFires from UserEntry scanner
}
catch(Exception e){ //There was something wrong with the input.
System.out.println("\nInvalid entry, please enter a number !"); //Error message upon wrong input
}
}
//User entry for Mass of the empty vehicle with exception handling
Double massEmptyVehicle = null;
while(massEmptyVehicle==null){
System.out.print("Please enter The mass of the empty vehicle: "); //System print line for input
try {
massEmptyVehicle= input.getmassEmptyVehicle(); //Instantiates massEmptyVehicle from UserEntry scanner
}
catch(Exception e){ //There was something wrong with the input.
System.out.println("\nInvalid entry, please enter a number !"); //Error message upon wrong input
}
}
//User entry for Mass of the engine with exception handling
Double engineMass = null;
while(engineMass==null){
System.out.print("Please enter The mass of the engine: "); //System print line for input
try {
engineMass= input.getengineMass(); //Instantiates engineMass from UserEntry scanner
}
catch(Exception e){ //There was something wrong with the input.
System.out.println("\nInvalid entry, please enter a number !"); //Error message upon wrong input
}
}
//User entry for Fuel mass with exception handling
Double fuelMass = null;
while(fuelMass==null){
System.out.print("Please enter The mass of the fuel: "); //System print line for input
try {
fuelMass= input.getfuelMass(); //Instantiates fuelMass from UserEntry scanner
}
catch(Exception e){ //There was something wrong with the input.
System.out.println("\nInvalid entry, please enter a number !"); //Error message upon wrong input
}
}
//Outputs based on user inputs
Calculating Master = new Calculating(); //Creates object of Calculating class
System.out.println("\nThe average mass of the vehicle: " +Master.theAverageMassOfTheVehicle() /1000);
}
}
答案 0 :(得分:2)
问题在于你的构造函数。
public Calculating() { //Constructor (inputs)
this.totalImpulse = totalImpulse;
this.averageImpulse = averageImpulse;
this.timeEjectionChargeFires =timeEjectionChargeFires;
this.massEmptyVehicle = massEmptyVehicle;
this.engineMass = engineMass;
this.fuelMass = fuelMass;
}
你永远不会传递任何参数,那你怎么把它们搞砸呢?
更好的构造函数可能是:
public Calculating(double totalImpulse, double averageImpulse,
double timeEjectionChargeFires, double massEmptyVehicle,
double engineMass, double fuelMass) {
this.totalImpulse = totalImpulse;
this.averageImpulse = averageImpulse;
this.timeEjectionChargeFires = timeEjectionChargeFires;
this.massEmptyVehicle = massEmptyVehicle;
this.engineMass = engineMass;
this.fuelMass = fuelMass;
}
您的Master
课程不必延长Calculating
。
您在main
中所要做的就是创建一个Calculating
对象,使用您从输入中获取的参数进行初始化。
Calculating calculations = new Calculating(totalImpulse,averageImpulse,timeEjectionChargeFires,massEmptyVehicle,engineMass,fuelMass);
然后你可以拨打calculations.theAverageMassOfTheVehicle()