PPA115D-TRO115D Issue 2 Umsebenzi - LOD

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 7

Masisebenze “Let’s work” Issue 2: Learning unit D.1 – Learning unit D.

2 2024 S2

Purpose:
• To prepare students with understanding on how to use selection control structure in a
program.

Section A: Short Question

Question 1: (Simple if, If-else, Nested if statement)


1.1 Rewrite only the relational expression to express the following conditions.

eg. A child’s age may not exceed 11. // age is the variable that you will use
If age <= 11 then
If age < 12 then
a. A person’s salary is equal to R30 000
b. A person’s age is 35 and above as well as the height
c. Months of the year may not exceed 12
d. Current year must not be equal -1

1.2 Write only the suitable if statement in pseudocode for the following conditions.
Make use of the variable names as provided.
eg. A child’s age may not exceed 12 years, display “You are a child”; else “You are
an adult” . // age is the variable that you will use
If age <= 11 then
Display “ You are a child”
Else
Display “ You not a child”
End
or
If age < 12 then
Display “ You are a child”
Else
Display “ You not a child”
End
PPA115D/TRO115D 2022 S1 - Umsebenzi Issue 1
1
a. If an angle is equal to 180 degrees, display the message “The angle is a straight
angle”; else display the message “The angle is not a straight angle”.
b. If the difference between weight1 and wieght2 exceeds 9.5 kg calculate the mass
of both dogs.
c. If the value p is greater than the sum of q and r and s is less than 20, then prompt
the user for the t value.
d. If money is left in particular bank for more than 8 years, the bank pays interest at
a rate of 4.5%, otherwise, the interest rate is 3.0%.

1.3 Re-write the following nested if statements using logical operators.


1.3.1 if (hoursWorked > 40)
{
if (daysWorked == 7)
System.out.println (“You have worked overtime”);
else
System.out.println (“You have not worked overtime”);
}

1.3.2 if (gender == ‘F’ )


{
System.out.println (“Female”);
}
else
{
if (gender == ‘f’ )
System.out.println (“Female”);
else (if gender == ‘M’)
System.out.println (“Male”);
else (if gender == ‘m’ )
System.out.println (“Male”);
else
System.out.println (“Invalid gender”);
}

PPA115D/TRO115D 2022 S1 - Umsebenzi Issue 1


2
Question 2 (Expression, if else, Nested if)

2.1 Deterimine the value of the following expressions, assuming x = 10, y = 4, z = 8, a= 12, b
= 3:
Pseudocode Expression Java expression Value
x>y
x <> b
A mod y = z mod y
x=y

2.2 Write only the if statement for the following:

The interest rate paid on funds deposited in a bank is determined by the amount of time
the money is left on deposit.

Time on deposit Interest rate


Greater than or equal to 5 years 0.040
Less than 5 years but greater than or equal to 4 years 0.035
Less than 4 years but greater than or equal to 3 years 0.030
Less than 3 years but greater than or equal to 2 years 0.025
Less than 2 years but greater than or equal to 1 year 0.020
Less than 1 0.15

PPA115D/TRO115D 2022 S1 - Umsebenzi Issue 1


3
Question 3

Write a Java solution for Question 2.2.


Do all the necessary planning to solve this problem:
3.1 Create an IPO chart
3.2 Design for your program using a flow chart
3.3 Create a Java program (See Figure 3.1 and Figure 3.2).

Sample output:

Figure 3.1:

Figure 3.2

PPA115D/TRO115D 2022 S1 - Umsebenzi Issue 1


4
Question 4

Students in Programming 1 had written a test and they need to know their performance.
Write a Java application that will help them achieve this using the information given below:

Enter the following input fields:


 Student number
 Student surname
 Marks obtained in a test
 The total for the test
o Convert this mark to a percentage and display the percentage on the screen in a
meaningful format that will include the student name and student number. It
must also include the status i.e.
 (Fail) if the percentage is less than 50%,
 (Pass) if the percentage is from 50% to 74.9% and
 (Pass with Distinction) if the percentage is 75% or above.
Many students did not perform well in this test and therefore the lecturer decided to
increase all final marks with 5%. Display the new percentage as well as the status of this new
percentage.
Display both answers in a user friendly way.

4.1 Create an IPO chart


4.2 Design for your program using a flow chart
4.3 Create a Java program (See Figure 4.1 and Figure 4.2).

Figure 4.1:

Figure 4.2:

PPA115D/TRO115D 2022 S1 - Umsebenzi Issue 1


5
Question 5

A student in Programming 1 wants to know the surface area and volume of a cylindrical shape.

Figure 7.1: Example of a cylinder

Prompt the user to enter the following values:


 height of the cylinder,
 and the radius of the base of the cylinder.
 Ask the user what he or she would like to see V or v for volume or A for A for Area?
o Based on the users choice display the corresponding calculations
Calculate the volume and surface area of a cylinder using the formulas below.

Volume = PI(R^²) x height

Area = 2PI(R^²) x height + 2PI(R^²)

5.1 Create an IPO chart


5.2 Design for your program using a flow chart
5.3 Create a Java program (See Sample output below).

Figure 5.1 Display area only

Figure 5.2 Display volume only

PPA115D/TRO115D 2022 S1 - Umsebenzi Issue 1


6
Figure 5.3 Invalid option

5.4 Write a corresponding C++ code for the above problem statement, using a switch statement.

PPA115D/TRO115D 2022 S1 - Umsebenzi Issue 1


7

You might also like