Programming I (CM10134 - CM50147): Lab 2
You will need to show the answers to all the standard questions. The challenge exercises 12 and 13 are optional
- Exercise 1
Modify the constructor of TicketMachine so that it no longer has a parameter. Instead, the price of tickets should be fixed at 1000 cents. What effect does this have when you construct ticket machine objects with BlueJ?
- Exercise 2
Implement a method, empty, that simulates the effect of removing all money from the machine. This method should have a void return type, and its body should simply set the total field to zero. Does this method need to take any parameters? Test your method by creating a machine, inserting some money, printing some tickets, checking the total and then emptying the machine. Is this method a mutator or an accessor?
- Exercise 3
Implement a method, setPrice, that is able to set the price of tickets to a new value. The new price is passed in as a parameter value to the method. Test your method by creating a machine, showing the price of tickets, changing the price, and then showing the new price. Is this method a mutator or an accessor?
- Exercise 3
Give the class two constructors. One should take a single parameter that specifies the price, and the other should take no parameter and set the price to be a default value for your choosing. Test the implementation by creating machines via the two different constructors.
- Exercise 4
Open the book-exercise project. Add two accessor methods to the Book class - getAuthor and getTitle - that return the author and title fields in their respective results. Test your class by creating some instances and calling the methods.
- Exercise 5
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 6
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 7
Add a method, printDetails, to the Book class. This should print details of the author, title and pages to the terminal window. For example:
Title: Robinson Crusoe,
Author: Daniel Defoe,
It is your choice how the details are formatted.
- Exercise 8
Add a further field, refNumber, to the Book class. This field can store a reference number for a library. It should be of type String and initialised to the zero length string ("") in the constructor as its initial value is not passed in a parameter to the constructor. Instead, define a mutator for it with the following signature:
public void setRefNumber(String ref)
The body of this method should assign the value of the parameter to the refNumber field. Add a corresponding getRefNumber accessor to help you check that the mutator works correctly.
- Exercise 9
Modify your printDetails method to include printing the reference number. However, the method should print the reference number only if it has been set - that is, the refNumber string has a non-zero length. If it has not been set, then print the string "zzz" instead. Hint: Use a conditional statement whose test calls the length method on the refNumber string.
- Exercise 10
Modify your setRefNumber mutator so that it sets the refNumber field only if the parameter is a string of at least three characters. If it is less than three, then print an error message and leave the field unchanged.
- Exercise 11
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.
- The following two exercises are challenge exercises - here for anyone who wants more practice or completes the other exercises within 2 hours.
- Exercise 12
Create a new project, heater exercise, within BlueJ. Edit the details in the project description - the text note you see in the diagram. Create a class, Heater, that contains a single integer field, temperature. Define a constructor that takes no parameters. The temperature field should be set to the value 15 in the constructor. Define the mutators warmer and cooler, whose effect is to increase or decrease the value of temperature by 5° respectively. Define an accessor method to return the value of temperature.
- Exercise 13
Modify your Heater class to define three new integer fields: min, max and increment. The values of min and max shoud be set by parameters passed to the constructor. The value of increment should be set to 5 in the constructor. Modify the definitions of warmer and cooler so that they use the value of increment rather than an explicit value of 5. Before proceeding further with this exercise, check that everything works as before. Now modify the warmer method so that it will not allow the temperature to be set to a value greater than max. Similarly, modify cooler so that it will not allow the temperature to be set to less than min. Check that the class works properly. Now add a method, setIncrement, that takes a single integer parameter and uses it to set the value of increment. Once again, test that the class works as you would expect it to by creating some Heater objects within BlueJ. Do things still work as expected if a negative value is passed to the setIncrement method? Add a check to this method to prevent a negative value from being assigned to increment.