APS101: Assignment 1

Introduction

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:

The Task

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.

Background

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:

Class Car

The Car class represents a car. It has a number of basic fields and methods:

Variables

Type

Description

int

The year the car was made.

String

The brand of the car.

String

The model of the car.

String

The license plate of the car.

String

The colour of the car.

double

The price of the car, in dollars (with at most 2 decimal places).

Methods

Constructor

Description

Car(int, String, String, String, double)

Constructor for the Car class. The parameters, in order, are: the year of the car, the car brand, the car model, the car colour, the car price. Note that the license plate is null, by default.

Method Name

Description

getYear()

Return the year of this car (as an int).

getBrand()

Return the brand of this car (as a String).

getModel()

Return the model of this car (as a String).

getLicensePlate()

Return the license plate of this car (as a String).

getColour()

Return the colour of this car (as a String).

getPrice()

Return the price of this car, in dollars (as a double).

setLicensePlate(String)

Set the license plate of this car to a new value.

setColour(String)

Set the colour of this car to a new value.

Class Driver

The Driver class represents a driver of a car. It has a number of fields and methods:

Variables

Type

Description

Car

The current car of the driver.

String

The name of the driver.

int

The current age (in years) of the driver.

double

The amount of money the driver has, in dollars (with at most 2 decimal places).

double

The monthly insurance cost on the driver's current car, in dollars (with at most 2 decimal places).

int

The number of demerit points the driver has.

int

A static variable. Keeps track of the total number of used cars (by any driver).

Constants

Type

Name

Value

Description

public static final String

LICENSE_PLATE

"APS 101"

The standard license plate that should be given to a car, if no other license plate is provided.

Methods

Constructors

Description

Driver(String, int, double)

Constructor 1 for the Driver class. The parameters, in order, are: the name of the driver, the age of the driver, the amount of money the driver has.

Note that the driver should start off with no demerit points. Also, note that the driver has no car, and therefore should have no monthly insurance payments.

Driver(String, int, double, Car)

Constructor 2 for the Driver class. The parameters, in order, are: the name of the driver, the age of the driver, the amount of money the driver has, the car that the driver owns.

Note that the driver should start off with no demerit points. Also, since the driver has a car, the monthly insurance cost needs to be udpated (see below).
Since no license plate was provided, the license plate of the driver's car should be set to the standard license plate.

Driver(String, int, double, Car, String)

Constructor 3 for the Driver class. The parameters, in order, are: the name of the driver, the age of the driver, the amount of money the driver has, the car that the driver owns, the license plate of the car.

Note that the driver should start off with no demerit points. Also, since the driver has a car, the monthly insurance cost needs to be udpated (see below).
The license plate of the driver's car should be set to the value that is provided.

Method Name

Description

getCar()

Return the car of this driver (as a Car).

getName()

Return the name of this driver (as a String).

getAge()

Return the age of this driver (as an int).

getMoney()

Return the amount of money this driver has (as a double).

getInsurance()

Return the monthly insurance cost on this driver's car (as a double).

getDemeritPoints()

Return the number of demerit points this driver has (as an int).

hasCar()

Return true if this driver has a car, and return false if this driver does not have a car (as a boolean).

inDebt()

Return true if this driver is in debt (i.e., the amount of money this driver has is a negative value), and return false if this driver is not in debt (as a boolean).

canAfford(Car)

Return true if this driver can afford the given car (i.e., the amount of money this driver has is at least as much as this car is worth), and return false if this driver cannot afford the car (as a boolean).

repaintCar(String)

Set the colour of this driver's car to the new specified colour.

calculateInsurance()

Calculate and return the monthly insurance cost on this driver's car (as a double). Use the following formula to do this:

Multiply 5% of the car's cost by the inverse of the base-10 logarithm of the driver's age, then add the number of demerit points the driver has multiplied by 100.

updateInsurance()

Calculate the cost of the monthly insurance on this driver's car and store this new value.
Use bankersRound on the value first, before storing it, so it has at most 2 decimal places (see below).

receiveSpeedingTicket(int)

Increase the number of demerit points this driver has by the given amount. Also, the monthly insurance cost needs to be updated.

cleanRecord()

Remove all the demerit points from this driver. Also, the monthly insurance cost needs to be updated.

receiveParkingTicket(double)

Decrease the amount of money this driver has by the specified amount (the parking ticket fine).

makeMoney(double)

Increase the amount of money this driver has by the specified amount (the money the driver made).

payInsurance()

Decrease the amount of money this driver has by the current cost of this driver's monthly insurance.

growOlder()

Increase the age of this driver by one year. Also, the monthly insurance cost needs to be updated.

sellCar()

Get rid of this driver's current car. (NOTE: you can assume that the driver has a car.)

Since this driver makes a profit, increase the amount of money this driver has by 75% of the original price of the car that is sold.
Use bankersRound on the new money value first, before storing it, so it has at most 2 decimal places (see below).

This driver should now have NO monthly insurance payments. Also, the number of used cars needs to be updated.

buyCar(Car, String)

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.)

Decrease the amount of money this driver has by the price of the new car. Also, the monthly insurance cost needs to be updated.

carInfo()

Return the description of this driver's current car (as a String). The format is as follows:

[year] [brand] [model], [colour], License plate: [license plate]

Example: "2005 Honda Civic, red, License plate: APS 101"
You must format the output exactly like this -- pay attention to commas, spacing, and letter case.

driverInfo()

Return the description of this driver (as a String). The format is as follows:

[name], [age] years old, $[money] in the bank

Example: "Bob, 20 years old, $3000 in the bank"
You must format the output exactly like this -- pay attention to commas, spacing, and letter case.
Also, note that the
money variable should be an integer (which is equal to the double value, with the decimal truncated)

.

.

getNumberOfUsedCars()

A static method. Returns the total number of cars that have been used by any driver (as an int).

compareCars(Car, Car)

A static method. Returns true if the two given cars are the same (you should compare only three things: the year of the cars, the brand of the cars, and the model of the cars), and return false if the two given cars are different (as a boolean).

bankersRound(double)

A static method. Returns the value of the given number, rounded to at most 2 decimal places (as a double).
This is done by multiplying the number by 100, then using Math.rint on it, then dividing it by 100.

Failure to use bankersRound where specified may result in errors when we test your code.
There will be NO remarks if you make this mistake.

Note:

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).

Restrictions

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 (<, <=, ==, =>, >, !=, &&, ||, !).

Marking

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.

What to Hand In

The course website describes how you hand in your assignment. This section tells you what to hand in:

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:

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.