In: Computer Science
Using JAVA:
This assignment is about aggregation and class collaboration.
You will create several Classes that will be part of an overall class named InstrumentDisplay. The classes are FuelGage, Odometer, MilesSinceLastGas, and CruisingRange.
The FuelGage should assume a 15 gallon tank of gasoline and an average consumption of 1 gallon every 28 miles. It should increment in 1 gallon steps when you "add gas to the tank". It should decrement by 1 every 28 miles. It should display its current value.
You should also have a display that shows how many miles traveled since you last added gasoline, MilesSinceLastGas, and a display for the estimated number of miles until you run out of gasoline, the CruisingRange.
The Odometer should keep track of total mileage from 0 to 999,999. After that it turns over back to 0. Make sure your code allows for this rollover.
The overall class is to be named InstrumentDisplay. You may create the various outputs using System.out or JOptionPane. Make sure that account for both filling and emptying the tank. While your odometer will display in 1 mile increments, you should keep track of mileage internally in one tenth of a mile increments for purposes of computing gasoline remaining in the FuelGage and miles in the CruisingRange
Please make sure you comment your code thoroughly.
The code should be nicely formatted and should use proper variables.
public class FuelGuage {
private static double capacity = 15;
public static double getCapacity() {
return capacity;
}
public static void emptyTank()
{
if(capacity>0)
capacity =
capacity-0.1;
}
public void fillTank()
{
if(capacity <15)
{
capacity =
capacity + 1;
MilesSinceLastGas.setMilesTraveled(0);
}
}
public void currentCapacity() {
System.out.println("Current
Capacity = "+capacity);
}
}
public class Odometer {
private double mileage;
public void incMileage()
{
mileage +=
(mileage%999999)+1;
MilesSinceLastGas.setMilesTraveled(MilesSinceLastGas.getMilesTraveled()
+ 1);
if((mileage%2.8) == 0)
{
FuelGuage.emptyTank();
}
}
public double getMileage() {
return mileage;
}
public void setMileage(double mileage) {
this.mileage = mileage;
}
}
public class MilesSinceLastGas {
private static double milesTraveled;
public void milesTraveled()
{
System.out.println("Miles travled
since last fillup" + getMilesTraveled());
}
public static double getMilesTraveled() {
return milesTraveled;
}
public static void setMilesTraveled(double
milesTraveled) {
MilesSinceLastGas.milesTraveled =
milesTraveled;
}
}
public class CruisingRange {
private double range()
{
return
(FuelGuage.getCapacity())*2.8;
}
public void milesLeft()
{
System.out.println("Cruising Range"
+ range());
}
}
public class InstrumentDisplay {
private static FuelGuage f = new FuelGuage();
private static Odometer o = new Odometer();
private static MilesSinceLastGas mslg = new
MilesSinceLastGas();
private static CruisingRange cr = new
CruisingRange();
public static void main(String args[])
{
o.setMileage(0);
for(int i=0;i<28;i++)
o.incMileage();
f.currentCapacity();
mslg.milesTraveled();
cr.milesLeft();
}
}