0% found this document useful (0 votes)
49 views3 pages

PA3

This document contains instructions and sample code for 6 programming exercises related to concepts in an introduction to computer science course. The exercises cover topics like discounts, gas mileage calculations, triangle properties, letter grades, determining elementary/middle/high school levels, and boolean logic evaluations.

Uploaded by

john osama
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)
49 views3 pages

PA3

This document contains instructions and sample code for 6 programming exercises related to concepts in an introduction to computer science course. The exercises cover topics like discounts, gas mileage calculations, triangle properties, letter grades, determining elementary/middle/high school levels, and boolean logic evaluations.

Uploaded by

john osama
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/ 3

Rand<randomgrayRand<g Rand<GRand<g Rand<Grandomgray

University of Canada in Egypt


Faculty of Engineering
Dr.Amr Elmougy
Rana Eisa

Introduction to Computer Science, Winter Semester 2018


Practice Assignment 3

Exercise 3-1 Discount


To be solved in Lab
A discount is made on a purchase as follows:

• if purchase ≤ 1000 L.E., there is no discount

• if purchase > 1000 L.E., the discount is 5%

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.

Exercise 3-2 Gas Station


To be discussed in Lecture
National Last Chance gas station sits on Cairo Alexandria route. There is no other gas station for 200
Miles. Write an algorithm to help drivers decide if they need gas or not. The user will enter:

• The capacity of the gas tank, in Gallons.

• The indication of the gas gauge in percent (full=100, three quarters=75 and so on).
• The miles per gallon of the car.

The following is a sample run of the algorithm:

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

Exercise 3-4 Letter Grades


To be discussed in Lecture
Students marks in a class are graded on the following policy:

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

Exercise 3-5 Student School


To be solved in Lab
The following algorithm prints out whether a current student is in elementary (1st - 5th), middle (6th -
8th), or high school (9th - 12th), with and with-out Nested if-statements.

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;

if(x==true && y==true)


System.out.println(!x || z);

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.

You might also like