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...
Create java class with name MyClass1 Instructions for MyClass1 For this portion of the assignment you...
Create java class with name MyClass1 Instructions for MyClass1 For this portion of the assignment you will need to create a java class of your own to model some objects in the real world or your own imagination. your classes should have at least three instances variables, accessor and mutator methods for each of those instance variables, one constructor method of any type, as well as an overridden toString method that will display every field value as part of 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...
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.
Assignment 1: JAVA Classes, Objects, and Constructors The goal of this assignment is to get you...
Assignment 1: JAVA Classes, Objects, and Constructors The goal of this assignment is to get you familiar with some of the most used JAVA syntax, as well as constructors objects, objects calling other objects, class and instance attributes and methods. You will create a small program consisting of Musician and Song classes, then you will have Musicians perform the Songs. Included is a file “Assignment1Tester.java”. Once you have done the assignment and followed all instructions, you should be able to...
Java assignment, abstract class, inheritance, and polymorphism. these are the following following classes: Animal (abstract) Has...
Java assignment, abstract class, inheritance, and polymorphism. these are the following following classes: Animal (abstract) Has a name property (string) Has a speech property (string) Has a constructor that takes two arguments, the name and the speech It has getSpeech() and setSpeech(speech) It has getName and setName(name) It has a method speak() that prints the name of the animal and the speech. o Example output: Tom says meow. Mouse Inherits the Animal class Has a constructor takes a name and...
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.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT