This question explores objects, classes, methods, messages, selection and iteration You should be able to answer this question once you have completed Units 1–5.
As you complete each part remember to paste the code you have written for it into your Solution Document.
Note – your methods mustnotuse dialogue boxes.
In this question you will write a class calledCarpetCostEstimatorwhich models a cost estimator for a carpet shop supplying and fitting carpet. The class has 3 instance variables:
price,the price of the carpet in £ per square metre as a double,
labourCharge,the labour charge in £ per square metre as a double,
postCode,the customer’s postcode as a String (used to determine a price multiplier for expensive areas).
You will write methods to calculate and display the cost of fitting a carpet, given its width and length and the customer’s postcode.
a.Launch BlueJ, open the projectTMA01_Q1in theTMA01Downloadfolder you unzipped earlier and then immediately save the project asTMA01_Q1_Sol, in theTMA01Downloadfolder. This doesn’t contain any classes yet.
Next create a class calledCarpetCostEstimatorin this project. You will develop this class in the following parts.
In the class that is created, delete the example instance variable, the line of code in the constructor that assigns0to the variable, and the sample method. Also remove any comments related to the variable and the sample method.
Retain the class comment but edit to it include your own name and the date. For the description of the class write ‘Answer to TMA01 Question 1’.
Paste the whole class so far into yourSolution
Document.
(1 mark)
b.In the classCarpetCostEstimator:
- i.Declare the three instance variables using the names and types specified earlier.
- ii.Edit the constructor forCarpetCostEstimatorso that it takes a singledoubleargument and sets the instance variablelabourChargeto the value of this argument, and setspriceandpostCoderespectively to-1.0and“XXX XXX”.
- iii.Write getter and setter methods forprice, labourCharge and postCode.
Compile your class and then create an object of the classCarpetCostEstimatorin the OUWorkspace, passing it the value 4.0 for the labour charge. Test your getter methods by sending an appropriate message to your object and checking the output in the Display Pane. If you are not sure how to do this test please see the first two lines of test code in the project README.TXT file for example test code. In the test code, we used the identifierccefor ourCarpetCostEstimatorobject.
Include a screenshot of the workspace showing your completed tests in yourSolution Document.
(5 marks)
c.In this part you are going to write two instance methods to calculate the purchase cost of the carpet as well as its fitting cost. These methods should beprivateas they are helper methods but you can make thempublicfor the purpose of the question to aid testing.
- i.Write a methodcalculateCarpetCost()that takes twointarguments representing the width and length in metres of the carpet to be purchased and returns the cost of the carpet as adouble. It should first calculate the area of the carpet then multiply this by theprice.
- ii.Write a methodcalculateFittingCost()that takes two int arguments representing the width and length in metres of the carpet to be purchased and returns the fitting cost of the carpet as a double. It should first calculate the area of the carpet then multiply this by thelabourCharge.
(3 marks)
- d.In this part you will write a method that uses thepostCodeto create a multiplier for thelabourChargeto increase this for expensive areas.
- i.Write apublicmethodcostMultiplier()that takes no arguments and returns a double. The method should return 1.2 for postcodes beginning “WC1A” or “EC1A”, and 1.0 otherwise. Don’t forget to test your method in the OUWorkspace using various postcodes.
- ii.ShouldcostMultiplier()bepublicorprivate? Briefly explain your reasoning.
- iii.Amend yourcalculateFittingCost()method from part (c)(ii) so that it multiplies the labour cost by the appropriate cost multiplier.
Important: you will need to useStringmethods such assubstring()for this part. You’ll find these documented on pp. 18 - 21 of your Exam Handbook.
About the Exam Handbook:You are allowed to annotate the Exam Handbook and take it into the exam with you. However, we strongly recommend that you do not begin adding annotations at this point, because you may clutter your handbook up with facts which it turns out later you don’t really need. When it gets nearer to the exam, you will be in a much better position to decide which annotations you are going to find most useful.
(6 marks)
- e.In this part you will write a methodprintCostTable()that prints out a table like the following:
price |
carpet |
fitting |
---|
5.0 |
150.0 |
120.0 |
9.0 |
270.0 |
120.0 |
13.0 |
390.0 |
120.0 |
The method should take fourintarguments representing the width and length of the carpet, a start price and an end price.
The method should first print the headings
It should then print the price, carpet cost and fitting cost for each price starting fromstartPriceand increasing in increments of £4, up to but not exceedingendPrice. For example, assume you have initialised an instance ofCarpetCostEstimator ccewith labour charge £4.0. Then ifstartPriceis £5 andendPrice£15, and you execute.
cce.printCostTable(5, 6, 5, 15);
in the OUWorkspace then three rows of figures should be printed, as shown in the example above.
In order to align the columns of the table accurately, you should begin your method by defining a string that represents a tab character, like this
String tab = "\t";
You can then use this string to align the output in columns. For example,
System.out.println("Rat" + tab + 2020);
System.out.println("Ox" + tab + 2021);
System.out.println("Tiger" + tab + 2022);
will print
(5 marks)
Don’t forget to:
- include yourTMA01_Q1_Solin theTMA01Downloadfolder
- paste the code you wrote for each part of the question into the corresponding part of yourSolution Documentfor TMA01.