Question
I'd like help coding this problem, thnank you. Java in Eclipse Introduction: Everyone is familiar with a television. It is the object we are going
I'd like help coding this problem, thnank you. Java in Eclipse
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 televisions state can be described by how these
controls are set.
We will write the Television
class. Each object that is created from theTelevision class must be able to hold information about that instance of a
Television in its 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
it
will be a named constant.
screenSize
. The
screenSize
attribute will hold the size of the television screen. This
cannot change onc
e the television has been created so
it
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
numeric integer
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.
power
. The
power
method will toggle the power between on
and off, changing the value
stored in the
powerOn
field from
true
to
false
or from
false
to
true
.
increaseVolume
. The
increaseVolume
method will increase the value stored in the
volume field by 1.
Page
2
of
6
decreaseVolume
. The
decreaseVolume
method will decrease t
he value stored in the
volume field by 1.
getChannel
. The
getChannel
method will return the value stored in the channel field.
getVolume
. The
getVolume
method will return the value stored in the volume field.
getManufacturer
. The
getManufacturer
method
will return the constant value stored
in the MANUFACTURER field.
getScreenSize
. The
getScreenSize
method will return the
numeric integer
constant
value stored in the SCREEN_SIZE field.
We will also need
two overloaded
constructor methods that will be used to create an instance of a
Television
.
One constructor method will receive via its parameters the
manufacturer
name and the
screen
size
.
Additionally,
t
he constructor method will
initialize
the remaining attribute
fields to the following default values:
powerOn
will be initialized to
false
,
volume
will
be initialized to 20 and
channel
will be initialized to 2.
One constructor method will receive via its parameters the values for all of th
e attribute
fields.
It will use those parameter values to initialize all of its attribute fields.
These ideas can be brought together to form a UML (Unified Modeling Language) diagram for this
class, as shown below.
Television
-
MANUFACTURER: String
-
SCREEN_SIZE: int
-
powerOn: Boolean
-
channel: int
-
volume: int
+ Television(brand: String, size: int):
+ Television(brand: String, size: int
,
p
o
w
e
r
:
B
o
o
l
e
a
n
,
s
t
a
t
i
o
n
:
i
n
t
,
s
o
u
n
d
L
e
v
e
l
:
i
n
t
):
+ setChannel (station: int
): void
+ power( ): void
+ increaseVolume( ): void
+ decreaseVolume( ): void
+ getChannel( ): int
+ getVolume( ): int
+ getManufacturer( ): String
+ getScreenSize( ): int
Class Name
Attributes or fields
Methods
+ public
-
private
NOTE:
field names in all
upper
case are constants
and not variables.
NOTE:
not always do setter/mutator methods begin with the prefix of se
t. See examples:
power, increaseVolume, decreaseVolume methods.
Page
3
of
6
Starting Instructions:
1.
In your Eclipse lab workspace, create a new java project and name it
LastNameFi
-
Lab0
6
Proj
(i.e.
SmithJ
-
Lab06Proj)
.
2.
Download from the D2L > LabMaterial
> LabExer0
6
Module the
TelevisionDemo
.java
source
code file
.
3.
Wait to
import this
TelevisionDemo
.java
source code file into
the
Eclipse
project
until told to do
so in a later task in this exercise.
4.
Begin your work with Task #1, which will create a new
class that will be the model for a
Television real world entity.
Task #
1
Create a New Class
1.
Create a new class called
Television
within this project. This class will be the definition
of a Television entity and thus
will not
contain a main method.
2.
Put the following header comment at the top of the class file.
//*********************************************
//
Class
:
class name
//Date:
month/year
//Author:
programmers name
//***********************************
**********
3.
Declare the 2 constant fields listed in the UML diagram.
4.
Declare the 3 remaining fields listed in the UML diagram.
5.
Write a comment for each field indicating its purpose or contents
6.
Save this file, which will compile the code. Fix any syntax
errors before proceeding.
7.
Do not run
remember there is no main method present in which to start the execution.
Task #
2
A Write a Constructor Method
1.
Create a constructor method definition that has two parameters, a manufacturers brand
and a screen
size. These parameters will bring in information to this method. Remember a
constructor method has the same name as the class and has no return data type
and has no
void keyword.
2.
Inside the constructor, assign the values taken in from the parameters to the corresponding
attribute fields.
3.
Initialize the
powerOn
field to
false
(power is off), the
volume
to 20, and the
channel
to 2.
4.
Write comments in Javadoc format describing the
purpose of the constructor method above
the method header and commenting the parameters with @param tags.
5.
Save this file, which will compile the code. Fix any syntax errors before proceeding.
6.
Do not run.
Task #
2B Write a Second Overloaded Construc
tor Method
1.
Create a constructor method definition that has
five
parameters
that provide the following:
a manufacturers brand
, a
screen size
, a power on/off Boolean indicator, a channel value
Page
4
of
6
and a volume value
. These parameters will bring in informatio
n to this method. Remember
a constructor method has the same name as the class and has no return data type
and has
no
void keyword.
2.
Inside the constructor, assign the values taken in from the parameters to the corresponding
attribute fields.
3.
Write com
ments in Javadoc format describing the purpose of the constructor method above
the method header and commenting the parameters with @param tags.
4.
Save this file, which will compile the code. Fix any syntax errors before proceeding.
5.
Do not run.
Task
#
3
Write
the class methods that define the behaviors
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 (
!
-
the
exclamation symbol
). If the
boolea
n
variable
powerOn
is
true
, then
!powerOn
is
false
and vice versa.
Use the assignment statement to change the state of
p
o
werOn
and then store it back into
p
owerOn
(remember assignment statements evaluate the right
hand side first, then assign the result to the left hand side variable.)
powerOn = !powerOn;
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.
6.
Save this file, which will compile the code. Fix any syntax errors before proceeding.
7.
Do not run.
Task #
4
Running the Application
1.
You can only execute (run) a program that has a main method, so
the file that you
downloaded from D2L earlier in the instructions contains a starter version of the
demo/driver program that
you can use to
test out your
Television
class.
Complete a
File System Import of this
TelevisionDemo
.java
source code file into the
src
folder of the
LastNameFi
-
Lab0
6
Proj
project. (NOTE: The instructions regarding how to
import a
single/separate file into a projec
t
can be found in the
Getting Started With Eclipse
instruction
document found in the Course/Java Resources module in D2L.)
2.
At this point there should be no syntax errors. However, this demo/driver class and the
Television class must work in tandem, so i
f the class name or method names and
parameters do not match between the two files then syntax errors will occur
.
So if there
are any syntax errors, then fix these
errors before proceeding.
3.
Run the
TelevisionDemo
program and complete the input prompts as it runs.
Page
5
of
6
4.
If your output matches the output below. The
Television
.
java
class is complete and
correct. There will not be any further changes to the class file. However, if the following
output is not created, t
hen youll need to review the
Television.java
class file to
determine what needs to be changed to produce this output.
NOTE: Highlighted in yellow and bolded is user input
A 55 inch Toshiba has been turned on.
TV Settings
-
Channel: 2 Volume: 20
What channel do you want?
56
Volume too low! Increase the volume.
TV Settings
-
Channel: 56 Volume: 25
Too loud! Lowering the volume.
TV Settings
-
Channel: 56 Volume: 20
Task #
5
Creation another instance of a Television
1.
Edit the
TelevisionDemo.java
file.
2.
Instantiate another
Television
object whose reference variable is called
portable
Instantiate this object using the
Television
class fully parameterized constructor and
provide the following data to initialize the objects attribute fields: Shar
p is the
manufacturer, 19 inches is the size,
power is not on (i.e. false),
30 is the channel and 12 is
the volume level.
3.
Using
some of
the objects
accessor(
getter
) methods to
output the following statement that
includes the objects size and manufactur
er
The Television is a
19
inch made by
Sharp
4.
Use a call to the
power
method to turn the power on.
5.
Use calls to the accessor methods to print that the television was turned on and its current
channel and volume settings
T
he
19
inch
Sharp
has been turned on
TV Settings
-
Channel:
30
Volume:
12
6.
Use calls to the mutator(setter) methods to change the
channel
to the users preference
and decrease the
volume
by 2 levels
What channel do you want?
35
Please decrease the volume two levels
Note: channel
number is user input
Page
6
of
6
7.
Use calls to the accessor methods to print the changed state of the
portable
object.
TV Settings
-
Channel:
35
Volume:
10
8.
Save this file, which will compile the code. Fix any syntax errors before proceedin
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started