Scott Allen
Scott Allen

Reputation: 35

Java - How to initialize parameters for an object?

I have been struggling with this issue for weeks and still cannot get what I need. My CalculatingRocketFlightProfile class holds the constructor with the parameters (totalImpulse, averageImpulse etc...) and the methods which calculates the outputs. My MAIN class has an object which inherits my keyboard entry class. This allows users to input a number. Now, all I need is to use these inputs and calculate a result (using the methods in the CalculatingRocketFlightProfile class) to be displayed. However my object of CalculatingRocketFlightProfile class wont acceppt any parameters or I simply doing something wronng. Please help me out I'm really frustrated.

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

Upvotes: 0

Views: 3073

Answers (1)

Tobi
Tobi

Reputation: 86

You are not storing the returned information from your kbentry methods. Try this:

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

Do the same for the other variables, then use those to pass in as arguments for your new object.

Upvotes: 1

Related Questions