4.1 project better-ticket-machine:
After a ticket has been printed, could the value in the balance field ever be set to a negative value by subtracting price from it? Justify your answer.
(exercise 2.47 from Barnes and Kolling 3rd edition)
| For this exercise, include a short paragraph answer in your portfolio. |
4.2 project better-ticket-machine:
Write an assignment statement that will store the result of multiplying two variables, price and discount, into a third variable, saving.
(exercise 2.49 from Barnes and Kolling 3rd edition)
| For this exercise, include a single Java statement in your portfolio. |
4.3 project better-ticket-machine:
Write an assignment statement that will divide the value in total by the value in count and store the result in mean.
(exercise 2.50 from Barnes and Kolling 3rd edition)
| For this exercise, include a single Java statement in your portfolio. |
4.4 project better-ticket-machine:
Why does the following version of refundBalance not give the same results as the original?
public int refundBalance()
{
balance = 0;
return balance;
}
What tests can you run to demonstrate that it does not?
(exercise 2.53 from Barnes and Kolling 3rd edition)
| For this exercise, include a short paragraph answer in your portfolio. |
4.5 project better-ticket-machine:
What happens if you try to compile the TicketMachine class with the following version of refundBalance?
public int refundBalance()
{
return balance;
balance = 0;
}
What do you know about return statements that helps to explain why this version does not compile? (The error message provided probably will help you.)
(exercise 2.54 from Barnes and Kolling 3rd edition)
| For this exercise, include a short paragraph answer in your portfolio. |
4.6 project better-ticket-machine:
Add a new method, emptyMachine, that is designed to simulate emptying the machine of money. It should both return the value in total and reset total to be zero.
(exercise 2.55 from Barnes and Kolling 3rd edition)
4.7 project better-ticket-machine:
Rewrite the printTicket method so that it declares a local variable, amountLeftToPay. This should then be initialized to contain the difference between price and balance. Rewrite the test in the conditional statement to check the value of amountLeftToPay. If its value is less than or equal to zero, a ticket should be printed, otherwise an error message should be printed stating the amount still required. Test your version to ensure that it behaves in exactly the same way as the original version.
(exercise 2.57 from Barnes and Kolling 3rd edition)
4.8 project book-exercise:
The provided project defines two fields and a constructor to initialise the fields.
Add two accessor methods to the class - getAuthor and getTitle - that return the author and title fields as their respective results. Test your class by creating some instances and calling the methods.
(based on exercise 2.74 from Barnes and Kolling 3rd edition)
4.9 project book-exercise:
Add two methods, printAuthor and printTitle to the outline Book class. These should print the author and title fields, respectively, to the terminal window.
(exercise 2.75 from Barnes and Kolling 3rd edition)
4.10 project book-exercise:
Add a further field, pages, to the Book class to store the number of pages. This should be of type int, and its initial value should be passed to the single constructor, along with the author and title strings. Include an appropriate getPages accessor method for this field.
(exercise 2.76 from Barnes and Kolling 3rd edition)
4.11 project book-exercise:
Add a method, printDetails, to the Book class. This should print details of author, title and pages to the terminal window. It is your choice how the details are formatted. For instance, all three items could be printed on a single line, or each could be printed on a separate line. You might also choose to include some explanatory text to help a user work out which is the author and which is the title, for example:
Title: Robinson Crusoe, Author: Daniel Defoe, Pages: 232
(exercise 2.77 from Barnes and Kolling 3rd edition)
4.12 project book-exercise:
Add a further integer field, borrowed, to the Book class. This keeps a count of the number of times a book has been borrowed. Add a mutator, borrow, to the class. This should update the field by 1 each time it is called. Include an accessor, getBorrowed, that returns the value of this new field as its result. Modify printDetails so that it includes the value of this field with an explanatory piece of text.
(exercise 2.81 from Barnes and Kolling 3rd edition)