Java - 如何初始化对象的参数?

时间:2015-03-17 23:36:49

标签: java parameters constructor

我几周来一直在努力解决这个问题但仍然无法得到我需要的东西。我的CalculatingRocketFlightProfile类使用参数(totalImpulse,averageImpulse等...)和计算输出的方法来保存构造函数。我的MAIN类有一个继承我的键盘入口类的对象。这允许用户输入数字。现在,我只需要使用这些输入并计算结果(使用CalculatingRocketFlightProfile类中的方法)来显示。但是我的CalculatingRocketFlightProfile类的对象不会加入任何参数或者我只是做了一些事情。请帮帮我,我真的很沮丧。

//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
}

1 个答案:

答案 0 :(得分:1)

您没有存储kbentry方法返回的信息。试试这个:

System.out.print("\nPlease enter a number for Total Impulse: " );
double totalImpulse = input.totalImpulse1();
System.out.println("You have entered : " + totalImpulse);

对其他变量执行相同操作,然后使用这些变量作为新对象的参数传入。