Assessed Exercise II
CS5002 Advanced Programming
Dr. Paul Henderson
February 2022
• Deadline: 16:30 on 18th March 2022
• Contribution to final course mark: 25%
• Solo or Group: Solo work
• Anticipated duration: 3 hours
Introduction
From the course Moodle page, download SlowCalculator.java. This contains
a class SlowCalculator implementing Runnable. Its constructor takes a num-
ber N as input and stores it. Method run performs a long, slow calculation
on this number, then prints the result. You do not need to consider (or even
understand) the mathematical details of what is being calculated!
In this exercise, you will build a program that allows a user to request the
calculation be performed for particular numbers. As the calculation is slow, you
will run it on background threads, so the user can continue to interact with the
system while tasks are running. To take advantage of modern multi-core CPUs,
the user may request several calculations be run in parallel.
Task
You will implement a console application (i.e. text-based, no graphical user
interface). When run, your program will:
1. display the text Enter a command followed by a newline
2. allow the user to enter a line of text, containing a command (see below)
3. run this command, and print any output specified (see below)
4. return to (1), unless the command requested the program to exit
The following table lists the commands the user may enter, and the required
behaviour of your program (note N is a long integer chosen by the user):
1
start N start calculating with input N , on a new thread
cancel N cancel the calculation with input N that is currently
running (do nothing if it already completed)
running print the total number of calculations now running,
and their inputs N (in any order), in the form
3 calculations running: 83476 1000 176544
get N if calculation for N is finished, print result is M
where M is the integer result; if calculation is not yet
finished, print calculating
exit finish running all calculations previously requested
by the user, then exit
abort exit immediately, without finishing any running cal-
culations
If the user enters any command not given above, the program will print the line
Invalid command.
Note that the provided code for SlowCalculator prints the answer at the
end of run(). You should remove this print statement, and replace it with
a suitable mechanism to return the result so the user can retrieve it. You
should not modify the calculations performed by SlowCalculator.calculate
or SlowCalculator.isPrime, but you may want to add support for interrup-
tion. You may change other parts of this class as required (e.g. adding new
fields or constructor parameters).
Here is an example of what should happen when your program runs; the
highlighted lines are input entered by the user:
Enter a command
start 1045606
Enter a command
running
1 calculations running: 1045606
Enter a command
get 1045606
result is 3
Enter a command
start 7234568
Enter a command
start 53491256
Enter a command
get 53491256
calculating
Enter a command
running
2 calculations running: 7234568 53491256
2
Enter a command
cancel 7234568
Enter a command
running
1 calculations running: 53491256
Enter a command
exit
Notes
• We will not provide invalid/strange inputs, for which the behaviour is un-
defined, i.e. not specified above. This includes asking to cancel a job that
was never started, asking to start a calculation that is already running, or
asking for the result of a cancelled (or never started) calculation
• Do ensure you print Invalid command (as mentioned above) if the user
enters a command that is not of the required form (e.g. N is missing or
not an integer, or a command is misspelt)
• We will not run the program with an excessively large number of calcula-
tions in parallel (more than 20), so there is no need to explicitly limit the
number of running threads
• Your program should not print any output aside from what is specified
above
Submission
• The static main method of your program should be defined in a class
Solution in file Solution.java
• You will submit a single .zip file (not a tar, or a rar, or anything else),
named 1234567.zip where 1234567 is replaced by your 7-digit numeric
student ID
• This .zip file shall contain no folders, (e.g. src/), but only the .java files
containing your code, including SlowCalculator.java and Solution.java,
and maybe others if you wish
• Upload your .zip file to Moodle before the deadline!
If you do not follow these instructions precisely, you will lose marks
(your submission will be processed by a computer program; it will
assume you’ve followed the instructions).
3
Mark Scheme
Your submission will be marked out of 50 points, broken down as follows:
• Code compiles successfully [5]
• Code is readable and appropriately commented [10]
• Program starts successfully and user can enter commands [5]
• start and cancel commands work as expected [12]
• running and get commands work as expected [12]
• exit and abort commands work as expected [6]
4