Introduction

We will write a television class. A television object will have the following attributes (fields in the class):

  • manufacturer. The manufacturer attribute will hold the brand name, such as Sony, Toshiba, etc. 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).

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.
  • decreaseVolume. The decreaseVolume method will decrease the 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 constant value stored in the SCREEN_SIZE field.

We will also need a constructor method that will be used to create an instance of a Television. see image.

Task #1 Creating a Television Class

1.See the given Television.java file.

2.The 2 constant fields and the 3 remaining fields listed in the UML diagram have been declared.

3.Read and try to understand the constructor that has two parameters, a manufacturer's brand and a screen size. These parameters will bring in information.

4.Initialize the powerOn field to false (power is off), the volume to 20, and the channel to 2 inside the constructor.

5.Compile and fix the syntax errors. Do not run.

Task #2 Methods

1.Define access methods called getChannel, getManufacturer, and getScreenSize that return the value of the corresponding field. Follow the sample given as getVolume.

2.Read and try to understand the method given in the Televesion.java. Define decreaseVolume method that will decrease the volume by 1.

3.Compile and fix the syntax errors. Do not run.

Task #3 Run the application

1.You can only execute (run) a program that has a main method, so there is a driver program that is already written to test out your Television class. Copy the file TelevisionDemo.java to the same directory as Television.java.

2.Compile and run TelevisionDemo and follow the prompts. If your output matches the output below, Television.java is complete and correct. You will not need to modify it further for this lab.

OUTPUT (boldface is user input)

A 55 inch Toshiba has been turned on.
What channel do you want? 56
Channel: 56 Volume: 21
Too loud!! I am lowering the volume.
Channel: 56 Volume: 15

Task #4 Create another instance of a Television

1.Edit the TelevisionDemo.java file. Declare another Television object called portable.

2.Instantiate portable to be a Sharp 19 inch television.

3.Use a call to the power method to turn the power on.

4.Use calls to the accessor methods to print what television was turned on.

5.Use calls to the mutator methods to change the channel to the user's preference and decrease the volume by two.

6.Use calls to the accessor methods to print the changed state of the portable.

7.Compile and run TelevisionDemo again. See the given output sample (outputSample.doc)

Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.