Submit Your Question

Answering Assignment Homework Questions

High Quality, Fast Delivery, Plagiarism Free - Just in 3 Steps

Upload Questions Details and Instructions:


Select Assignment Files

24/7 ASSIGNMENT ANSWER



Plagiarism-Free Answers

Assignment solution along with originality report.

Answers From Qualified Tutors

Get assignment answer help by skilled & qualified tutors.

Best Price Guarantee

Friendly pricing & refund policy.

Case Study Help reviews

COMPUTERS CSC1016S Assignment 4: Class Declarations with Encapsulation Solved Assessment Answers

COMPUTERS CSC1016S Assignment 4: Class declarations with Encapsulation

Assignment Instructions

This assignment involves building, testing and debugging Java programs, that create and manipulate composition of classes that model simple types of object; and that employ the full gamut of implementing technologies: fields, constructors, methods and access modifiers.

Get genuine Computer Science Assignment Help and Computer engineering diploma assignment help service from our Certified CS programming Expert tutors – Order Now

Order Now

Scenario

The scenario remains the same as for assignment 3: the simulation of a pay-to-stay car paid. In this assignment we bring it to a conclusion by adding in the missing element: payment.

To recap, the kind of car park in question has a ticket machine at the entrance and a cashier at the exit. A driver, on entering the car park receives a ticket stamped with the arrival time. (The arrival time is also recorded on the magnetic strip on the back.) On exit, the driver gives the ticket to the cashier, the duration of the stay is calculated and from that, how much must be paid.

For this assignment, the focus is on developing a class of “tariff table” that can be used to determine the cost of a stay.

Here is sample I/O from the final program:

Car Park Simulator

The current time is 00:00:00.

Commands: tariffs advance {minutes}, arrive, depart, quit.

>tariffs

[0 minutes  … 30 minutes]: R10.00 [30 minutes  … 1 hour]: R15.00 [1 hour  … 3 hours]: R20.00

[3 hours .. 4 hours] : R30.00

[4 hours .. 5 hours] : R40.00

[5 hours .. 6 hours] : R50.00

[6 hours .. 8 hours] : R60.00

[8 hours .. 10 hours] : R70.00

[10 hours .. 12 hours] : R90.00

[12 hours .. 1 day] : R100.00

>arrive

Ticket issued: Ticket [id=80000001, time=00:00:00].

>advance 20

The current time is 00:20:00.

>arrive

Ticket issued: Ticket [id=80000002, time=00:20:00].

>advance 15

The current time is 00:35:00.

>depart 80000001

Ticket details: Ticket [id=80000001, time=00:00:00]. Current time: 00:35:00.

Duration of stay: 35 minutes. Cost of stay: R15.00.

>advance 6

The current time is 00:41:00.

>depart 80000002

Ticket details: Ticket [id=80000002, time=00:20:00]. Current time: 00:41:00.

Duration of stay: 21 minutes. Cost of stay: R10.00.

>quit

Goodbye.

Items in bold represent user input.

As you can see, where it differs from the previous version is that:

  • There’s a ‘tariff’ command that prints a list of parking
  • When a vehicle departs, the cost of stay is printed after the duration. The following diagram depicts the classes that form the program:

CSC1016S Assignment 4

We have tweaked the Duration and Time classes, and created a new TimeUnit class. You should use these versions for this assignment. (The big change is the addition of some string formatting for Duration.)

The new components are a Time Period class, a Tariff Table class, and a Parking Tariff class. Money and Currency (from assignment 1) make reappearance. The CarParkSim class is modified to accommodate the need to, given the duration of a stay, look up the tariff.

A Time Period is a duration range. It has an inclusive lower bound, , and an exclusive upper bound,

. A time duration, , falls within the range if ≤ < .

For example, a Time Period might be 30 up to (but not including) 90 minutes, where 30 is the inclusive lower bound, and 90 is the exclusive upper bound. Duration of 90 minutes does not fall within the period, duration of 89 minutes does.

A Parking Tariff represents the parking tariff, , for stays that fall within a given time period, .

For example, if you park your car in the Cavendish Square shopping mall (in Claremont, Cape Town), stays from 2 hours up to (but not including) 3 hours cost R15.

A Tariff Table represents a series of parking tariffs in ascending order of time period. For example, (taken from Cavendish Square again)

Time Period Tariff
0 – 2 Hours R13
2 – 3 Hours R15
3 – 4 Hours R20
4 – 5 Hours R30
5 – 6 Hours R50
6 – 7 Hours R70

 

Construction of a Time Period class is the subject of exercise 1. Construction of a Tariff Table class is the subject of exercise 3, and requires the design and construction of a Parking Tariff class, which is the subject of exercise 2. Revising the CarParkSim class is the subject of exercise 3.

For this assignment we expect to see the use of access modifiers to properly implement classes. In this regard, class specifications are properly abstract – they describe a type of object’s attributes and behavior but not how it’s implemented i.e. there are no details of instance variables and how they are manipulated.

The assignment will be part automatically marked and part manually marked. The tutors will review your implementation decisions; your choice of instance variables and data types.

Exercise 1 [30 marks]

Your first task is to develop a Time Period class of object that meets the following specification:

Class Time Period

A Time Period is a Duration range. It has an inclusive lower bound, l, and an exclusive upper bound, u. A Duration, d, falls within the range if l<=d<u.

Constructors

Public Time Period (Duration lower Bound, Duration upper Bound)

// create a Time Period with the given inclusive lower bound and exclusive upper bound.

Methods

Public Duration lower Bound ()

// obtain the lower bound for this time period.

Public Duration upper Bound ()

// obtain the upper bound for this time period.

Public Boolean includes (Duration duration)

// determine whether the given duration falls within this time period i.e. whether

// lower Bound () ≤duration<upper Bound ().

Public Boolean precedes (Time Period other)

// determine whether this time period precedes the other time period i.e. whether

// this. Upper Bound () ≤other. Lower Bound ().

Public Boolean adjacent (Time Period other)

// determine whether this time period is adjacent to the other time period i.e. whether

// this. Upper Bound () is equal to other. Lower Bound (), or Lower Bound () is equal to other. Upper Bound ()

Public String to String ()

// obtain a String representation of this Time Period object in the form:

//”[<duration>… <Duration>]” where durations are given as a series of non-zero quantities of time

// units, the smallest being minutes.

If you look at the details for the proceeds () and adjacent () methods you’ll see that this is a ‘self-referential’ class declaration. Each of these methods, when performed on a Time Period object, accepts a reference to another Time Period objects as a parameter. (The Money, Time and Duration classes that you’ve been using for past assignments also have this characteristic – you’re welcome to examine the code to see exactly how they function.)

Here’s a code snippet to illustrate Time Period behaviour:

//…

Final TimePeriod pOne = new TimePeriod(new Duration(“hour”, 1), new Duration(“hour”, 2));

Final TimePeriod pTwo = new TimePeriod(new Duration(“hour”, 2), new Duration(“hour”, 3));

Final TimePeriod pThree = new TimePeriod(new Duration(“hour”, 3), new Duration(“hour”, 4));

System.out.printf(“%s\n%s\n%s\n”,pOne, pTwo, pThree);

 

System.out.println(pOne.includes(new Duration(“minutes”, 59)));

System.out.println(pOne.includes(new Duration(“minutes”, 60)));

System.out.println(pOne.includes(new Duration(“minutes”, 119)));

System.out.println(pOne.includes(new Duration(“minutes”, 120)));

 

System.out.println(pOne.precedes(pThree));

System.out.println(pTwo.precedes(pOne));

 

System.out.println(pTwo.adjacent(pOne));

System.out.println(pOne.adjacent(pThree));

//…

Here’s the output:

[1 hour  … 2 hours]

[2 hours  … 3 hours]

[3 hours  … 4 hours] false

True ()

True ()

False ()

True ()

False ()

True ()

False ()

NOTE: To implement the toString() method you should use the Duration class format method. The specification is in the appendices. You should use “minute” as the time unit.

How are you to evaluate your work? There are two possibilities: (i) write a test program, or (ii) use the jGrasp interactive feature.

Exercise 2 [20 marks]

Your next task is to develop a Parking Tariff class. The primary objective is actually to develop a

Tariff Table, however, there’s a design issue that must be solved first.

A Tariff Table must store a series of parking tariffs. Each is an association between a time period and a cost. You could implement a Tariff Table by using two arrays, one containing Time Period objects and the other containing Money objects, where the Time Period at index i in the first array is associated with the Money object at index I in the second array.

That’s not a particularly nice solution since there’s nothing in the resulting variable declarations to indicate the relationship, and there’s potential for mismatch. A better solution is to have a Tariff Table contain a collection of Parking Tariff objects, where each Parking Tariff object stores a Time Period and Money.

Develop a Parking Tariff class of object.

  • Your class will have instance variable(s), constructor(s), and method(s).
  • You should aim to move beyond simple get and set methods to ones that offer greater functionality.

With respect to the second point, you may wish to develop the Parking Tariff class in conjunction with the Tariff Table class (exercise 3):

  • Think about what a Tariff Table object must
  • Think about what could be delegated to Parking Tariff

objects your solution will be manually marked by the tutors.

Exercise 3 [30 marks]

Develop a TariffTable class that meets the following specification:

Class Tariff Table

A Tariff Table records parking tariffs for a pay-to-stay car park.

Constructors

Public Tariff Table (in max Size)

// create a Tariff Table with the given maximum number of entries.

Methods

Public void add Tariff (Time Period, Money tariff)

// add the tariff for the given period to the table. The period must directly follow, and be

// adjacent to, that for the previous tariff entered.

// If the period does not follow or is not adjacent then an Illegal Argument Exception is thrown.

Public Money get Tariff (Duration length Of Stay)

// obtain the tariff for the given length of stay.

Public String toString()

// obtain a String representation of this Tariff Table in the form:

// <period0> : <tariff0>

// …

// <periodn> : <tariffn>

 

Here’s a snippet of code to illustrate behaviour:

//…

final Currency currency = new Currency(“R”, “ZAR”, 100);

final TimePeriod pOne = new TimePeriod(new Duration(“hour”, 1), new Duration(“hour”, 2));

final TimePeriod pTwo = new TimePeriod(new Duration(“hour”, 2), new Duration(“hour”, 3));

final TariffTable tariffTable = new TariffTable(2); tariffTable.addTariff(pOne, new Money(“R2”, currency)); tariffTable.addTariff(pTwo, new Money(“R5”, currency));

System. out. print in (tariff Table);

System. out. print in (tariff Table. Get Tariff(new Duration(“minute”, 65)));

System. Out. print in (tariff Table. get Tariff(new Duration(“hour”, 2)));

//….

The output from the fragment would be:

 

[60…120 minutes]: R2.00

[120…180 minutes]: R5.00

R2.00

R5.00

 

NOTE:

  • The tostring() method must return a string containing a series of lines, one for each parking tariff. All but the last line must end with a newline character ‘\n’.
  • The add Tariff() method must check that the period, pn, for the new tariff follows on from the previous one, pp entered i.e. that p0 precedes and is adjacent to pn. If this condition is not met then an exception should be thrown. Here’ s the expression your code should use:

Throw new Illegal Argument Exception (“Time Period: add Tariff (): precondition not met.”);

Evaluate your work: by (i) writing a test program (something like that required for exercise 1 of assignment 1), or (ii) using the jGrasp interactive feature.

Exercise 4 [20 marks]

The Car Park Sim class contains the main program method. It creates the Register, Clock and Tariff Table objects, and handles user input/output.

Extending the Car Par Sim solution that you constructed for the previous assignment, you need to:

  • Add code to create and populate a Tariff Table object.
  • Extend the code for the ‘depart’ command to include printing the tariff that applies to a stay of that duration.
  • Add code for an additional ‘tariffs’ command: When the user enters the ‘tariffs’ command, a list of parking tariffs will be printed.

List of parking tariffs will be printed.

Here are some more samples I/O:

Car Park Simulator

The current time is 00:00:00.

Commands: tariffs advance {minutes}, arrive, depart, quit.

>advance 10

The current time is 00:10:00.

>arrive

Ticket issued: Ticket [id=80000001, time=00:10:00].

>advance 1

The current time is 00:11:00.

>arrive

Ticket issued: Ticket [id=80000002, time=00:11:00].

>arrive

Ticket issued: Ticket [id=80000003, time=00:11:00].

>depart 80000003

Ticket details: Ticket [id=80000003, time=00:11:00]. Current time: 00:11:00.

Duration of stay: 0 minutes.

Cost of stay: R10.00.

>depart 80000006

Invalid ticket ID.

>depart 80000001

Ticket details: Ticket [id=80000001, time=00:10:00]. Current time: 00:11:00.

Duration of stay: 1 minute. Cost of stay: R10.00.

>tariffs

[0 minutes  … 30 minutes]:

R10.00 [30 minutes  … 1 hour] :

R15.00  [1 hour .. 3 hours] :

R20.00

[3 hours .. 4 hours] : R30.00

[4 hours .. 5 hours] : R40.00

[5 hours .. 6 hours] : R50.00

[6 hours .. 8 hours] : R60.00

[8 hours .. 10 hours] : R70.00

[10 hours .. 12 hours] : R90.00

[12 hours .. 1 day] : R100.00

>quit

Goodbye.

Submission

Submit the TimePeriod.java, ParkingTariff.java, TariffTable.java and completed CarParkSim.java files to the automatic marker.

Appendices

Class Money

An object of this class represents an amount of money in a particular currency. Amounts can be added and subtracted. The amount is stored as a quantity of the minor unit of the currency e.g. 1 Rand will be stored as 100 cents.

Constructors

Public Money (String amount, Currency)

// create a Money object that represents the given amount of the given currency.

// The String is assumed to have the following format: <currency symbol><quantity of

// units>.<quantity of minor units> e.g. in the case of USD, $50.30, $0.34.

Methods

Public Money add (Money other)

// add the other amount of money to this amount and return the result. The objects must be of the

// same currency.

Public String toString ()

// obtain a string representation of the monetary value represented by this object e.g. “€45.10” for

// a Money object that represents 45 Euros and 10 cents.

 

Class Currency

An object of this class represents a Currency such as US Dollars or British Pound Stirling.

A currency has an ISO 4217 currency code and a symbol denoting the currency’s major unit. Many currencies have a minor (or fractional) unit such as the cent in the case of US dollars.

It is assumed that the currency symbol always appears in front of an amount; that negative amounts are

represented by a minus sign, ‘-‘, that precedes the currency symbol, “-£34.50” for example; that the decimal point is always represented using a full stop; that no attempt is made to group the digits of large quantities, so for example, one million Rand is assumed to be represented as “R1000000” (as opposed to “R1,000,000”).

Constructors

Public Currency (String symbol, String code, in minor Per Major)

// create a Currency object that represents the currency with the given unit symbol (e.g. “£” for

// Sterling), ISO 4217 code, and number of minor units per major units (e.g. 100 in the case of

// pennies per British Pound).

 

Class Duration

A Duration object represents a length of time (with millisecond accuracy).

Constructors

Public Duration (String time Unit, long quantity)

// create a Duration object that represents the given quantity of the given time unit.

// Permissible time units are: “millisecond”, “second”, “minute”, “hour”, “day” ,”week”.

Methods

Public in compare To (Duration other)

// Returns a negative, zero, or positive value, depending on whether this duration is smaller, equal

// to, or greater than the other duration.

Public Boolean equals (Object o)

// determine whether object o is equivalent to this object i.e. if it is a Duration and of the same

// value as this Duration.

 

Public static String format(final Duration, final String smallest Unit)

// obtain a formatted string that expresses the given duration as a series of no-zero quantities of

// the given time units.

// For example: Given Duration d=new Duration (“second”, 88893);, the expression

// Duration (d, “second”) returns the string “1 day 41 minutes 33 seconds”, while

// Duration (d, “minute”) returns the string “1 day 41 minutes”.

Get This Answer for Study Help

If you need study assistance with writing your questions and answers, our professional assignment writing service is here to help!

PLACE YOUR ORDER HERE

Content Removal Request

If you are the original writer or copyright-authorized owner of this article and no longer wish to have, your work published on casestudyhelp.com, then please Request for removal of this content.

Top