Date | November 2017 | Marks available | 1 | Reference code | 17N.2.SL.TZ0.11 |
Level | SL | Paper | 2 | Time zone | no time zone |
Command term | Construct | Question number | 11 | Adapted from | N/A |
Question
The global variable tables
is declared as follows:
Payment[] tables = new Payment[50]
;
The indices in this array represent the table number, so tables[1]
is a Payment
object for the customers occupying table number 1.
The driver (main) class contains the following code. Note: You can assume that all appropriate accessor and mutator methods have been included in their respective classes.
tables[1] = new Payment();
tables[2] = new Payment();
FoodItem a = new FoodItem("f102", 2);
FoodItem b = new FoodItem("f100", 1);
DrinkItem c = new DrinkItem("d102", 3);
tables[1].addFoodItem(a);
tables[1].addFoodItem(b);
tables[2].addDrinkItem(c);
tables[2].addDrinkItem(new DrinkItem("d103",1));
System.out.println(tables[1].getFiCount());
System.out.println(Payment.getFoodTax());
System.out.println(tables[2].getDi(1).getItemCode());
The price of each item is stored in an object of the Item
class.
The class is outlined below:
public class Item
{
private String code; // item code
private String name; // item name
private double price; // unit price before tax
// all accessor, mutator and constructor methods are included
}
All of the objects in this class are held in the global array pl
according to the following declaration:
Item[] pl = new Item[200]
;
Note: The number of objects held in this array will change from week to week.
The method findPrice(Item[] pl, String c)
in the Payment
class looks up and returns the price of the item with code c
.
State the output after this code is executed.
Construct statements, in code, that will print out the following: The number of drink items ordered by table 40.
Construct statements, in code, that will print out the following: The item code of the third food item ordered by table 2.
Construct the method findPrice()
. You may assume that the item exists in the array.
When a customer wishes to pay the bill, the method is called. If the bill was for table 10 then the following call would be made:
double finalBill = tables[10].calculateBill(Item[] pl)
;
Construct the calculateBill()
method. You should make use of any previously defined methods.
Markscheme
Award [1] for each correct value.
2;
0.2;
d103;
System.out.println(tables[40].getDiCount())
;
Allow variations of the get
method name
Note: Ignore minor syntax errors.
System.out.println(tables[2].getFi(2).getItemCode());
Allow variations of the get
method name
Note: Ignore minor syntax errors.
Award marks as follows up to [6 max].
Award [1] for correct initialization.
Award [2] for loop that checks entries, but has early exit.
Award [1] for a loop that checks all 200 entries with no early exit.
Award [1] for correct assignment.
Award [1] for correct comparison.
Award [1] for correct return value.
Award [1] for both the assignment and comparison IF get
methods are not used.
public static double findPrice(Item[] pl, String c)
{
int x = 0;
double price = 0.0;
boolean found = false;
while(!found)
{
if ((pl[x].getCode()) == c)
{
price = pl[x].getPrice();
found = true;
}
x++;
}
return price;
}
Award marks as follows up to [7 max].
Award [1] for correct method header.
Award [1] for correct initialization.
Award [1] for correct loop.
Award [1] for finding item code.
Award [1] for use of findPrice
method.
Award [2] for the calculation if completely correct, award only [1] if taxes are wrong and/or no use of accessor.
Award [1] for consideration of both food and drink objects (whether correct or not);
public double calculateBill(Item[] pl)
{
double total = 0.0;
for(int x = 0; x < fiCount; x++)
{
String c = fi[x].getItemCode();
double price = findPrice(pl,c);
total = total + fi[x].getQuantity()*price*(1 + foodTax);
}
for(int y = 0; y < diCount; y++)
{
String c = di[y].getItemCode();
double price = findPrice(pl,c);
total = total + di[y].getQuantity()*price*(1 + drinkTax);
}
return total;
}