The main purpose of the assignment is to get you to think about classes, objects, class (static) methods and variables, and object (instance) methods and variables, and to write them all out. Understanding the concepts is not difficult; one of the challenges is to translate the English description you are given below into Java in the first place. The other main challenge is getting everything right in Java - especially if you've never done it before. What the classes are all about is sometimes artificial and does not correspond to the real world - that's because this is an exercise, and some things are simplified to keep them doable.
The following are some hints:
Start your assignment early; most people take much longer than they originally anticipate.
Read through the description carefully, to make sure you understand it.
If you are having difficulty understanding or doing the assignment: come and see the instructor or a teaching assistant - but don't wait until the last minute; it's more difficult to provide help in an emergency.
For this assignment, you are asked to work with 2 classes:
Car
and Driver
. We provide you with the Car.class file -- but
not the source code! -- as well as a sample tester program for you to see how Car
works and
how your code will be tested for correctness.
Your main task is to
write the Driver
class from scratch, according to the exact specification given below.
In large cities like Toronto, many people own a car and use it as the primary mode of transportation. This assignment is about representing the cars and drivers as objects with different properties described below, and simulating some of the actions of a driver. Cars have a colour, brand, model, price, and so on. Drivers typically own one car and can sell it or buy a new one. Drivers also have to pay insurance, which can increase if the driver breaks the law, by getting a speeding ticket, for example.
Here are the class descriptions:
Pay close attention to the tables below! There are two crucial things to remember:
Descriptions: while the descriptions given below are informal, they are precise and complete - everything is here, and well-defined;
Naming: you have to decide on your variable names, but your class must have exactly the constant and method names described in the tables - including the right letter case - and the methods must have exactly the parameters given: same order and same type, and must have the right return type as stated in the description. If you have to, use copy and paste for the method name, etc. from this page to get it right. (If you get it wrong, the assignment testing programs will not recognize your methods and you will get no credit for the methods.) As with the variables, we leave the parameter names up to you.
Car
The Car
class represents a car. It has a number
of basic fields and methods:
Type |
Description |
---|---|
|
The year the car was made. |
|
The brand of the car. |
|
The model of the car. |
|
The license plate of the car. |
|
The colour of the car. |
|
The price of the car, in dollars (with at most 2 decimal places). |
Constructor |
Description |
---|---|
|
Constructor for the |
Method Name |
Description |
|
Return the year of this car (as an |
|
Return the brand of this car (as a |
|
Return the model of this car (as a |
|
Return the license plate of this car (as a |
|
Return the colour of this car (as a |
|
Return the price of this car, in dollars (as a |
|
Set the license plate of this car to a new value. |
|
Set the colour of this car to a new value. |
Driver
The Driver
class represents a driver of a car. It has a number
of fields and methods:
Type |
Description |
---|---|
|
The current car of the driver. |
|
The name of the driver. |
|
The current age (in years) of the driver. |
|
The amount of money the driver has, in dollars (with at most 2 decimal places). |
|
The monthly insurance cost on the driver's current car, in dollars (with at most 2 decimal places). |
|
The number of demerit points the driver has. |
|
A |
Type |
Name |
Value |
Description |
---|---|---|---|
|
|
|
The standard license plate that should be given to a car, if no other license plate is provided. |
Constructors |
Description |
---|---|
|
Constructor 1 for the |
|
Constructor 2 for the |
|
Constructor 3 for the |
Method Name |
Description |
|
Return the car of this driver (as a |
|
Return the name of this driver (as a |
|
Return the age of this driver (as an |
|
Return the amount of money this driver has (as a |
|
Return the monthly insurance cost on this driver's car (as a |
|
Return the number of demerit points this driver has (as an |
|
Return |
|
Return |
|
Return |
|
Set the colour of this driver's car to the new specified colour. |
|
Calculate and return the monthly insurance cost on this driver's car (as a |
|
Calculate the cost of the monthly insurance on this driver's car and store this new value. |
|
Increase the number of demerit points this driver has by the given amount. Also, the monthly insurance cost needs to be updated. |
|
Remove all the demerit points from this driver. Also, the monthly insurance cost needs to be updated. |
|
Decrease the amount of money this driver has by the specified amount (the parking ticket fine). |
|
Increase the amount of money this driver has by the specified amount (the money the driver made). |
|
Decrease the amount of money this driver has by the current cost of this driver's monthly insurance. |
|
Increase the age of this driver by one year. Also, the monthly insurance cost needs to be updated. |
|
Get rid of this driver's current car. (NOTE: you can assume that the driver has a car.) |
|
Set this driver's car to be the given car, with the specified license plate. (NOTE: you can assume that the driver does not have a car.) |
|
Return the description of this driver's current car (as a |
|
Return the description of this driver (as a |
. |
. |
|
A |
|
A |
|
A |
As a general rule, you can assume that all parameters have correct values:
not null where not null is expected, positive where expected, etc. This means
that your methods don't have to worry about these input values.
Also, you don't have to worry about situations that are not specified in the description above.
For example, we won't be running the repaintCar()
or calculateInsurance()
methods when the driver has no car (this would produce an error).
You must not use if-statements, loops and arrays. If you do, you will lose marks. (We leave those for later assignments.)
You will find that you can do everything you need to do with the
arithmetic operators (+, -) and boolean
operators (<,
<=, ==, =>, >, !=, &&
, ||
,
!
).
For this assignment, you will only be marked on CORRECTNESS.
In other words,
your program has to work exactly as specified. We will try to test as many individual
portions of your code as possible -- so you can get part marks, even if you didn't
complete all the methods -- but try to do as much of the assignment as possible, and remember
to test it carefully to make sure there are no errors.
Also, make sure that you read the Assignment
rules page for some general rules and guidelines -- there are
more hints there about commenting and style, for future assignments.
The course website describes how you hand in your assignment. This section tells you what to hand in:
Driver.java
.
Do NOT hand in
Car.class
and
CarTester.java
(as we have already provided you with these files!).
Remember that spelling, including case, count in Java: your files must be named exactly as above.
NOTE: Other than the signoff statement,
only hand in the files that end with the .java
suffix. Be careful about this, because in the same place as your
.java
files you may also have files with the extension
.class
(that is, they end with the .class
suffix), but otherwise have the same name. Two particular pitfalls:
Microsoft operating systems often do not display file extensions unless they've been told to do so.
DrJava creates "backup files", which means that
you'll see filenames like "Plane.java~
".
Notice the ~
at the end. It indicates that the file is
a backup file, which happens to be an older version of your program.
Don't hand these in.
Since .class
files cannot be read by TAs or run with
our testing programs, submitting the wrong files might cause you to
fail the assignment. Every year, a half-dozen students submit the
wrong file; we simply cannot do anything to fix this mistake.