Lab 06
Lab 06
Methods
Lab Objectives
Be able to declare a new class
Be able to write a constructor
Be able to write instance methods that return a value
Be able to instantiate an object
Be able to use calls to instance methods to access a change the state of an object
Introduction
Everyone is familiar with a television. It is the object we are going to create in this lab.
First we need a blueprint. All manufacturers have the same basic elements in the
televisions they produce as well as many options. We are going to work with a few basic
elements that are common to all televisions. Think about a television in general. It has a
brand name (i.e. it is made by a specific manufacturer). The television screen has a
specific size. It has some basic controls. There is a control to turn the power on and off.
There is a control to change the channel. There is also a control for the volume. At any
point in time, the television’s state can be described by how these controls are set.
We will write the Television class. Each object that is created from the
Television class must be able to hold information about that instance of a
Television in fields. So a Television object will have the following attributes:
manufacturer. The manufacturer attribute will hold the brand name. This
cannot change once the television is created, so will be a named constant.
screenSize. The screenSize attribute will hold the size of the television
screen.
This cannot change once the television has been created so will be a named
constant.
powerOn. The powerOn attribute will hold the value true if the power is on
and false if the power is off.
channel. The channel attribute will hold the value of the station that the
television is showing.
volume. The volume attribute will hold a number value representing the
loudness (0 being no sound).
These attributes become fields in our class.
The Television object will also be able to control the state of its attributes. These
controls become methods in our class.
setChannel. The setChannel method will store the desired station in the
channel field.
We will also need a constructor method that will be used to create an instance of a
Television.
These ideas can be brought together to form a UML (Unified Modeling Language)
diagram for this class, as shown below.
-MANUFACTURER: String
-SCREEN_SIZE: int Attributes or fields
-powerOn: boolean
-channel: int
-volume: int
Task #3 Methods
1. Define accessor methods called getVolume, getChannel,
getManufacturer, and getScreenSize that return the value of the
corresponding field.
2. Define a mutator method called setChannel that accepts a value to be stored in
the channel field.
3. Define a mutator method called power that changes the state from true to
false or from false to true. This can be accomplished by using the NOT
operator (!). If the boolean variable powerOn is true, then !powerOn is
false and vice versa.
Use the assignment statement
powerOn = !powerOn;
to change the state of powerOn and then store it back into powerOn (remember
assignment statements evaluate the right hand side first, then assign the result to
the left hand side variable.
4. Define two mutator methods to change the volume. One method should be
called increaseVolume and will increase the volume by 1. The other method
should be called decreaseVolume and will decrease the volume by 1.
5. Write javadoc comments above each method header.
/**
This class demonstrates the Television class.
*/
// Declare variables
int station; // The user's channel choice