Algorithms worksheet 2

advertisement
Name:
Algorithm Design Worksheet 2
Criterion 1 - Design and evaluate algorithmic solutions to a range of problems
Question 1
The following is a partially completed
algorithm for an applet designed to calculate
the cost of a taxi fare based on the rate (Day
or Night) and the distance travelled in
kilometres.
Taxi Fare Calculator
Rate (‘D’ or ’N’):
D
Distance (in kilometres): 0
An applet window for this algorithm is
shown to the right.
Calculate
Total cost:
Reset
Initially
rate = D
distance = 0
cost = 0
When a value is entered into the “rate” TextField
set rate to value in “rate” TextField
When a value is entered into the “distance” TextField
set distance to value in “rate” TextField
When the “calculate” button is pressed
if rate = D
cost = 3.6 + 1.94 x distance
display cost
The following intended features of the program do not work. Indicate the necessary changes to
the algorithm so that these features work.
1. The calculated fare depends on the rate, using the following formulas

Day: cost = 3.6 + 1.94 x distance

Night: cost = 3.6 + 2.32 x distance
When the “calculate” button is pressed
Rosny College 2009, modified by Claremont College 2014
Page 1 of 4
2. The user should be able to reset the calculator back to the initial settings.
When the “reset” button is pressed
3. The calculator needs to be modified to include an additional charge of $2 for trips that start or
finish at the airport.
In addition to showing the changes to the algorithm, also show what changes need to be made to
the applet window.
When the “calculate” button is pressed
Claremont College 2015, based on Rosny College 2009
Page 2 of 4
Question 2
An applet is needed for a drink vending machine that contains cola and
lemonade. The machine is initially stocked with 100 cans of each product.
When a user buys a drink, they will select either the cola or lemonade button.
The machine also has a built in feature that prints a warning when the
number of cans of any product falls to 5 cans or less.
NOTE: Do not consider the insertion of coins or the cost of the drinks.
Initially
When “cola” button is pressed
When “lemonade” button is pressed
A new model of vending machine will hold 200 cans (or bottles) of cola, lemonade or water in
total. Show the modifications you would make to the algorithm so it will work for the new
machine.
Hint: Although the water will presumably be in bottles, you can treat these as “cans” for this
algorithm.
Claremont College 2015, based on Rosny College 2009
Page 3 of 4
Question 3
An applet is required that
will allow a user to enter
any number of values, and
then return the largest
value entered.
1. Sketch the design of the
applet interface (showing
any Buttons, Labels,
TextFields etc) in the box
to the right…
2. Write an algorithm in pseudocode for the applet using the initially/when structure:
Claremont College 2015, based on Rosny College 2009
Page 4 of 4
Download