Question: You will be implementing and testing two classes: Rocket and Thruster. Thruster Class Thruster has one constructor: Thruster(double mass): where the provided mass initializes the
You will be implementing and testing two classes: Rocket and Thruster.
Thruster Class
Thruster has one constructor:
- Thruster(double mass): where the provided mass initializes the Thrusters mass.
Thruster has one private data member:
- double mass: represents the current amount of fuel the thruster has.
Thruster has two public member functions:
- double GetMass(): returns the current amount of fuel the thruster has.
- double Burn(double mass): this function burns the provided amount of fuel mass, returning the force produced by burning the fuel. This function also decreases the thrusters current mass by the amount of fuel burnt. If the supplied mass is more than the current mass available in the thruster, use the remaining mass in the thruster instead.
The expression for the force produced by burning a given mass is provided (see below).
Use the following code to get started:
class Thruster
{
public static double THRUST = 20.0;
private double mass;
public Thruster (double mass)
{
// Initialize thruster's initial fuel mass.
// ...
}
public double GetMass()
{
// Return the Thruster's remaining fuel mass.
// ...
}
public double Burn(double mass)
{
// If provided mass is larger than remaining fuel mass,
// then use the remaining fuel mass instead.
// ...
// Compute force burning mass created
double force = THRUST * mass;
// Reduce thruster's mass by amount burned
// ...
// Return computed force
// ...
}
}
Rocket Class
The Rocket class represents a rocket with a mass, a speed, and a Thruster.
Rocket has one constructor:
- Rocket(double mass, Thruster thruster): mass is the mass of the rocket and thruster is a Thruster object used to change the speed of the rocket.
Rocket has three private data members:
- double speed: the current speed of the rocket (default: 0)
- double mass: the mass of the rocket.
- Thruster thruster: the thruster attached to the rocket, which provides thrust.
Rocket has two public member functions:
- double GetSpeed(): returns the current speed of the rocket.
- double Burn(double mass): burns the provided fuel mass to increase the speed of the rocket.
Note, if the fuel mass provided to Burn is larger than the remaining fuel mass of the Rockets Thruster, the remaining fuel mass in the Thruster should be used instead.
The expression to update the objects speed based on the Thruster is provided (see below).
Use the following code to get started:
class Rocket
{
private double speed;
private double mass;
private Thruster thruster;
public Rocket (double mass, Thruster thruster)
{
// Initialize private data members
// ...
}
public double GetSpeed()
{
// Return the Rockets speed.
// ...
}
public void Burn(double burnMass)
{
// Get thruster mass prior to burn (thrusterMass)
// ...
// Limit burnMass to be the smaller of burnMass and thrusterMass
// ...
// Get rocket and thruster combined mass prior to burn (totalMass)
// ...
// Burn fuel mass with thruster to obtain thrust force (force).
// ...
// Update Rocket speed using thrust force
this.speed += force * (Math.Log10(totalMass) - Math.Log10(totalMass - burnMass)) / Math.Log10(Math.E);
}
}
Using Rocket and Thruster
Finally, in Main, test out your Rocket and Thruster classes.
Create a Thruster object and a Rocket object.
- Thruster with mass of 110
- Rocket with mass of 100, with the thruster created before.
Using your Rocket object, burn 20 units of fuel and print the Rockets resultant speed to console.
Next, burn an additional 100 units and print the rockets final speed to console.
Run your code and record the results. At the bottom of Main, as a comment, include both printed speeds as part of your final submission (rounded to the first decimal place is fine).
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
