Java
OOP GUIDE / WORK
LAMP CLASS
Topics
- Instance variable of type Boolean
- Using encapsulation
TASK –
PART 1 – LAMP CLASS WITHOUT ENCAPSULATION
Create
the class named Lamp that will represent a lamp object. It has the following specification:
INSTANCE VARIABLES
- public boolean
isOn;
When isOn
is true, the lamp is on. When isOn is
false, the lamp is off.
- public int
onCounter;
OnCounter
will be used to count the number of times that the lamp is switched on.
CONSTRUCTOR
- There is a constructor that gets no parameters. It creates a lamp that is off with an onCounter
set to zero.
INSTANCE METHODS
- A toString() method that returns a String in
the following form: "The
lamp is ON. It has been switched
on 8 times".
TASK –
PART 2 – TESTING THE LAMP CLASS
Create
a LampTester class.
Inside main, create a lamp object.
Update the instance variables and use the toString() method to output the lamp’s state.
Notice
that we have to be careful to always update onCounter when we turn on the
lamp. The current code setup means
that we are responsible for keeping the onCounter’s
value accurate. This is not good
design! We want to object to do this automatically
for us.
The
solution to this problem is to use Encapsulation. We will do this in the next steps.
TASK –
PART 3 – SETUP
We
will be changing the Lamp class and the LampTester
class. So we will make copies so that
you can later compare the initial class with the encapsulated one.
- Create a package named lamp01.
- Move your Lamp and LampTester
classes into package lamp01.
- Create a package names lamp02.
- Make copies of the Lamp and LampTester
classes and paste them into package lamp02.
- Close all your open files and open the classes in
lamp02.
TASK –
PART 4 – ENCAPSULATED LAMP CLASS
We
want the instance variable onCounter to automatically update when the lamp is turned
on. We do this by making the instance
variables private and providing needed access with instance methods.
INSIDE LAMP.JAVA
- Make the instance variables onCounter and isOn
private.
- Add a switchOn() method. It makes isOn become true. It should also update onCounter.
- Add a switchOff() method. It makes isOff become false.
INSIDE LAMPTESTER.JAVA
- Notice that the LampTester
class contains errors now because the old code is trying to directly
access the instance variables which are no longer public. Remove the statements that are errors.
- Test both the switchOn() and switchOff() methods. Be sure to output the lamp’s state to
make sure that things are working as expected.
ADDITIONAL CHANGES
- Did you deal with the case where you might switchOn() your lamp several times in a
row?
- Notice that you can only see the state of the lamp’s
instance variable using toString() at the moment. It would probably be a good idea to
have get methods for both isOn and onCounter.
- Provide a getIsOn() method.
- Provide a getOnCounter()
method.
- It might be useful to have a method that simply
returns “ON” or “OFF” depending on the state of the lamp. Provide at getState() method that does this.
|