Académique Documents
Professionnel Documents
Culture Documents
Assessment 2
Sample Version
For the purpose of this assessment you need to open the attached java file in BlueJ and open
the BlueJ package VendingMachine. It will be a downloaded package on the actual
assessment
WARNING: The class does not work correctly when you first open it.
Note questions 1 to 5 refer to the original source code you have downloaded (BEFORE YOU
MAKE ANY CHANGES)
4. Write the line number of the second field in the class. (1)
5. Write the line number of the third parameter to be found in the class. (1)
● Open the Object inspector and then try out each of the methods for the
VendingMachine to see what it does. (it probably does not do what you expect!)
Now for parts 6, 7 and 8 use BlueJ to change the program code as explained below.
Question 6. The addMoney() method incorrectly sets the balance to zero. It should add the
value of the coin to the total. Change the line so that it works correctly in BlueJ. When
you have written and checked it is correct, copy the code here
}
2 marks
Question 7. Add a line to the method displayPrice() so that it exactly displays the
message
The price is 10p
when the price holds the value 10 and
The count is 9p
when the price holds the value 9.
hint: call
displayPrice() then the method changePrice() and then call the
displayPrice() method again. The message printed out should change!
When you have written and checked it is correct in BlueJ, copy the line of code here.
public void displayPrice( )
{
} 2 marks
Question 8. The takeChoc() method does not work correctly. It should only reduce the
number of chocolates by one if the value held in total is more than or equal to the
value held in price.... and the number of chocolates is more than zero
Replace the line that needs to be changed with an if statement(s) so that the value in
chocolate remains correct. Only if chocolates is reduced by 1, total needs to be
reduced by the price and balance needs to be increased by price
When you have written and checked it is correct using BlueJ, copy the complete method here.
4 marks
}
Question 9. Write a method called needChocs() that returns the boolean value true if the
number held in chocolates is less than 9, otherwise it will return the value false.
After you have developed the method in BlueJ write it in the box below.
}
2 marks