public class LunarLander { double currentFuelFlowRate; d.pdf
1. public class LunarLander
{
double currentFuelFlowRate;
double verticalSpeed;
double altitude;
double amountOfFuel;
double massOfTheLander;
double maxFuelConsumptionRate;
double maxThrust;
/* constructor to initialize member variales*/
public LunarLander(double al, double fuel, double mass, double rate, double thrust)
{
currentFuelFlowRate=0.0;
verticalSpeed=0.0;
altitude=al;
amountOfFuel=fuel;
massOfTheLander=mass;
maxFuelConsumptionRate=rate;
maxThrust=thrust;
}
/* functions to get the values of instance variables*/
public double getCurrentFuelFlowRate()
{
return currentFuelFlowRate;
}
public double getVerticalSpeed()
{
return verticalSpeed;
}
public double getAltitude()
{
return altitude;
}
public double getAmountOfFuel()
2. {
return amountOfFuel;
}
public double getMassOfTheLander()
{
return massOfTheLander;
}
public double getMaxFuelConsumptionRate()
{
return maxFuelConsumptionRate;
}
public double getMaxThrust()
{
return maxThrust;
}
/* updates the value of currentFuelFlowRate*/
public void setCurrentFuelFlowRate(double rate)
{
currentFuelFlowRate=rate;
}
/* simulating the passage for a small amount of time t*/
public void setPassage(double t)
{
if(currentFuelFlowRate>0)
{
if(amountOfFuel==0)
{
currentFuelFlowRate=0;
}
}
/* velocity is the verticalSpeed*/
double f=maxThrust*currentFuelFlowRate;
double m=massOfTheLander;
verticalSpeed=t*((f/m)-1.62);
//v is verticalSpeed
altitude=t*verticalSpeed;
3. //ship is landed
if(altitude<0)
{
altitude=0;
verticalSpeed=0;
}
//r is currentFuelFlowRate
//c is maxFuelConsumptionRate
double changeInRemainFuel=t*currentFuelFlowRate*maxFuelConsumptionRate;
amountOfFuel=amountOfFuel-changeInRemainFuel;
if(amountOfFuel<0)
{
amountOfFuel=0;
}
}
}
////////////////////////////////////////////////////////
public class LunarLanderDemo {
public static void main(String[] args) {
LunarLander L=new LunarLander(1000,1700,900,10,5000);
/*give values between 0.0 , 1.0*/
L.setCurrentFuelFlowRate(0.6);
L.setPassage(0.1);
System.out.println("Simulation of Lunar Lander Passage at 0.1 seconds");
System.out.println("Current Fuel Flow Rate: "+L.getCurrentFuelFlowRate());
System.out.println("Vertical Speed: "+L.getVerticalSpeed());
System.out.println("Altitude: "+L.getAltitude());
System.out.println("Amount of Fuel: "+L.getAmountOfFuel());
L.setCurrentFuelFlowRate(0.3);
L.setPassage(0.09);
System.out.println("Simulation of Lunar Lander Passage at 0.09 seconds");
System.out.println("Current Fuel Flow Rate: "+L.getCurrentFuelFlowRate());
System.out.println("Vertical Speed: "+L.getVerticalSpeed());
System.out.println("Altitude: "+L.getAltitude());
4. System.out.println("Amount of Fuel: "+L.getAmountOfFuel());
}
}
Solution
public class LunarLander
{
double currentFuelFlowRate;
double verticalSpeed;
double altitude;
double amountOfFuel;
double massOfTheLander;
double maxFuelConsumptionRate;
double maxThrust;
/* constructor to initialize member variales*/
public LunarLander(double al, double fuel, double mass, double rate, double thrust)
{
currentFuelFlowRate=0.0;
verticalSpeed=0.0;
altitude=al;
amountOfFuel=fuel;
massOfTheLander=mass;
maxFuelConsumptionRate=rate;
maxThrust=thrust;
}
/* functions to get the values of instance variables*/
public double getCurrentFuelFlowRate()
{
return currentFuelFlowRate;
}
public double getVerticalSpeed()
{
return verticalSpeed;
5. }
public double getAltitude()
{
return altitude;
}
public double getAmountOfFuel()
{
return amountOfFuel;
}
public double getMassOfTheLander()
{
return massOfTheLander;
}
public double getMaxFuelConsumptionRate()
{
return maxFuelConsumptionRate;
}
public double getMaxThrust()
{
return maxThrust;
}
/* updates the value of currentFuelFlowRate*/
public void setCurrentFuelFlowRate(double rate)
{
currentFuelFlowRate=rate;
}
/* simulating the passage for a small amount of time t*/
public void setPassage(double t)
{
if(currentFuelFlowRate>0)
{
if(amountOfFuel==0)
{
currentFuelFlowRate=0;
}
}
6. /* velocity is the verticalSpeed*/
double f=maxThrust*currentFuelFlowRate;
double m=massOfTheLander;
verticalSpeed=t*((f/m)-1.62);
//v is verticalSpeed
altitude=t*verticalSpeed;
//ship is landed
if(altitude<0)
{
altitude=0;
verticalSpeed=0;
}
//r is currentFuelFlowRate
//c is maxFuelConsumptionRate
double changeInRemainFuel=t*currentFuelFlowRate*maxFuelConsumptionRate;
amountOfFuel=amountOfFuel-changeInRemainFuel;
if(amountOfFuel<0)
{
amountOfFuel=0;
}
}
}
////////////////////////////////////////////////////////
public class LunarLanderDemo {
public static void main(String[] args) {
LunarLander L=new LunarLander(1000,1700,900,10,5000);
/*give values between 0.0 , 1.0*/
L.setCurrentFuelFlowRate(0.6);
L.setPassage(0.1);
System.out.println("Simulation of Lunar Lander Passage at 0.1 seconds");
System.out.println("Current Fuel Flow Rate: "+L.getCurrentFuelFlowRate());
System.out.println("Vertical Speed: "+L.getVerticalSpeed());
System.out.println("Altitude: "+L.getAltitude());
System.out.println("Amount of Fuel: "+L.getAmountOfFuel());
7. L.setCurrentFuelFlowRate(0.3);
L.setPassage(0.09);
System.out.println("Simulation of Lunar Lander Passage at 0.09 seconds");
System.out.println("Current Fuel Flow Rate: "+L.getCurrentFuelFlowRate());
System.out.println("Vertical Speed: "+L.getVerticalSpeed());
System.out.println("Altitude: "+L.getAltitude());
System.out.println("Amount of Fuel: "+L.getAmountOfFuel());
}
}