Question

In: Computer Science

Using JAVA: This assignment is about aggregation and class collaboration. You will create several Classes that...

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.

Solutions

Expert Solution


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


Related Solutions

JAVA PROGRAMMING. In this assignment, you are to create a class named Payroll. In the class,...
JAVA PROGRAMMING. In this assignment, you are to create a class named Payroll. In the class, you are to have the following data members: name: String (5 pts) id: String   (5 pts) hours: int   (5 pts) rate: double (5 pts) private members (5 pts) You are to create no-arg and parameterized constructors and the appropriate setters(accessors) and getters (mutators). (20 pts) The class definition should also handle the following exceptions: An employee name should not be empty, otherwise an exception...
java For this assignment, you will create a Time class that holds an hour value and...
java For this assignment, you will create a Time class that holds an hour value and a minute value to represent a time. We will be using "military time", so 12:01 AM is 0001 and 1 PM is 1300. For this assignment, you may assume valid military times range from 0000 to 2359. Valid standard times range from 12:00 AM to 11:59 PM. In previous assignments, we had a requirement that your class be named Main. In this assignment, the...
0. Introduction. In this assignment you will implement a stack as a Java class, using a...
0. Introduction. In this assignment you will implement a stack as a Java class, using a linked list of nodes. Unlike the stack discussed in the lectures, however, your stack will be designed to efficiently handle repeated pushes of the same element. This shows that there are often many different ways to design the same data structure, and that a data structure should be designed for an anticipated pattern of use. 1. Theory. The most obvious way to represent a...
C++ HW Aim of the assignment is to write classes. Create a class called Student. This...
C++ HW Aim of the assignment is to write classes. Create a class called Student. This class should contain information of a single student. last name, first name, credits, gpa, date of birth, matriculation date, ** you need accessor and mutator functions. You need a constructor that initializes a student by accepting all parameters. You need a default constructor that initializes everything to default values. write the entire program.
In Java, using the code provided for Class Candle, create a child class that meets the...
In Java, using the code provided for Class Candle, create a child class that meets the following requirements. Also compile and run and show output ------------------------------------------------------------------------ 1. The child class will be named  ScentedCandle 2. The data field for the ScentedCandle class is:    scent 3. It will also have getter and setter methods 4. You will override the parent's setHeight( ) method to set the price of a ScentedCandle object at $3 per inch (Hint:   price = height * PER_INCH) CODE...
Using a minimum of 2 classes create a java program that writes data to a file...
Using a minimum of 2 classes create a java program that writes data to a file when stopped and reads data from a file when started. The data should be in a readable format and the program should work in a way that stopping and starting is irrelevant (e.g. all data doesn't have to save just the important elements.) Program should be unique and semi-complex in some way.
in Java using netbeans create a project and in it a class with a main. We...
in Java using netbeans create a project and in it a class with a main. We will be using the Scanner class to read from the user. At the top of your main class, after the package statement, paste import java.util.Scanner; Part A ☑ In your main method, paste this code. Scanner scan = new Scanner(System.in); System.out.println("What is x?"); int x = scan.nextInt(); System.out.println("What is y?"); int y = scan.nextInt(); System.out.println("What is z?"); int z = scan.nextInt(); System.out.println("What is w?");...
in Java using netbeans create a project and in it a class with a main. We...
in Java using netbeans create a project and in it a class with a main. We will be using the Scanner class to read from the user. At the top of your main class, after the package statement, paste import java.util.Scanner; Part A ☑ In your main method, paste this code. Scanner scan = new Scanner(System.in); System.out.println("What is x?"); int x = scan.nextInt(); System.out.println("What is y?"); int y = scan.nextInt(); System.out.println("What is z?"); int z = scan.nextInt(); System.out.println("What is w?");...
JAVA A simple Class in a file called Account.java is given below. Create two Derived Classes...
JAVA A simple Class in a file called Account.java is given below. Create two Derived Classes Savings and Checking within their respective .java files. (modify display() as needed ) 1. Add New private String name (customer name) for both, add a New int taxID for Savings only. 2. Add equals() METHOD TO CHECK any 2 accounts Demonstrate with AccountDemo.java in which you do the following: 3. Create 1 Savings Account and 3 Checking Accounts, where 2 checkings are the same....
JAVA Assignement In the same file, create two classes: a public class Module1 and a non-public...
JAVA Assignement In the same file, create two classes: a public class Module1 and a non-public (i.e. default visibility) class Module1Data. These classes should have the following data fields and methods: 1) Module1 data fields: a. an object of type Module1Data named m1d b. an object of type Scanner named scanner c. a double named input 2) Module1Data methods: a. a method named square that returns an int, accepts an int parameter named number, and returns the value of number...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT