Semester Test 4 PAPER 2
Semester Test 4 PAPER 2
Student Number
⬚
= _____%
70
Surname Initials
Instructions:
Read instructions at the beginning of each question.
All questions must be answered on the Electronic Campus(EC) platform.
Scientific, non-programmable calculators are allowed.
Cellular Phones are not allowed.
No sharing of calculators and/or stationery.
Round decimal answers to 2 decimal places.
BROWSE THROUGH THE WHOLE DOCUMENT BEFORE YOU START.
1
PPAFO5D/TROF05D SEMESTER TEST 4 PAPER A OCTOBER 2024
Please answer this question directly into the provided space on EC.
Use the variable names provided. You may use intermediate variables.
1.1 What type of loop is guaranteed to run at least once, regardless of the condition? (1)
1.2 What type of loop would you use if you knew exactly how many times you wanted to
iterate? (1)
int iTotal = 0;
for (int i = 11; i <= 43; i+=3)
{
iTotal += i;
}
1.4 What are the three essential elements that govern the loop behaviour in iteration control
structures? (3)
1.5 What is the difference between a pre-test loop and a post-test loop? (2)
1.6 Write a for loop that will print “Tshwala bami, tshwala bami” eight times. (4)
1.7 Write a any loop that prints one character at a time as it iterates, following this pattern :
@_@_@_@_@_
The pattern should consist of alternating @ and _ characters, with a total of 10 characters, all
printed on the same line. (7)
Hint: If loop control variable is even, print @; if odd, print _.
2
PPAFO5D/TROF05D SEMESTER TEST 4 PAPER A OCTOBER 2024
Convert the given flowchart in figure 1, into a Java application class called Factors and save
it as Factors.java.
3
PPAFO5D/TROF05D SEMESTER TEST 4 PAPER A OCTOBER 2024
Write a Java class (AverageCalculator) that calculates the average quiz score of a student. It
allows the user to enter multiple quiz marks, one at a time. The program terminates when the
user enters -1 as the mark.
Input:
• Quiz marks as integers, entered one at a time.
• Enter -1 to stop the input process.
• You can assume the user will always enter a positive integer as a quiz mark.
Output:
• If the user enters at least one valid mark (other than -1), the program calculates and
displays the average mark. (Figure 3)
• If the user enters only -1 (no valid marks), the program displays a message indicating
no marks were entered. (Figure 4)
4
PPAFO5D/TROF05D SEMESTER TEST 4 PAPER A OCTOBER 2024
• Download AirCargoCostEstimator.txt.
• Open the file in Notepad++ and save it as AirCargoCostEstimator.java.
• Add the missing logic based on the statements below.
• Once you are done, copy and paste it into the provided space in EC.
Background
The goal of the program is to calculate the air cargo cost based on the following criteria:
The existing code includes comments that indicate where you need to fill in the missing
implementations. Below are the key aspects you should consider while completing the code.
Task:
Download the partially written Java code (AirCargoCostEstimator) OR copy the code at
the end of the question, and complete the following tasks:
1. Inputs:
• The program should capture user inputs for package weight, distance to be travelled,
and cargo type.
2. Calculations:
• The base cost should be calculated based on the weight using the following tiered
rates:
5
PPAFO5D/TROF05D SEMESTER TEST 4 PAPER A OCTOBER 2024
• If the distance exceeds 500 kilometres, calculate a surcharge of R5.50 per kilometre.
• Calculate the total estimated cost, which includes base cost, multiplier, distance
surcharge and the fixed additional charges for handling fees and fuel surcharges,
set at R450.00.
3. Outputs:
• The program should display a detailed summary of the air cargo cost estimation,
including: (Figures 5 – 7)
6
PPAFO5D/TROF05D SEMESTER TEST 4 PAPER A OCTOBER 2024
7
PPAFO5D/TROF05D SEMESTER TEST 4 PAPER A OCTOBER 2024
8
PPAFO5D/TROF05D SEMESTER TEST 4 PAPER A OCTOBER 2024
Partial completed AirCargoCostEstimator class
import java.text.DecimalFormat;
import java.util.Scanner;
do
{
System.out.println("\n\n\tAir Cargo Cost Estimation Calculator\n");
9
PPAFO5D/TROF05D SEMESTER TEST 4 PAPER A OCTOBER 2024
// Multiplier based on cargo type --- ADD CODE HERE---
switch ( )
{
default:
System.out.println("Invalid cargo type. Please enter 'normal', 'perishable', or 'dangerous'.");
continue;
}
// Ask the user if they want to sRepeat the process for another package --- ADD CODE HERE---