Java - 如何初始化一个对象的参数?
Java - How to initialize parameters for an object?
我已经为这个问题苦苦挣扎了几个星期,但仍然无法得到我需要的东西。我的 CalculatingRocketFlightProfile class 包含带有参数 (totalImpulse、averageImpulse 等...)的 构造函数和计算输出的方法。我的 MAIN class 有一个对象继承了我的键盘输入 class。这允许用户输入一个数字。现在,我只需要使用这些输入并计算出要显示的结果(使用 CalculatingRocketFlightProfile class 中的方法)。然而,我的 CalculatingRocketFlightProfile 对象 class 不会接受任何参数 或者我只是做了一些错误的事情。请帮帮我,我真的很沮丧。
//CalculatingRocketFlightProfile class
public class CalculatingRocketFlightProfile { //Calculation class
//Declaring fields
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; //declare variables to store results of calculations
public double theVehiclesMaximumVelocity;
public CalculatingRocketFlightProfile(double totalImpulse, double averageImpulse, double timeEjectionChargeFires, double massEmptyVehicle,
double engineMass, double fuelMass) { //Setting the parameters
this.totalImpulse = totalImpulse;
this.averageImpulse = averageImpulse;
this.timeEjectionChargeFires = timeEjectionChargeFires;
this.massEmptyVehicle = massEmptyVehicle;
this.engineMass = engineMass;
this.fuelMass = fuelMass;
}
//Mutators and Accessors
//Accessors
//Methods for calculations - Calculating outputs, using inputs.
public double theAverageMassOfTheVehicle() {
return massEmptyVehicle + ((engineMass + (engineMass - fuelMass) )/ 2); //Formula to calculate Average mass
}//method
public double theVehiclesMaximumVelocity() { //Formula to calculate Maximum velocity
return totalImpulse / getTheAverageMassOfTheVehicle();
}//method
//Mutators - SET
public void setTheAverageMassOfTheVehicle(double theAverageMassOfTheVehicle) {
this.theAverageMassOfTheVehicle = theAverageMassOfTheVehicle;
}//method
public void setTheVehiclesMaximumVelocity(double theVehiclesMaximumVelocity) {
this.theVehiclesMaximumVelocity = theVehiclesMaximumVelocity;
}//method
//Getters
public double getTheAverageMassOfTheVehicle() {
return theAverageMassOfTheVehicle;
}//method
public double getTheVehiclesMaximumVelocity() {
return theVehiclesMaximumVelocity;
}//method
}//class
public class Main { //Master class
public static void main( String args[] ) //Standard header for main method
{
kbentry input = new kbentry();
System.out.print("\nPlease enter a number for Total Impulse: " );
System.out.println("You have entered : " +input.totalImpulse1());
System.out.print("\nPlease enter a number for Average Impulse: " );
System.out.println("You have entered : " +input.averageImpulse2());
System.out.print("\nPlease enter a number for Time ejection charge fires: " );
System.out.println("You have entered : " +input.timeEjectionChargeFires3());
System.out.print("\nPlease enter a number for the Mass of the vehicle: " );
System.out.println("You have entered : " +input.massEmptyVehicle4());
System.out.print("\nPlease enter a number for the Mass of the engine: " );
System.out.println("You have entered : " +input.engineMass5());
System.out.print("\nPlease enter a number for the Mass of the fuel: " );
System.out.println("You have entered : " +input.fuelMass6());
//Output
CalculatingRocketFlightProfile calculations = new CalculatingRocketFlightProfile(totalImpulse,averageImpulse,timeEjectionChargeFires,massEmptyVehicle,engineMass,fuelMass ); //This will give me an error "cant find variables"
System.out.println("\nThe average mass of the vehicle: " +calculations.theAverageMassOfTheVehicle() +
"\nThe vehicles maximum velocity: " + calculations.theVehiclesMaximumVelocity());
}
}
//kbentry class (Same for all methods e.g. averageImpulse2, timeEjectionChargeFires3 etc...)
public class kbentry{
double totalImpulse1(){
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
//Total Impulse entry
String strTotalImpulse = null; // These must be initialised
int intTotalImpulse = 0;
//System.out.print("Please enter a number for Total Impulse: ");
//System.out.flush();
// read string value from keyboard
try {
strTotalImpulse = in.readLine();
}
catch (IOException ioe) {
// ignore exception
}
您没有存储从您的 kbentry 方法返回的信息。试试这个:
System.out.print("\nPlease enter a number for Total Impulse: " );
double totalImpulse = input.totalImpulse1();
System.out.println("You have entered : " + totalImpulse);
对其他变量执行相同的操作,然后使用它们作为新对象的参数传入。
我已经为这个问题苦苦挣扎了几个星期,但仍然无法得到我需要的东西。我的 CalculatingRocketFlightProfile class 包含带有参数 (totalImpulse、averageImpulse 等...)的 构造函数和计算输出的方法。我的 MAIN class 有一个对象继承了我的键盘输入 class。这允许用户输入一个数字。现在,我只需要使用这些输入并计算出要显示的结果(使用 CalculatingRocketFlightProfile class 中的方法)。然而,我的 CalculatingRocketFlightProfile 对象 class 不会接受任何参数 或者我只是做了一些错误的事情。请帮帮我,我真的很沮丧。
//CalculatingRocketFlightProfile class
public class CalculatingRocketFlightProfile { //Calculation class
//Declaring fields
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; //declare variables to store results of calculations
public double theVehiclesMaximumVelocity;
public CalculatingRocketFlightProfile(double totalImpulse, double averageImpulse, double timeEjectionChargeFires, double massEmptyVehicle,
double engineMass, double fuelMass) { //Setting the parameters
this.totalImpulse = totalImpulse;
this.averageImpulse = averageImpulse;
this.timeEjectionChargeFires = timeEjectionChargeFires;
this.massEmptyVehicle = massEmptyVehicle;
this.engineMass = engineMass;
this.fuelMass = fuelMass;
}
//Mutators and Accessors
//Accessors
//Methods for calculations - Calculating outputs, using inputs.
public double theAverageMassOfTheVehicle() {
return massEmptyVehicle + ((engineMass + (engineMass - fuelMass) )/ 2); //Formula to calculate Average mass
}//method
public double theVehiclesMaximumVelocity() { //Formula to calculate Maximum velocity
return totalImpulse / getTheAverageMassOfTheVehicle();
}//method
//Mutators - SET
public void setTheAverageMassOfTheVehicle(double theAverageMassOfTheVehicle) {
this.theAverageMassOfTheVehicle = theAverageMassOfTheVehicle;
}//method
public void setTheVehiclesMaximumVelocity(double theVehiclesMaximumVelocity) {
this.theVehiclesMaximumVelocity = theVehiclesMaximumVelocity;
}//method
//Getters
public double getTheAverageMassOfTheVehicle() {
return theAverageMassOfTheVehicle;
}//method
public double getTheVehiclesMaximumVelocity() {
return theVehiclesMaximumVelocity;
}//method
}//class
public class Main { //Master class
public static void main( String args[] ) //Standard header for main method
{
kbentry input = new kbentry();
System.out.print("\nPlease enter a number for Total Impulse: " );
System.out.println("You have entered : " +input.totalImpulse1());
System.out.print("\nPlease enter a number for Average Impulse: " );
System.out.println("You have entered : " +input.averageImpulse2());
System.out.print("\nPlease enter a number for Time ejection charge fires: " );
System.out.println("You have entered : " +input.timeEjectionChargeFires3());
System.out.print("\nPlease enter a number for the Mass of the vehicle: " );
System.out.println("You have entered : " +input.massEmptyVehicle4());
System.out.print("\nPlease enter a number for the Mass of the engine: " );
System.out.println("You have entered : " +input.engineMass5());
System.out.print("\nPlease enter a number for the Mass of the fuel: " );
System.out.println("You have entered : " +input.fuelMass6());
//Output
CalculatingRocketFlightProfile calculations = new CalculatingRocketFlightProfile(totalImpulse,averageImpulse,timeEjectionChargeFires,massEmptyVehicle,engineMass,fuelMass ); //This will give me an error "cant find variables"
System.out.println("\nThe average mass of the vehicle: " +calculations.theAverageMassOfTheVehicle() +
"\nThe vehicles maximum velocity: " + calculations.theVehiclesMaximumVelocity());
}
}
//kbentry class (Same for all methods e.g. averageImpulse2, timeEjectionChargeFires3 etc...)
public class kbentry{
double totalImpulse1(){
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
//Total Impulse entry
String strTotalImpulse = null; // These must be initialised
int intTotalImpulse = 0;
//System.out.print("Please enter a number for Total Impulse: ");
//System.out.flush();
// read string value from keyboard
try {
strTotalImpulse = in.readLine();
}
catch (IOException ioe) {
// ignore exception
}
您没有存储从您的 kbentry 方法返回的信息。试试这个:
System.out.print("\nPlease enter a number for Total Impulse: " );
double totalImpulse = input.totalImpulse1();
System.out.println("You have entered : " + totalImpulse);
对其他变量执行相同的操作,然后使用它们作为新对象的参数传入。