Exercise 1 [5 points]:
Create the following classes shown in the UML diagram. Then, create PointTest.java class with main method to test all functionality of these classes.
Exercise 2 [10 points]:
The following figure shows a UML diagram in which the class Student is inherited from the class
a. Implement a Person class. The person constructor takes two strings: a first name and a last name. The constructor initializes the email address to the first letter of the first name followed by first five letters of the last name followed by @tru.ca. If the last name has fewer than five letters, the e-mail address will be the first letter of the first name followed by the entire last name followed by a @tru.ca. Examples:
b. Override Object’s toString method for the Person class. The toString method should return the present state of the object.
c. Now, create a Student class that is a subclass of Person and implements Comparable interface.
d. The Student constructor will be called with two String parameters, the first name and last name of the student. When the student is constructed, the inherited fields lastName, firstName, and email will be properly initialized, the student’s gpa and number of credit will be set to 0. The variable lastIdAssigend will be properly incremented each time a Student object is constructed and the studentId will be set to the next available ID number as tracked by the class variable lastIdAssigend.
e. Override the object’s toString method for the Student class. The toString method should return the present state of the object. Note that it should use the toString() method from its superclass.
f. The addCourse() method should update the credits completed, calculate, and update the gpa value.
Use the following values for grade:
Example GPA calculation:
GRADE CREDIT CALCULATION
(A) 4.0 x 4 = 16.00
(B) 3.0 x 4 = 12.00
(B) 3.0 x 4 = 12.00
(A) 4.0 x 1 = 4.00
(C) 2.0 x 3 = 6.00
GPA = 50.00 / 16 = 3.125; the getGPA() method should return this value.
g. Students are compared to each other by comparing GPAs. Override the compareTo() method for the student class. Note that to override the compareTo() method, the Student class must implement Comparable interface.
Now, test your code with the supplied client code (StudentClient.java). Note: You should not modify this client code. We will use the same client code to test your classes.
Exercise 3 [10 points]:
In this exercise, you need to implement a class that encapsulate a Grid. A grid is a useful concept in creating board-game applications. Later we will use this class to create a board game. A grid is a two-dimensional matrix (see example below) with the same number of rows and columns. You can create a grid of size 8, for example, it’s an 8×8 grid. There are 64 cells in this grid. A cell in the grid can have any arbitrary value. Later in this course, we will learn how to create a cell that can hold any arbitrary values. However, for the time being, we will assume that the cell-values are non-negative integers. If a cell contains a value ‘0’ that means, the cell is empty. To identify a cell, you need row number and column number, for example, cell (1, 3) means 3rd cell of the
1st row. You need to create a class that satisfies the following requirements.
Write tests and code for each of the following requirements, in order. The words in bold indicate message names. Whenever a requirement says the user can “ask whether…“, the expected answer is boolean. Whenever a requirement speaks of a “particular” item, then that item will be an argument to the method.
1. The user can create a Grid specifying the number of row and column. Also the user can create a Grid specifying the size only.
2. The user can ask a Grid whether its isEmpty. A grid is empty when all the cells of the grid is empty.
3. The user can clear all the cells of the grid. It’s a void method.
4. The user can ask a Grid whether a particular cell isValid.
5. The user can ask a Grid to set a particular value by setValue to a particular row and col. The grid sets the value only if that cell is valid.
6. The user can ask a Grid to getValue (in this case integer) from a particular row and col. The grid returns the value only if the locations are valid.
7. The user can ask a Grid to set part of its row and column with particular values. Example, setCells(int rows, int cols, int vals), in this method user can specify the indexes of rows and columns that the user wants to set with some particular values (supplied in vals array). Note that, rows, cols, and vals arrays should be of same size.
8. Make another overridden method for setCells(int rows, int cols, int v), that will set particular rows and cols of a grid with a particular value.
Apart from these basic requirements, feel free to add more behaviors to the Grid class that you think appropriate. Please provide explanation for those behaviors as a comment in the source file.
Write a test class to test the behaviours of the Grid class and submit the test class as well.
Note: A grid is composed of many cells. So, you may want to decompose the grid into a Cell class that encapsulates all the necessary behavior of a cell in the grid
Exercise 4 [10 points]:
This exercise asks you to implement a small class that will later serve as part of larger programs. You will implement a new kind of number, a BoundedInteger.
When we write programs to solve problems in the world, we often need to model particular kinds of values, and those values don’t match up with the primitive types in our program language. For example, if we were writing a program to implement a clock or a calendar, then we would need numbers to represent minutes or days. But these numbers aren’t Java integers, which range from -231to 231-1; they range from 0 to 59 and 1 to 31 (or less!), respectively. Java’s base types are useful building blocks, but at the level of atoms, not molecules.
Write a class for representing bounded integers.
A bounded integer takes integer values within a given range. For example, the minutes part of a time takes values in the range [0..59]. The next minute after 59 is 0. The minute before 0 is 59.
We want to be able to create bounded integers in two ways.
1. We specify the object’s lower bound, its upper bound, and its initial value.
2. We specify only the object’s lower bound and its upper bound. The object’s initial value is the same as its lower bound.
Of course, this means that you need to write two constructors.
We would like to be able to use bounded integers in much the same way we use regular ints.
We would like to be able to:
• add an int to a bounded integer.
• subtract an int from it.
• increment and decrement it.
A bounded integer “wraps around” when it does such arithmetic. For example, suppose that we have a minutes bounded integer whose value is 52. Adding 10 to this object gives it a value of 2.
We would like to be able to:
• ask a bounded integer for its value as an int.
• change its value to an arbitrary int.
• print its value out. The way to do this in Java is for the object to respond to toString() message by returning a String that can be printed.
If we try to set a bounded integer to a value outside of its range, the object should keep its current value and print an error message to System.err. or System.out.
We would like to be able to:
• ask one bounded integer if it is equal to another.
• ask one bounded integer if it is less than another.
• ask a bounded integer if a particular int is a legal value for the bounded integer. For example, 23 is a legal value for the minutes bounded integer, but 67 is not.
The answer to each of these questions is true or false.
You may implement these requirements in any order you choose, though you’ll need at least one constructor before you can test any of the others. Write a class to test your code and submit that test class as well.
Some Suggestions for Coding:
To write your program, first write a test that expresses what you would like your code to do, and then write the code to do it. Take small steps, and your tests will give you feedback as soon as possible.
Exercise 5 [15 Points]
For this exercise, you will design a set of classes that work together to simulate a parking officer checking a parked car issuing a parking ticket if there is a parking violation. Here are the classes that need to collaborate:
• The ParkedCar class: This class should simulate a parked car. The car has a make, model, color, license number.
• The ParkingMeter class: This class should simulate a parking meter. The class has three parameters:
− A 5-digit ID that identifies the meter.
− A reference to the car (ParkedCar object) that is currently parked at the meter. If no car is parked, this parameter should be set to null (in this case, make sure to return “Car: none” in your toString method).
− Whether the meter has expired or not.
− The current time (you may simulate the current time).
• The ParkingTicket class: This class should simulate a parking ticket. The class should report:
− The make, model, color and license number of the illegally parked car.
− Meter ID
− The current time.
− The amount of fine, which is $25 if the meter has expired and the current time is between 9 a.m. and 5 p.m. and $10 if the meter has expired and the current time is between 5.01 p.m. and 8.59 a.m.
− The name and badge number of the police officer issuing the ticket (DO NOT store ParkingOfficer object).
• The ParkingOfficer class: This class should simulate a parking officer inspecting parked cars. The class has
− Officer’s name and badge number
− isExpired method that examines the ParkingMeter object and determines whether the meter has expired.
− A method that examines the given parking meter and returns a parking ticket (generates a ParkingTicket object) if the meter has expired. If it is not expired or no car is parked at the meter, return null value.
You may include instance variables and public interfaces in each class as you may find appropriate. Also add appropriate error checks.
Write a client program that demonstrates how these classes collaborate. In addition, you should create a UML diagram for demonstrating relationship among the classes and submit that UML as part of your submission.
Here are some examples of your output. They need not be exactly as shown. Feel free to be creative, but you should test your program for all cases.
Parking Meter: 34521 Time: 8.00 p.m.
No car parked.
Parking Meter: 45673 Time: 4.54 p.m.
Car Parked: Lexus ES350, Black, ABC123
Meter: Not expired
Parking Meter: 98764 Time: 5.01 p.m.
Car Parked: Toyota Camry, Red, EFL786
Parking Ticket Issued
Toyota Camry, Red, EFL786
Meter ID: 98764 Time: 5.01 p.m.
Fine Amount: $10
Issuing Officer: G.Bertrand, IO5674