Understanding Class Definitions
Aim
To understand the basic elements of class definitions: fields,
constructor and methods.
Exercises
-
Download the naive-ticket-machine Bluej project to your work area. Start Bluej and open it. Do the
exercises 2.33 to 2.42 of the Bluej book.
-
2.33) Add a method called prompt to the TicketMachine class. This should have a void return type and take no parameters. The body of the method should print something like: Please insert the correct amount of money.
-
2.34) Add a showPrice method to the TicketMachine class. This should have a void return type and take no parameters. The body of the method should print something like:
The price of a ticket is xyz cents.
Where xyz should be replaced by the value held in the price field when the method is called.
-
2.35) Add a getTotal method to the TicketMachine class. This should have an int return type and take no parameters. The method should return the total amount of money collected by the machine (using the total field of the class).
-
2.40) 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?
-
2.41) 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?
Additional exercise. Include a check in the method to ensure that the value of the ticket is an integer greater than zero. If this is not the case the price of the ticket should not change and the method should send a message to the user saying something like: "Ticket cost cannot be set to <specified amount>. It has been left unchanged".
-
2.42) Frequently Java classes have more than one constructor. In these cases the constructors have the same name but different parameters. Give the class an additional constructor. The new constructor should take no parameter and set the price to be a default value of your choosing. Test your implementation by creating machines via the two different constructors.
Additional exercise. Include a check in the first constructor (the one that takes a parameter specifying the ticket price) to ensure that the price passed is sensible (greater than zero). If this is not the case the price of the ticket should be set to the default value and the constructor should send a message to the user saying something like: "Ticket cost cannot be <specified amount>. It has been set to 1000" (or whatever the default value is for you).
-
Download the book-exercise Bluej project to your work area. Start Bluej and open it. Do the
exercises 2.75 to 2.84 of the Bluej book.
-
2.75) 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.
-
2.76) Add two methods, printAuthor and printTitle, to the Book class. These should print the author and title fields, respectively, to the terminal window.
-
2.77) 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.
-
2.78) 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, Pages: 232
It is your choice how the details are formatted.
-
2.79) 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.
-
2.80) 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.
-
2.81) 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.
-
2.82) 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.
-
2.83) Challenge exercise. 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 degrees respectively. Define an accessor method to return the value of temperature.
-
2.84) Challenge exercise. 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.
| Last updated at 8:55pm, Thursday September 22nd 2011 |
Dr Natalia Beloff (N.Beloff@sussex.ac.uk) |
|
|
|