Exercise 1

Part 1

Aanlyse the following text and sketch out the class diagram. Remember to include class attributes (i.e. variables attached to classes) and methods (i.e., actions that objects may perform).

You are required to produce a program to automate the process of pricing up a decorating job (that is, produce the total cost of a job). Each job consists of a single room, and an hourly rate for the decorator. Each room has a set surface area (square metres), an approximate time to decorate (measured per full hour), and a unit cost for materials. The basic cost of decorating a room is calculated by adding together (a) the result of multiplying the surface area by the unit cost of the materials, and (b) the time (in hours) required to decorate the room, multiplied by the hourly rate of the decorator. (Hint: at this state there is no need to consider inheritance)

Part 2

Check your class diagram against the model answer on Moodle. Using the model answer (or your own answer, if they match!), write a Java console application, Decorator, that will read in an hourly rate, along with a room's area, unit cost and time to decorate, and print out the total cost of the job.

Sample program session:

Please enter hourly rate: 6.50
Please enter room's area: 10
Please enter room's unit cost: 2.5
Please enter room's decorating time: 4
The job will cost 51.0

Interpreted as

(4 hours at 6.50 = 26) + (10 m2 at 2.50 = 25) = 51.

You will need to ensure that your code is divided into an application class Decorator (a public class containing the main() method), a Job class, and a Room class. Ensure that the Job and Room classes contain all the functionality identified in the model answer.

Exercise 2

In this exercise you will use inheritance to implement a new type of room. You should have (from the previous exercise) the following classes in place: Decorator, Job and Room. Create an additional class, TiledRoom, which is a subclass of the Room class (i.e. it extends Room). The TiledRoom has the following additional features, which must be included when the cost of decorating the room is calculated:

  • Grouting (extra materials) must be charged at 1 per unit area.
  • The hourly rate must be increased by 50% to account for the extra work involved.

Here is an example calculation:

Please enter hourly rate: 6
Please enter room's area: 10
Please enter room's unit cost: 4
Please enter room's decorating time: 4
The job will cost 86.0
Cost = (area * unit cost) + time * (hourly rate * 1.5) + grouting
Cost = 40 + (4 * 9) + 10
Cost = 40 + 36 + 10
Cost = 86

Note: The TiledRoom cost calculation method must involve calling CalculateCost in the parent class (Room), since the basic calculation is the same. Think about what you need to pass to CalculateCost() in the parent class.

Replace the Room constructed in the main method with a TiledRoom to test by reproducing the sample calculation above. Note: there is no need to change the Job class. You can pass in a reference to a TiledRoom to the constructor because a TiledRoom is a Room (inheritance represents an 'is a' relationship). This is an example of polymorphism which we will cover in more depth later on. Next week we will extend the Job class to use an array list with a mix of room types to make a multi-room job.

Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.