Practical 2: More Java Basics

Programming Refresher - MSc Computing , IT and SWE
Practical 2: More Java Basics
Lecturers: Alvaro Miyazawa and Ana Cavalcanti
Material Prepared by Rob Alexander
The aims of this practical are:

Use if...else and looping constructs to control program flow;

Use arrays to store data.
Exercise 1: Largest and Smallest
New Challenge: use if…else statements to make decisions.
Write a program that accepts three integer values. The values are then compared to determine which
value is the largest and which is the smallest. For example if the input numbers are:
10 25 -2
The output should be:
Largest of three numbers is 25
Smallest of three numbers is -2
Your program should be able to cope with the scenario of two or three numbers being the same value.
For example if the input numbers are:
10 10 10
The output should be:
Largest of three numbers is 10
Smallest of three numbers is 10
Note: There are several ways to do this, one of which is using the Math.min() and Math.max()
methods from the Java library. That’s a bit easy, however – a better exercise is to do it using nested IfElse statements.
Exercise 2: ATM
New Challenges: create a command-line menu; validate user input.
Write a program to simulate a simple ATM machine (a bank cash machine/cash point), such that:

The starting balance is 100.00;

There is a Deposit Option. A valid deposit amount must be:
 Expressed in pounds only (no pence);
 In the range 100 to 500.
Programming Refresher - MSc Computing , IT and SWE

There is a withdrawal Option. A valid withdrawal amount must be:
 Expressed in pounds only (no pence);
 No more than 250;
 No less than 10;
 A multiple of 10 pounds;
 Not enough to leave the balance less than zero.
Note:

All values that don’t change should be stored as named constants;

Users should be informed of errors using the error stream (Eclipse will helpfully show this in
red);

After a successful deposit or withdrawal, the user should be informed of the amount
deposited or withdrawn, and a new balance should be displayed;

Although it might seem natural to represent pounds and pence values using a float or double,
they are not designed for this purpose. Whenever you need to store a value representing an
amount of money, use an int to store it in pence (or cents, and so on).
User interaction should be via a command-line menu. Make sure you check that the inputs are valid
before trying to use them.
Exercise 3: Vending Machine
New Challenges: build a more complex program than previously; use a loop as part of an algorithm.
Write a simple program that simulates a vending machine. On starting up, the program will display the
following items as part of a command line menu:
Chocolate £0.57
Cola £0.71
Crisps £0.34
Upon selecting a valid item the user is shown the item purchased, the cost, and the change due.
Assume that the user has already inserted one pound into the machine. At the end of the program,
calculate how to deliver the change in the lowest number of coins. For example if a chocolate bar
was purchased, the change due will be £0.43, which the machine should return in the following coins:







£1:
50p:
20p:
10p:
5p:
2p:
1p:
0
0
2
0
0
1
1
The program should output the change returned as a list similar to the one above.
Programming Refresher - MSc Computing , IT and SWE
Hints:



Create a method getLowestCoins which both calculates and outputs the change due;
o You will need to make use of the Integer Division and Modulo arithmetical operations.
The quit option should also refund change;
If user selects invalid item then they get no change.
Once you have tested that your program works correctly, change the price of Chocolate to £0.55 and
check that the program still returns the correct change for a chocolate purchase.
Exercise 4: Factorial
New Challenge: none – this just gives you more practice with loops. Skip this one if you’re pressed for
time.
Write a program that prompts the user for an integer and then prints its factorial.
The factorial of a number n is defined as:
 n × (n – 1) × (n – 2) × (n – 3) ×
. . .
× 1
For example, the factorial of 8 is:
 8 × 7 × 6 × 5 × 4 × 3 × 2 × 1 = 40320
Note




There is a special case: the factorial of 0 is 1;
There is no need for a command line menu here;
Display the final factorial total in a suitable message;
Do apply validation (e.g. print an error message if the user inputs a negative number).
Exercise 5: Arrays
New Challenge: perform a range of common array operations.
Write a program that will manipulate an array of seven elements. The program should declare an
integer array of seven elements and then display some values derived from it.
Create a class-level array which stores the following values:

2, 5, 9, 2, -1, 2, 22, -2, 10
Provide a command line menu and processing to achieve the following:




Display the number of times the value 2 appears in the array;
Display the total of all the element values;
Display an average of all the element values (to three decimal places);
Display the contents of the array.
Programming Refresher - MSc Computing , IT and SWE
Exercise 6: Enhanced Vending Machine
New Challenge: use a loop to create a repeating command-line menu.
Create a new program based upon your Vending Machine program from earlier, which allows the user
to repeatedly buy items until they choose to quit.
Required modifications:

Maintain a running total of cost of sales for user;

Maintain a running total of change left for user;

Use a while loop to control the command line menu;

When the program starts, assume that the user has put £5.00 in the machine ;

Each time user selects an item to buy, check that they can afford to buy it:
 If not, display a suitable message;
 If so
 Print a message which displays the display item bought, cost and change left;
 Add item, item cost and total cost to a virtual receipt.

Add a “Complete Sale” option which when selected:
 Displays the virtual receipt with a suitable header row;
 Displays the total change to be refunded;
 Returns change in lowest number of coins (for this exercise, extend this to include the
£2 coin).