Practical 2 A
Practical 2 A
You can carry out this worksheet using either BlueJ or at the command prompt with
Textpad/Notepad, etc. It is up to you!
Open This PC, and then open the V: Groups on Wide\CSCU9A2 folder, and then the Java
folder. Take a copy of the file Marks.java into your own file space in a suitable new
folder.
Remember that it is good practice to keep each programming project in its own,
separate folder.
Marks.java will not compile as it stands, as it has methods missing.
Open Marks.java and look at what is there:
There is a main method that asks the user how many marks are to be processed, then
declares an array of the correct size to hold the marks. The method body then
contains the main processing in the form of calls of three further methods whose
names indicate their function: Read the marks, discard the lowest, and calculate the
average – and it should be clear that they identify three processing steps that
together solve the overall problem. This method is complete and correct.
At the end of the program is a standard readInteger method, with the addition of a
parameter that is used as an initial prompt to the user. This is complete and correct.
In between is the header for one method called from main, readMarks, but with an
empty method body. This method placeholder is here because it is a subtask
introduced in the refinement of the main method body. At a later stage, its body will
be completed.
Now for some work:
Two other methods are called from main: discardLowestMark and
averageTheMarksExceptTheLastOne. Add two further placeholders for these two
methods – the method headers should match how the methods are called in main.
The descriptions of those two methods as devised by the designer of main are:
discardLowestMark: Assume that the given array is full of marks (integers). The
lowest mark is discarded by moving all the following marks down one element in the
array . This leaves valid marks in all except the last element of the array.
averageTheMarksExceptTheLastOne: Add all the marks in the given array together,
excluding the very last array element (as that element contains junk left over from
discarding the lowest mark). Return the average as the result.
The program might now compile, but doesn't do much yet!
There are three outstanding tasks: three methods with empty bodies.
Focus on method readMarks: You can write the Java for readMarks' body quite
easily (it's readArray from previously). Compile, and readMarks should run
correctly. [But how can you test that it runs correctly? One possibility is to
temporarily add into main, after the call of readMarks, a short for loop that outputs
each array element to System.out.println];
Now focus on method discardLowestMark: That looks a bit tricky, so we'll refine it
into two further subtasks (without coding their full details just now). The two steps
are: work out where the lowest mark is, then remove it. Insert this into the body of
discardLowestMark, which introduces two further methods:
int position = findLowestMark(marks);
removeMark(position, marks);
Then add two placeholders for the two new methods. You'll come back to them
later.
Their descriptions are:
findLowestMark: Search the given array of marks, and return the index of the lowest
mark (or an equal lowest mark).
removeMark: Remove the value at index position in array marks by moving all
further values down one element.
Now focus on method averageTheMarksExceptTheLastOne: You can write the Java
for the method body quite easily – a loop to add, and then calculate and return the
average (the principle was in a previous practical). Compile and run: you should be
able to enter marks and see some kind of average (but, of course, it is not the
proper required answer, as the method to discard the lowest mark currently does
nothing!).
Remember: Your code must be well formatted and clearly commented.
SBJ