Date | November 2017 | Marks available | 2 | Reference code | 17N.2.SL.TZ0.10 |
Level | SL | Paper | 2 | Time zone | no time zone |
Command term | State | Question number | 10 | Adapted from | N/A |
Question
A restaurant uses an object-oriented program to manage the cost of the food and drink consumed by customers. Everytime a table is occupied a Payment
object is instantiated which will contain details of the items ordered. As each item is ordered, a FoodItem
or a DrinkItem
object is added to the Payment
object as appropriate.
public class Payment
{
private FoodItem[] fi = new FoodItem[100];
private int fiCount;
private static double foodTax = 0.2; // 20 % sales tax added to
// all food prices
private DrinkItem[] di = new DrinkItem[100];
private int diCount;
private static double drinkTax = 0.1; // 10 % sales tax added to
// all drink prices
public Payment()
{
fiCount = 0;
diCount = 0;
}
public DrinkItem getDi(int x)
{
return di[x];
}
// all other accessor and mutator methods are included
// addFoodItem() – this method adds a new FoodItem object
// addDrinkItem() – this method adds a new DrinkItem object
public static double findPrice(Item[] pl, String c)
{ //code missing }
// calculateBill() – This method returns the bill (the total value of
// the items consumed for a particular table)
}
public class FoodItem
{
private String itemCode;
private int quantity;
public FoodItem(String x, int y)
{
itemCode = x;
quantity = y;
}
// all accessor and mutator methods are included
}
The DrinkItem class is defined in a similar way.
Whenever a object is instantiated, the variables and are initialized to 0 through the code in the constructor.
Outline an alternative method of initializing these variables that would not require the use of the code in the constructor.
State the implication of the use of the term static
in the Payment
class.
With reference to two examples from the classes in the resource, explain the benefits gained by the use of different data types.
Describe the purpose of the following statement:private FoodItem[] fi = new FoodItem[100]
The Payment
class method addFoodItem()
is passed a FoodItem
object as a parameter.
Construct the method addFoodItem()
.
Markscheme
Award [1] for identifying the code and [1] for identifying the new position.private int fiCount = 0
and private int diCount = 0
;
In the variable/attribute section of the class and not in the constructor);
EITHER
The values of these (static) variables;
Are the same for all objects;
They belong to the class (not to the objects);
And are only created/declared once;
Note: Do not accept “cannot be changed”.
OR
The static methods;
Are class methods (not object methods);
Are independent of the objects in the class;
Award [2 × 2] for each clear example. Only award [2] for an example if the benefit is clearly shown;
Having different data types allow different operations to be carried out depending upon the type;
e.g.
Calculations in the integer “quantity” variable;
which are not possible for a String;
Double instead of integer allows the use of decimals;
which mirrors real-life scenarios/allows more precise calculations;
Using an array (of objects);
Allows individual items to be accessed/processed more easily;
Memory usage can be reduced;
e.g. “fiCount
” takes up less space as an integer than it would as a double (assuming it’s just a number);
The variable fi
is declared as an array of FoodItem
objects / of type FoodItem
;
With (a maximum of) 100 values;
It cannot be directly accessed/it is encapsulated within the Payment class;
Award [1] for each correct line of code.
public void addFoodItem(FoodItem f)
{
fi[fiCount] = f;
fiCount++;
}