computer science

chloecce
COMP_.pdf

COMP 1230

Lab 3 (OOP Design)

Total Points: 50

Due: Wednesday, May 30, 2018 by Midnight

In this lab, you will model some of the real-world objects into digital objects using

Java. You will also practice some important concepts of OOP, Encapsulation and

Modularity. They are two very important concepts in OOP, which also help hiding all

but the relevant data about an object to reduce complexity and increase efficiency.

Submission Instruction:

For each exercise, please test your code by running it with at least three different

test cases by changing the input values. Take a snap-shot (screen capture) of your

test runs and save them for submission. Please put all the source codes and snap-

shots (screen captures) of your test runs in a folder. Name the folder as

Lab3_FName (replace the FName with your first name) and zip it. Submit the zipped

folder through the moodle.tru.ca.

Exercise 1 [10 Marks]

In this exercise, you need to complete the “SnakeEye” game that we have

developed in the class for two human players. I have uploaded the completed

source codes on the moodle.tru.ca. You must study the codes and modify the

program that will allow a human player to play the game against a computer player.

Make sure that the computer-player releases its turn to the human player when it’s

score goes more than 20 for that particular turn. The other rules of the game are

unchanged.

Note: please write comments on the code and highlight which portion of the

source code you have updated to accommodate the change in the game.

Exercise 2 [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 8x8

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.

0

1

2

3

4

5

6

7

0 1 2 3 4 5 6 7

Each box

is a “cell”

Tasks

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 3 [10 points]:

Introduction

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.

Tasks

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.

Object creation

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.

Arithmetic operations

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.

Value operations

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.

Comparison operations

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 4 [20 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

Meter: Expired

Parking Ticket Issued

Toyota Camry, Red, EFL786

Meter ID: 98764

Time: 5.01 p.m.

Fine Amount: $10

Issuing Officer: G.Bertrand, IO5674