Viktor Szabad
Viktor Szabad

Reputation: 37

Inheriting from classes to get outputs.

I have been struggling for weeks with this issue. I cannot get a result for my calculations. I have all the methods within my Calculating class and have the user input within the Main class. Additionally At the end of my Main class I have created an object to inherit all the calculations from the Calculating class to get the results based on the inputs. It does not work. Any suggestions is highly appreciated.

//Calculating Class

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

}

//Master class

    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); 
   }

    }

Upvotes: 0

Views: 49

Answers (1)

gtgaxiola
gtgaxiola

Reputation: 9331

The problem is with your constructor.

public Calculating() { //Constructor (inputs)

    this.totalImpulse  = totalImpulse;
    this.averageImpulse = averageImpulse;
    this.timeEjectionChargeFires =timeEjectionChargeFires;
    this.massEmptyVehicle = massEmptyVehicle;
    this.engineMass = engineMass;
    this.fuelMass = fuelMass;
}

You are never passing any parameters, so how are you initalizing them?

A better constructor could be:

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

Your Master class doesn't have to extend Calculating.

All you have to do within your main is to create a Calculating object initialized with the parameters you are taking from input.

Calculating calculations = new Calculating(totalImpulse,averageImpulse,timeEjectionChargeFires,massEmptyVehicle,engineMass,fuelMass);

Then you can call calculations.theAverageMassOfTheVehicle()

Upvotes: 2

Related Questions