P5: The Gas Station
COMP 14-091 Summer Session I 2000
Assigned: Tuesday, June 13
Due: Tuesday, June 20, at the
beginning
of class!
I changed GasStation.java! The three constants
at the top of the class should say public static final int...
Description
In this exercise you will implement two classes to represent a gas station.
Then you will write a program to use these classes to run a simulation
of a gas station.
The two classes you need to write are:
-
GasPump -- this class represents a gas pump and keeps track of how much
gas there is in the pump's tank
-
GasStation -- this of course represents the station and it has three pumps:
one for each grade of gas.
Then you will write a program that uses these two classes. It should pick
a random number of gallons, a random grade of gasoline and a maximum price
that customer is willing to pay for each customer. Then it should use a
GasStation object that you've instantiated to simulate purchasing the gas
(choose your own numbers for the price of the gasoline and the number of
gallons in the gas pumps to start with). If the gas station's price is
too high, then the customer doesn't buy the gas. If there isn't enough
gas of the grade they want, they also leave without buying gas. You should
continue to run this simulation until the gas station runs out of all three
grades of gas. Then it should report these statistics for the gas station:
-
total money made by the gas station
-
total number of customers coming to the gas station
-
number of customers buying regular, plus and supreme gas
-
number of customers that had to be turned away because the station ran
out of the grade of gas that they wanted
-
number of customers that left because the price was too high
What if the station runs out of their type of gas and the price is too
high? Well, then just count them as a person who thought the price was
too high. If the price is too high, they wouldn't have even bothered to
see if there was any gas.
What does "run out of gas" mean?
In this case, we mean when a customer comes along that wants more gas than
is available in the pump's tank, you can say that that pump has run out
of gas.
For example, say there are 10 gallons of gas left in the regular pump.
A customer drives up and asks for 12 gallons. You don't have enough, so
you can consider that pump out of gas. (even though someone could drive
up and only want 5 gallons and you'd have that...) Now, it's up to you
as to whether you give the person who only wants 5 gallons of gas the gas
or not. The "run out of gas" condition is just for the purposes of ending
the simulation. In fact, it is easier to program if you give this person
the gas.
Don't forget to check the program grading criteria
to see all the normal stuff your program should do.
Grading for this Assignment
Assuming you perfectly comment your program and have excellent programming
style, here is what you need to have working to get each of these letter
grades:
-
F - GasPump class and GasStation class aren't implemented correctly (so
the rest of the program won't run either)
-
D - GasPump class is implemented correctly
-
C - GasPump and GasStation classes are implemented correctly
-
B - GasPump, GasStation classes correct & you can simulate multiple
people buying gas correctly (but maybe you can't tell when the pumps run
out of gas, and you don't have all the statistics printing correctly)
-
A - all needed for B + you need to report the statistics correctly
Steps to Follow
Same as usual. This time you will have 3 classes in your project. Here
are their names, what kind of class you should add, and a framework I made
to start from:
-
GasSimulator.java - this should be a ClassMain - framework for
GasSimulater.java
-
GasPump.java - this should be a Class - framework for GasPump.java
-
GasStation.java - this should be a Class - framwork for GasStation.java
Don't forget all the comments and the honor code pledge that should go
at the top of each class in your program!
What is in the GasPump class
-
private double gallonsAvailable - this instance variable represents
the number of gallons of fuel that are in the pump's tank
-
public GasPump(double quantity) - this is the constructor for
this class. In this case it has a parameter that indicates the number of
gallons of gas that should initially be in the pump's tank.
-
public double checkTank() - this method checks to see how much
gas is in the pump's tank. It should return the number of gallons that
are still in the tank.
-
public void pumpGas(double numGallons) - this method is used to
simulate pumping gas from the pump's tank to into the customer's vehicle.
So, it should basically just update the amount of fuel in the pump's tank
to represent the fact that it's now gone.
Your job in this class is to fill in the methods to make them do what is
expected. Also, you need to provide comments for the methods.
What is in the GasStation class
-
REGULAR, PLUS, and SUPREME are three static
constants that allow us to use integers to represent the three different
grades of gasoline.
-
pump_regular, pump_plus, and pump_supreme are
three instance object references that should be used to hold the three
gas pump objects we need at this gas station
-
price_regular, price_plus, and price_supreme
are instance variables that represent the price for the three grades of
gas.
-
money is an instance variable that represents the amount of money
that the gas station has made
-
public GasStation(double gallonsPerPump, double standardPrice)
- this constructor has two parameters. the first is the number of gallons
of gas that each pump should start off with. The second number is the price
that each gasoline grade's price should start off as.
-
public void setGasPrice(int grade, double newPrice) - this method
sets the new price for a specific grade of gas. The first parameter should
be one of the three constants defined above, and the second parameter is
the new price.
-
public double getGasPrice(int grade) - this method gets the price
for a specific grade of gas. the parameter should be one of the constants
defined above to indicate the grade of gas that they want to know the price
of.
-
public double buyGas(int grade, double numGallons) - this method
goes through the process of actually buying the gas. It should calculate
the total cost of the gas that the person is buying and add that to the
amount of money the gas station has. It should also make sure to remove
that amount of gas from the gas pump. Finally, if there is not enough gas
of the grade that the person wants, then it should return a negative number
to indicate that there wasn't enough gas. If there was enough gas then
it should return the total price of the gas.
-
public double moneyEarned() - this method should return the total
amount of money that the gas station has earned.
What is in the GasSimulator class
I have made three useful methods for you to use in your main method. They
pick the three random numbers that you need to run the simulation:
-
private static double randNumGallons() - this method returns a
random number of gallons from 0 to 20.
-
private static int randGrade() - returns a random grade of gasoline
(GasStation.REGULAR, GasStation.PLUS or GasStation.SUPREME)
-
private static double randMaxPrice() - returns a random price
(from 1.25 to 1.75) that can be used as the maximum price a customer is
willing to pay for their gas.
Testing your code
The following are some pieces of code that you can use to test the two
classes before you write your main method. The first thing you should do
is fill in GasPump.java. After you do that, you should replace the main
method in GasSimulator.java with this one and run it. It will test parts
of your class to make sure they work. DISCLAIMER: Just because your
class passes this test doesn't mean that everything in it is right. I'm
just trying to help you some.
public static void main(String[] args)
{
GasPump pump;
// instantiate a GasPump object with 10 gallons
in it
pump = new GasPump(10);
System.out.println("Pump has " + pump.checkTank()
+ " gallons in it.");
System.out.println("Should be 10");
// pump out 8.25 gallons of gas, and make sure
there is 1.75 gallons left
pump.pumpGas(8.25);
System.out.println("\nPump now has " + pump.checkTank()
+ " gallons in it.");
System.out.println("Should be 1.75");
}
After doing the above, you should write your GasStation.java code and
use this piece of code to test the two classes you have written now. (Once
again, the above disclaimer applies here too):
public static void main(String[] args)
{
GasStation crown;
double price, totalPaid;
// instantiate a GasStation object with 100 gallons
per tank and
// prices of 1.25 each
crown = new GasStation(100.0, 1.25);
System.out.println("Station has earned $" +
crown.moneyEarned());
System.out.println("Should be $0");
// change gas prices so they aren't all the same
crown.setGasPrice(GasStation.REGULAR, 1.15);
crown.setGasPrice(GasStation.SUPREME, 1.35);
// buy gas at each price to see if that works:
price = crown.buyGas(GasStation.REGULAR, 10.0);
System.out.println("\n10 gallons of regular
gas cost $" + price);
System.out.println("Should be 11.50");
totalPaid = price;
price = crown.buyGas(GasStation.PLUS, 10.0);
System.out.println("\n10 gallons of plus gas
cost $" + price);
System.out.println("Should be 12.50");
totalPaid += price;
price = crown.buyGas(GasStation.SUPREME, 10.0);
System.out.println("\n10 gallons of supreme
gas cost $" + price);
System.out.println("Should be 13.50");
totalPaid += price;
System.out.println("\nThe gas station has earned
$" + crown.moneyEarned());
System.out.println("Should be $" + totalPaid);
price = crown.buyGas(GasStation.SUPREME, 15.0);
System.out.println("\nTried to buy 15 gallons,
but there should have been only 10 left");
System.out.println("This number should be negative:
" + price);
}
This is exactly the sort of thing you should do when writing this stuff
for yourself from scratch. These little main methods are called drivers.
(see the book's discussion on page 186). They are little pieces of code
to test the functionality of your classes.
What to turn in
(don't forget to keep a copy for yourself!):
-
a disk with your Java code (the files GasSimulator.java, GasPump.java
and
GasStation.java) on it in a directory called P5. It's ok if
this directory contains the other files that Microsoft Visual J++ puts
in the project directory.
-
No printout is necessary for this assignment
-
any notes/comments you might have about your solution to the assignment,
trouble you had, etc. This should go in a file called "readme.doc" in the
directory
P5
-
Ok, here's exactly what your should be on your disk (you will lose 10 pts
if you don't follow this exactly!):
-
A:\P5\GasSimulator.java the Java code with main()
-
A:\P5\GasPump.java
the class which implements a gas pump
-
A:\P5\GasStation.java the class
which implements a gas station
-
A:\P5\readme.doc
anything you would like me to read while I'm grading your assignment (optional)
Notes
-
Check out the general Program Grading Criteria
-
Take a look at the Ten Steps to make your
life easier.
-
DON'T forget to comment your program! You will lose points if you don't
comment the program! (the grading is 50% for correctness and 50% for
commenting)
Note: this is only an example. Your work does not need to match the
example word for word.