Question
The management of the company will launch a new scheme to give every 50 th car driver and every 60 th motorcyclist a free coffee voucher. The code for printing this voucher has already been created and is activated by calling the static method vouchers.printcoffeevoucher ().
A getkind () method has already been added to the vehicle class, which returns a char value indicating whether it is a car (c) or a motorbike (m).
One test performed on the finished code was defined as follows:
a. Describe, without writing code, any changes required to the addvehicle method and the ParkingArea class to make the new voucher scheme[5]
work.
b. Identify three other tests you might perform on the completed code to prove that it functions correctly.[3]
c. The removevehicle method of the ParkingArea class searches in the array for a vehicle object with a specified registration plate, then removes[6]
it by setting that array index to null.
The method returns a reference to the vehicle object that has been removed from the array, or null if no matching registration plate was found.
Construct the removevehicle method.
▶️Answer/Explanation
Ans:
a. )
Award [1] for declaring both the variable for total cars and the variable for total motorbikes at class level (private);
Award [1] for initializing the variables, either by calling a mutator method, while declaring, or in the constructor;
Award [1] for deciding whether the car or motorbike is 50th or 60th in the daily additions;
Award [1] for printing the message if so;
Award [1] for incrementing the appropriate counter for the total cars / motorbikes added based on the value returned by getkind ();
Note: Do not award marks for simply adding mutator or accessor methods for the motorbike / car totals.
b. )
Car:
Award [1] for Car count mod 50 is 0, coffee voucher;
Award [1] for Car count mod 50 is $n, n$ coffee vouchers;
Motorbike:
Award [1] for Motorbike count mod 60 is not 0 , No coffee vouchers;
Award [1] for Motorbike count mod 60 is $n, n$ coffee voucher;
Non Car/Motorbike
Award [1] for Any amount of instances of the Vehicle class which are neither Car nor Motorbike, No coffee voucher
Note: do not award a mark for first test case above which is already give in question stem
Examples:
c.)
Award [1] for correct loop;
Award [1] for checking if not null;
Award [1] for checking registration using the getRegistration().equals() method;
Award [1] for setting vehicles[i] to null;
Award [1] for returning the reference (not the null element of the array), by making another variable;
Award [1] for returning null if it is not found;
Example 1:
public Vehicle removeVehicle(String registration) {
for (int i = 0; i < vehicles.length; i++) {
if (vehicles[i] != null &&vehicles[i].getRegistration().equals(registration)) { //question – why
do it in this order?
Vehicle leaving = vehicles[i];
vehicles[i] = null;
return leaving;
}
}
return null;
}
Example 2:
public Vehicle removeVehicle(String registration)
{ Vehicle v = null;
boolean found = false;
int i = 0;
while (!found && i < Vehicles.length)
{ found = Vehicles[i] != null &&
registration.equals(Vehicles[i].getRegistration());
if (found)
{ v = Vehicles[i];
Vehicles[i] = null;
}
else i = i + 1;
}
return v;
}
Question
An Event has been instantiated with 2 qualifying heats for a total of 11 swimmers.
Event free100 = new Event $(” 100 \mathrm{~m}$ free style”,2);
The swimmers were added to the two Race arrays and after the races, their times were recorded as shown in the table.
(For the purpose of this question, the name represents the full swimmer object.)
The method fillfinals () will select the 8 fastest swimmers, in ascending order of time, from both swimmer arrays and copy them to the swimmer array in the finals race.
To help with this selection, all entries from races [0] and races [1] will be copied into two new parallel arrays of size 16, one array for swimmers and one array for their times.
The two temporary arrays will be sorted using the following code.
int i,j;
Swimmer swapSwimmer;
double swapTime;
for(i = 0; i < 15; i++)
{
for(j = 0; j < 15; j++)
{
if(tempTime[j] > tempTime[j + 1]) // if wrong order then…
{
swapSwimmer = tempSwimmer[j]; // swap the swimmer and…
tempSwimmer[j] = tempSwimmer[j + 1];
tempSwimmer[j + 1] = swapSwimmer;
swapTime = tempTime[j]; // swap the time
tempTime[j] = tempTime[j + 1];
tempTime[j + 1] = swapTime;
}
}
}
a. Sketch the resulting swimmer array in finals.[3]
b. Construct the code fragment for the given situation that will copy swimmers and times into two parallel arrays named tempswimmer and temptime.
c.i. State the name of this sorting algorithm.
c.ii.Outline two improvements to this code that would make the algorithm more efficient.$[4]$
d. Construct the code fragment that will copy the names of the 8 fastest swimmers in ascending order of time from the array tempswimmer to the $[6]$
array swimers in the race finals.
▶️Answer/Explanation
Ans:
Award [1] for array of 8 slots with array name.
Award [1] for correct entries (in any order).
Award [1] for correct order.
b. )
Award [1] for correctly declaring 2 arrays of size 16.
Award [1] for correct outer loop.
Award [1] for correct inner loop.
Award [1] for copying swimmer object.
Award [1] for copying time.
Award [1] for incrementing the index of the new arrays.
Example answers:
Swimmer[] tempSwim = new Swimmer[16];
double[] tempTime = new double[16];
int newIndex = 0;
for(int i = 0; i < 2; i++){
for(int j = 0; j < 8; j++){
tempSwim.swimmer[newIndex] = races[i].swimmer[j];
tempTime.time[newIndex] = races[i].time[j];
newIndex++;
}
}
Note: that the question asks for all entries to be copied. However, do not penalize “efficient” solutions that avoid copying the null and 0 entries.
c.i. )
Bubblesort;
c.ii. )
Award [1] for stating an improvement and [1] for an elaboration up to [2 max].
Mark as [2] and [2].
Include a flag “swapped”;
That can help stop the outer loop if there is a pass through the inner loop with no swap;
Limit the inner loop by deducting the outer loop counter;
So that the sorted elements are no longer compared;
d. )
Award [1] for correctly instantiating variables.
Award [1] for correct loop condition (count $<8$ ).
Award [1] for checking for a 0 time (or null entry).
Award [1] for correctly copying.
Award [1] for incrementing count in the right place.
Award [1] for incrementing $k$ in the right place.
Example answers:
int k = 0;
int count = 0;
while(count < 8) && (k < 16){
if(tempSwim[k] != 0){
finals.swimmer[count] = tempSwim[k];
count++;
}
k++;
}
Note: Do not penalize for not including loop condition (k < 16)