0% found this document useful (0 votes)
111 views6 pages

Comp1405 A5 F2015

This document describes an assignment to create classes to model parking lots and cars. It involves: 1. A Time class to represent times of day or time periods. 2. A Car class to represent cars with attributes like license plate and parking permit status. 3. A ParkingLot class to represent parking lots with attributes like rate, capacity and revenue. 4. A test program that simulates cars entering and leaving lots at different times, producing a log of events and revenues. The document provides code stubs for methods to complete this simulation.

Uploaded by

Anna Gow
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
111 views6 pages

Comp1405 A5 F2015

This document describes an assignment to create classes to model parking lots and cars. It involves: 1. A Time class to represent times of day or time periods. 2. A Car class to represent cars with attributes like license plate and parking permit status. 3. A ParkingLot class to represent parking lots with attributes like rate, capacity and revenue. 4. A test program that simulates cars entering and leaving lots at different times, producing a log of events and revenues. The document provides code stubs for methods to complete this simulation.

Uploaded by

Anna Gow
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

COMP1405 - Assignment #5

(Due: Monday, November 23rd at 9:00AM)


(1) The Time Class
Define a class called Time that will represent either a time of day (e.g., 10:15),
or a period of time (e.g., a time range such as 72 hours). This class should have attributes
(i.e., instance variables) called hours and minutes, each of type int.
Create the following:

a constructor that accepts two int parameters: one represents hours, and the other represents
minutes. These values should be stored into the instance variables. Note that you MUST
examine the minutes parameter ... if it is greater than 59, then you must transfer the multiples of
60 over to the hours portion of the Time object. (e.g., new Time(10,93) results in 11 hours and 33
minutes). Assume parameters are both >= 0.

another constructor that accepts a double parameter (assumed to be >= 0) which represents the
time and is used to initialize the instance variables. (e.g., a parameter value of 9.5 may either
represent the time 9:30am or a 9 hours and 30 minutes time range, while a parameter value of
20.75 may either represent a time of 8:45pm or a time range of 20 hours and 45 minutes.

a toString() method which returns a String representation of a Time object in the


form hour:minute format (e.g., 9:20 or 16:15).

a static (i.e., class) method difference(Time t1, Time t2), which returns a Time object that is the
difference between two Time objects (i.e., t2 t1). Note that negative times are allowed as well
as times with more than 24 hours are allowed.

Test your class with this program:


public class TimeTestProgram {
public static void main(String args[]) {
System.out.println(new Time(9,30)); // should display 9:30
System.out.println(new Time(21,30)); // should display 21:30
System.out.println(new Time(10,93)); // should display 11:33
System.out.println(new Time(9.5));
// should display 9:30
System.out.println(new Time(21.5)); // should display 21:30
System.out.println(new Time(0,0));
// should display 0:00
System.out.println(new Time(0));
// should display 0:00
System.out.println(new Time(1,1));
// should display 1:01
System.out.println(Time.difference(new Time(1,10), new Time(10,10)));
// should display 9:00
System.out.println(Time.difference(new Time(7,50), new Time(8,10)));
// should display 0:20
System.out.println(Time.difference(new Time(12,57), new Time(2,10)));
// should display -10:47 or -10:-47
}
}

Note that the test cases shown in blue are a little trickier. If you get stuck on these, finish the rest of the
assignment and then come back to them.

(2) The Car Class


Define a class called Car which will be used to represent a car that parks in a parking lot. This class has
three attributes (i.e., instance variables) as follows:

plateNumber - a String representing the license plate (numbers and letters)


permit - a boolean indicating whether or not the car has a parking permit. Any car with a permit
will not have to pay upon leaving the parking lot
enteringTime - a Time object (from part 1) representing the time that the car entered a lot.
Should be null if the car is not currently in a lot.

Create the following:

a constructor that takes a license plate number as a parameter.


By default, new cars have no permit

a constructor that takes two parameters to represent the cars license plate
number as well as a boolean indicating whether or not the car has a permit.

a toString() method that displays a car as follows:


"Car ABC 123"
"Car ABC 123 with permit"

// when the car has no permit


// when the car has a permit

Test your class with this program:


public class CarTestProgram {
public static void main(String args[]) {
System.out.println(new Car("ABC 123"));
System.out.println(new Car("ABC 123", true));
}
}

(3) The ParkingLot Class


Now you will create the parking lot that will be able to make use of the Car and Time objects. Define
a ParkingLot object which maintains the following attributes:

lotNumber - an int representing the number id of the lot


hourlyRate - a double representing the price per hour for parking in this lot.
maxCharge - a double representing the maximum charge that a car would pay regardless of the
hours.
capacity - an int representing the maximum number of cars that can be parked in the lot at any
time.
currentCarCount - an int representing the total number of cars currently in the parking lot.

Create the following:

a constructor that takes no parameters, yet initializes ALL the attributes (i.e., instance variables) to
reasonable default values

a constructor that takes a lot number as well as a capacity as parameters and sets those
appropriately while setting all other attributes to reasonable default values.

a toString() method that returns a string representation of a parking lot that looks like this:
"Parking Lot #1 - rate = $3.25, capacity 4, current

cars 3"

Note that the values in red will obviously vary according to the lot's state.
Test your class with this program:
public class ParkingLotTestProgram {
public static void main(String args[]) {
ParkingLot p0 = new ParkingLot();
ParkingLot p1 = new ParkingLot(1, 4);
ParkingLot p2 = new ParkingLot(2, 6);
System.out.println(p0);
System.out.println(p1);
System.out.println(p2);
p1.hourlyRate = 5.5;
p1.maxCharge = 20.0;
p2.hourlyRate = 3.0;
p2.maxCharge = 12.0;
System.out.println(p1);
System.out.println(p2);
System.out.println(p1.maxCharge);
System.out.println(p2.maxCharge);
}
}

The result should be as follows (where ??? depends on your choice of default values):
Parking
Parking
Parking
Parking
Parking
20.0
12.0

Lot
Lot
Lot
Lot
Lot

#0
#1
#2
#1
#2

rate
rate
rate
rate
rate

=
=
=
=
=

$???,
$???,
$???,
$5.5,
$3.0,

capacity
capacity
capacity
capacity
capacity

???, current cars 0


4, current cars 0
6, current cars 0
4, current cars 0
6, current cars 0

(4) The Simulation


We would like to now simulate a real-life situation in which cars enter and leave parking lots. Examine
the output below. It is a log indicating the events that happened during the day at two parking lots.
Look over the log and make sure that you understand how it relates to real life. Draw a picture of the
parking lot if you are having trouble.

Parking Lot #1 - rate = $5.50, capacity 4, current cars 0


Parking Lot #2 - rate = $3.00, capacity 6, current cars 0
Car
Car
Car
Car
Car
Car
Car
Car
Car
Car
Car

ABC
ABC
ABD
ADE
AFR
AGD
AAA
AAA
AAA
ABB
XYZ

123
124
314
901
304
888
111
111
111
001
678

enters Lot 1 at 7:15.


enters Lot 1 at 7:25.
enters Lot 2 at 8:00.
enters Lot 2 at 8:10.
enters Lot 1 at 8:15.
enters Lot 1 at 8:20.
arrives at Lot 1 at 8:30, but the lot is full.
cannot get in.
enters Lot 2 at 8:32.
enters Lot 2 at 8:50.
with permit enters Lot 2 at 8:55.

Parking Lot #1 - rate = $5.50, capacity 4, current cars 4


Parking Lot #2 - rate = $3.00, capacity 6, current cars 5
Car
Car
Car
Car
Car
Car
Car
Car
Car
Car
Car

ADE
ABC
AGD
ABC
ABB
XYZ
ABB
AFR
AAA
ABD
ABB

901
124
888
123
001
678
001
304
111
314
001

leaves Lot 2 at 9:00 paid $3.00.


leaves Lot 1 at 9:05 paid $11.00.
leaves Lot 1 at 10:00 paid $11.00.
leaves Lot 1 at 10:30 paid $20.00.
leaves Lot 2 at 13:00 paid $12.00.
with permit leaves Lot 2 at 15:15.
enters Lot 1 at 17:10.
leaves Lot 1 at 17:50 paid $20.00.
leaves Lot 2 at 18:00 paid $12.00.
leaves Lot 2 at 18:15 paid $12.00.
leaves Lot 1 at 20:55 paid $20.00.

Parking Lot #1 - rate = $5.50, capacity 4, current cars 0


Parking Lot #2 - rate = $3.00, capacity 6, current cars 0
The total revenue of Lot 1 is $82.0.
The total revenue of Lot 2 is $39.0.

Below is a test program that should produce the output log shown above. You will be getting this code
to work (in part 5) by completing the missing methods shown in red. For now, just make sure that you
understand why the test code produces the output shown above.
public class ParkingSimulationProgram {
public static void carEnters(ParkingLot p, Car c, Time t) {
// ... code is missing ...
}
public static void carLeaves(ParkingLot p, Car c, Time t) {
// ... code is missing ...
}
public static void main(String
Car car1 = new Car("ABC
car3 = new Car("ABD
car5 = new Car("AFR
car7 = new Car("AAA
car9 = new Car("XYZ

args[]) {
123"), car2 =
314"), car4 =
304"), car6 =
111"), car8 =
678", true);

ParkingLot p1 = new ParkingLot(1, 4);


ParkingLot p2 = new ParkingLot(2, 6);
p1.hourlyRate = 5.5;
p1.maxCharge = 20.0;
p2.hourlyRate = 3.0;
p2.maxCharge = 12.0;

new
new
new
new

Car("ABC
Car("ADE
Car("AGD
Car("ABB

124"),
901"),
888"),
001"),

System.out.println(p1);
System.out.println(p2 + "\n");
carEnters(p1,
carEnters(p1,
carEnters(p2,
carEnters(p2,
carEnters(p1,
carEnters(p1,
carEnters(p1,
carEnters(p2,
carEnters(p2,
carEnters(p2,

car1,
car2,
car3,
car4,
car5,
car6,
car7,
car7,
car8,
car9,

new
new
new
new
new
new
new
new
new
new

Time(7,
Time(7,
Time(8,
Time(8,
Time(8,
Time(8,
Time(8,
Time(8,
Time(8,
Time(8,

15));
25));
0));
10));
15));
20));
30));
32));
50));
55));

System.out.println("\n" + p1);
System.out.println(p2 + "\n");
carLeaves(p2,
carLeaves(p1,
carLeaves(p1,
carLeaves(p1,
carLeaves(p2,
carLeaves(p2,
carEnters(p1,
carLeaves(p1,
carLeaves(p2,
carLeaves(p2,
carLeaves(p1,

car4,
car2,
car6,
car1,
car8,
car9,
car8,
car5,
car7,
car3,
car8,

new
new
new
new
new
new
new
new
new
new
new

Time(9, 0));
Time(9, 05));
Time(10, 0));
Time(10, 30));
Time(13, 0));
Time(15, 15));
Time(17, 10));
Time(17, 50));
Time(18, 0));
Time(18, 15));
Time(20, 55));

System.out.println("\n" + p1);
System.out.println(p2 + "\n");
System.out.println("Total revenue of Lot 1 is $" + p1.revenue + ".");
System.out.println("Total revenue of Lot 2 is $" + p2.revenue + "."); ;
}
}

(5) The Missing Code


Complete the carEnters() and carLeaves() methods in the ParkingSimulationProgram so that they
produce the results shown above. Assume that the events of entering/leaving occur chronologically
(i.e., in order of time-of-day). Assume also that all lots are closed from 11pm to 6am so that no cars
enter or leave during that time range. Assume also that all lots are empty when they close at 11pm.
Note that as the cars enter and leave the parking lot, System.out.println() is used to display the Car
object and which lot they are entering/leaving as well as the time entered/left and the amount paid (if
no permit). Make sure to make efficient use of any methods that you wrote in the Time class.
Note that you will need to add a revenue attribute to the ParkingLot to keep track of the money
made by the parking lot so far that day. Run the above test case to make sure that it works.
Note that when a Car with a permit leaves the lot, it should pay nothing. A car without a permit
should pay the hourlyRate times the number of full hours parked. All times in the lot should be
bumped up to the nearest hour. So, parking times of 2:00, 2:01, 2:12, 2:25, 2:34, 2:59,
etc.. would be considered as 3 hours of parking. That is, the amount paid should be a multiple of the
hourlyRate (to a maximum of the maxCharge).

Make sure that your code is robust by handling some potential "impossible" or "weird" cases like the
ones shown below. You will need to add some code to the end of the ParkingSimulationProgram
to simulate these situations/cases. DO NOT MODIFY the current 9 cars or parking lots. Instead,
add these two lines to the end of the ParkingSimulationProgram:
System.out.println("===============================================================");
System.out.println("===============================================================");

And then add your own code to create some new cars and parking lots to test your special cases.
So, to be clear, you will hand in your ParkingSimulationProgram so that it contains the exact same
code as before, with additional code (or your making) appended to the end. Therefore, when the TA
runs your code, he/she should see the original output from part 4 followed by the lines produced from
the above println() statements, followed by the output for your special cases. For each case, please
add a single System.out.println() statement indicating what you are testing. For example, you may
do this:
System.out.println("Testing car trying to leave lot before it has entered ...");
// ... your code
System.out.println("Testing car entering and leaving lot at exact same time ...");
// ... your code
// etc..

Here are the cases:

a car tries to leave a lot before it has even entered.


o (i.e., should do nothing and simply display an error message)
a car enters and leaves a lot at the exact same time of day
o (i.e., cost of zero)
a car enters a lot twice...never leaving in between
nd
o (i.e., should ignore 2 entry into lot altogether with an error message)
a car leaves a lot twice...never entering in between
st
nd
o (i.e., should handle 1 exit but ignore 2 one with an error message)
a car enters into one lot (say P1) but attempts to leave from another lot (say P2).
o (i.e., you need to somehow check for this (add attribute somewhere if you need to) and
display an error message upon attempt to exit a wrong lot).

NOTE: Submit a single zip file containing all of your .java file as well as all test programs. Submit your assignment using
CULearn. Note that if your internet connection at home is down or does not work, we will not accept this as a reason for
handing in an assignment late ... so make sure to submit the assignment WELL BEFORE it is due !

Please NOTE that you WILL lose marks on this assignment if any of your files are missing. You
will also lose marks if your pseudocode is not written neatly with proper indentation. See
examples in the notes for proper style.

You might also like