PA3
PA3
Given the cost of the purchase, write an algorithm to calculate and print the money paid taking into
consideration the 10% sales taxes. The taxes are calculated on the amount after the discount.
• The indication of the gas gauge in percent (full=100, three quarters=75 and so on).
• The miles per gallon of the car.
Tank Capacity: 12
Gas Gauge Reading in percent: 50
Miles per Gallon: 30
Get Gas!
The algorithm should print out Get gas or Safe to proceed depending on if the car can cross the 200
miles with the gas remaining in the tank.
1
Exercise 3-3 Triangle
To be solved in Lab
Write an algorithm that determines whether A, B, and C can form the sides of a triangle. Note A, B
and C can form the sides of a triangle if each side is less than the sum of the two other sides, i.e.:
A < B + C, B < A + C and C < A + B .
If A, B, and C forms a triangle, calculate its area using the formula:
p
Area = S(S − A)(S − B)(S − C) , where S = (A + B + C)/2
• A: 85-100
• B: 74-85
• C: 60-74
• D: 50-60
• F: <50
Keeping in mind that a student cannot score more than 105 marks, nor less than 0 marks.
Write an algorithm that reads each student’s marks, print either a grade or an error message.
a) Write an algorithm that will print the same messages as the algorithm above using nested if-
statements.
b) Write an equivalent algorithm that will print the same messages as the algorithm above without
using any nested if-statements.
c) Discuss the drawback of your algorithm? Hint: Compare the efficiency of both algorithms.
2
Exercise 3-6 Mysterious Task
To be discussed in Lecture - TO FIX
• What does the following program display for any boolean values x, y and z? Choose one answer
from the below choices and Justify.
boolean x = false;
boolean y = false;
boolean z = true;
else
System.out.println((x&&y) || z);
a) The value of x
b) The value of y
c) The value of z
d) The value of x and y
e) Always true
f) True if either x and y are both True or z is True, and False otherwise.
• Consider a program which changes the value of a boolean variable that tracks whether a light is on
or off. Three people, creatively named Sarah, Ali, and Mina, who are claiming to be expert light
switchers have written different implementations of this function.
Sarah’s Version:
boolean LightOn = ?;
if(LightOn)
LightOn = false;
else
LightOn = true;
Ali’s Version:
boolean LightOn = ?;
if(LightOn)
LightOn = false;
if(!LightOn)
LightOn = true;
Minas’s Version:
boolean LightOn = ?;
LightOn = !LightOn;
Are these implementations equivalent, i.e. performing the same task? Are all three people expert
light switchers? Why? Justify your answer.