0% found this document useful (0 votes)
53 views10 pages

Semester Test 4 PAPER 2

Uploaded by

ntandogift63
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
53 views10 pages

Semester Test 4 PAPER 2

Uploaded by

ntandogift63
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

PPAFO5D/TROF05D SEMESTER TEST 4 PAPER A OCTOBER 2024

Faculty of Information and Communication Technology


ICT First Years and Foundation Unit

Principles of Programming and


Introduction to Programming
(Extended) (Year 1)

PPAF05D & TROF05D


SEMESTER TEST 4
PAPER A
I declare that I am familiar
10 October 2024 Examiners: VL Baloyi
with, and will abide to the
KS Tshehla
Examination rules of Tshwane
Duration: 3 Hours Moderator: TA Maseko
University of Technology
Total: 70
Full Marks: 70 Number on Class List
Number of Pages: 10
_____________________
Signature GROUP

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

Question 1 (Short Questions) [20]

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)

1.3 How many times will this loop iterate/repeat? (2)

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

Question 2 (Convert flowchart to Java program) [10]

• Create a java program in Notepad++ based on the flowchart below.


• Once you are done, copy and paste it into the provided space in EC.

Convert the given flowchart in figure 1, into a Java application class called Factors and save
it as Factors.java.

Figure 1: The factors calculator flowchart

Figure 2: Example output showing factors of any number

3
PPAFO5D/TROF05D SEMESTER TEST 4 PAPER A OCTOBER 2024

Question 3 (Problem solving with Java programs) [15]

• Create a java program in Notepad++ based on the statements below.


• Once you are done, copy and paste it into the provided space in EC.

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)

Figure 3: Average calculated for valid inputs

Figure 4: Output when no marks were entered

4
PPAFO5D/TROF05D SEMESTER TEST 4 PAPER A OCTOBER 2024

Question 4 (Problem solving with Java programs) [25]

• 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.

Interview Question: Air Cargo Cost Estimator


As a student who is about to complete the PPA/TRO module, you applied for a scholarship. As part of
the application process for a scholarship opportunity, you are presented with a partially written Java
program designed to estimate the cost of shipping air cargo. Your task is to analyse the provided code
and complete it by using the given variables without changing any of them. You may, however,
introduce new variables as needed.

Background
The goal of the program is to calculate the air cargo cost based on the following criteria:

• Package weight (in kilograms)


• Distance to be travelled (in kilometres)
• Cargo type (normal, perishable, dangerous)

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:

Package size Weight range Rate


Small packages less than 10 kg R 23.45 per kg
Larger shipments 10 kg and 100 kg R 18.10 per kg
Bulk shipments over 100 kg R 10.55 per kg

• Select a multiplier based on the risk on the cargo type:

Cargo Type Multiplier


Normal 1.0
Perishable 1.2
Dangerous 1.5

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)

Package weight, Distance to be travelled, Cargo type,


Base cost, Cargo type multiplier, Distance surcharge,
Additional charges, Total estimated air cargo cost.
4. Looping Mechanism:
• After displaying the summary, the program should ask the user if they wish to
perform another calculation. (Figure 8)
• Complete the loop condition to repeat the process if they answer "yes."

Program Functionality Demonstration:


Figure 5: Small package, with multiplier and no distance surcharge.

6
PPAFO5D/TROF05D SEMESTER TEST 4 PAPER A OCTOBER 2024

Figure 6: Larger shipment, with multiplier and distance surcharge.

Figure 7: Bulk shipment, no multiplier and distance surcharge.

7
PPAFO5D/TROF05D SEMESTER TEST 4 PAPER A OCTOBER 2024

Figure 8: Repeat Option Prompt

Figure 9: Successful Program Exit

8
PPAFO5D/TROF05D SEMESTER TEST 4 PAPER A OCTOBER 2024
Partial completed AirCargoCostEstimator class

import java.text.DecimalFormat;
import java.util.Scanner;

public class AirCargoCostEstimator


{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
DecimalFormat currencyFormat = new DecimalFormat( ); //--- ADD CODE HERE---

// Fixed additional charge for handling fees and fuel surcharges


final double ADDITIONAL_CHARGES = 450.00;

String sRepeat = "yes", sCargoType;

double rBaseCost = 0, rTypeMultiplier = 1, rDistanceSurcharge;


double rTotalCost, rWeight, rDistance;

do
{
System.out.println("\n\n\tAir Cargo Cost Estimation Calculator\n");

// Input the package weight


System.out.print("Enter package weight (in kg): ");
rWeight = input.nextDouble();

// Input the package distance


System.out.print("Enter distance to be traveled (in km): ");
rDistance = input.nextDouble();

// Input the package cargo type --- ADD CODE HERE---

// Base cost calculation based on weight --- ADD CODE HERE---

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;
}

// Calculate distance surcharge if applicable --- ADD CODE HERE---

// Calculate the total estimated cost --- ADD CODE HERE---

// Output the detailed summary


System.out.println("\nAir Cargo Cost Estimation Summary:");
System.out.println("----------------------------------------------");
System.out.println("Package Weight\t\t: " + rWeight + " kg");
System.out.println("Distance to be Traveled\t: " + rDistance + " km");
System.out.println("Cargo Type\t\t: " + sCargoType.substring(0, 1).toUpperCase() + sCargoType.substring(1));
System.out.println("Base Cost\t\t: " + currencyFormat.format(rBaseCost));
System.out.println("Cargo Type Multiplier\t: " + rTypeMultiplier);
System.out.println("Distance Surcharge\t: " + currencyFormat.format(rDistanceSurcharge));
System.out.println("Additional Charges (Handling & Fuel): " + currencyFormat.format(ADDITIONAL_CHARGES));
System.out.println("----------------------------------------------");
System.out.println("Total Estimated Air Cargo Cost: " + currencyFormat.format( )); // --- ADD CODE HERE---
System.out.println("----------------------------------------------");

// Ask the user if they want to sRepeat the process for another package --- ADD CODE HERE---

}while( );// --- ADD CODE HERE---

System.out.println("\n\t---Thank you for using the Air Cargo Cost Estimator!---");


}
}
10

You might also like