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...
Using Java: Create a class that will hold the information about a clock, called Clock. You...
Using Java: Create a class that will hold the information about a clock, called Clock. You need to keep minutes, hours and seconds in military time (24-hour) format. Include member functions that will setTime ( this will set the hours, minutes and seconds of the clock), getHours (return the hours), getMinutes (return the minutes) and getSeconds (return the seconds). Another method printTime which will print the time out like this 03:13:09 (hours:minutes:seconds). Overloaded constructors that will take in hours, minutes...
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...
Using Java Summary Create a Loan class, instantiate and write several Loan objects to a file,...
Using Java Summary Create a Loan class, instantiate and write several Loan objects to a file, read them back in, and format a report. Project Description You’ll read and write files containing objects of the Loan class. Here are the details of that class: Instance Variables: customer name (String) annual interest percentage (double) number of years (int) loan amount (double) loan date (String) monthly payment (double) total payments (double) Methods: getters for all instance variables setters for all instance variables...
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?");...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT