Question

In: Computer Science

Lab Objectives Be able to declare a new class Be able to write a constructor Be...

Lab Objectives

  1. Be able to declare a new class
  2. Be able to write a constructor
  3. Be able to write instance methods that return a value
  4. Be able to write instance methods that take arguments
  5. Be able to instantiate an object
  6. Be able to use calls to instance methods to access and 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:

  1. manufacturer. The manufacturer attribute will hold the brand name. This cannot change once the television is created, so will be a named constant.
  2. 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.
  3. powerOn. The powerOn attribute will hold the value true if the power is on, and false if the power is off.
  4. channel. The channel attribute will hold the value of the station that the television is showing.
  5. 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.

  1. setChannel. The setChannel method will store the desired station in the channel field.

29

  1. 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.
  2. increaseVolume. The increaseVolume method will increase the value stored in the volume field by 1.
  3. decreaseVolume. The decreaseVolume method will decrease the value stored in the volume field by 1.
  4. getChannel. The getChannel method will return the value stored in the channel field.
  5. getVolume. The getVolume method will return the value stored in the volume field.
  6. getManufacturer. The getManufacturer method will return the constant value stored in the MANUFACTURER field.
  7. 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.

These ideas can be brought together to form a UML (Unified Modeling Language) diagram for this class as shown below.

30

Task #1 Creating a New Class

  1. In a new file, create a class definition called Television.
  2. Put a program header (comments/documentation) at the top of the file
    1. The purpose of this class is to model a television
    2. Your name and today’s date
  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 what it represents.
  6. Save this file as Television.java.
  7. Compile and debug. Do not run.

Task #2 Writing a Constructor

  1. Create a constructor definition that has two parameters, a manufacturer’s brand and a screen size. These parameters will bring in information
  2. Inside the constructor, assign the values taken in from the parameters to the corresponding fields.
  3. Initialize the powerOn field to false (power is off), the volume to 20, and the channel to 2.
  4. Write comments describing the purpose of the constructor above the method header.
  5. Compile and debug. Do not run.

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 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.

  1. 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.
  2. Write javadoc comments above each method header.
  3. Compile and debug. Do not run.

31

Task #4 Running 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 (see code listing 3.1) from the Student CD or as directed by your instructor. Make sure it is in the same directory as Television.java.
  2. Compile and run TelevisionDemo and follow the prompts.
  3. 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 #5 Creating another instance of a Television

  1. Edit the TelevisionDemo.java file.
  2. Declare another Television object called portable.
  3. Instantiate portable to be a Sharp 19 inch television.
  4. Use a call to the power method to turn the power on.
  5. Use calls to the accessor methods to print what television was turned on.
  6. Use calls to the mutator methods to change the channel to the user’s preference and decrease the volume by two.
  7. Use calls to the accessor methods to print the changed state of the portable.
  8. Compile and debug this class.
  9. Run TelevisionDemo again.
  10. The output for task #5 will appear after the output from above, since we added onto the bottom of the program. The output for task #5 is shown below.

OUTPUT (boldface is user input)

A 19 inch Sharp has been turned on.

What channel do you want? 7

Channel: 7 Volume: 18

Code Listing 3.1 (TelevisionDemo.java)

/** This class demonstrates the Television class*/

import java.util.Scanner;

public class TelevisionDemo

{

public static void main(String[] args)

32

{

//create a Scanner object to read from the keyboard Scanner keyboard = new Scanner (System.in);

//declare variables

int station;   //the user’s channel choice

//declare and instantiate a television object

Television bigScreen = new Television("Toshiba", 55); //turn the power on

bigScreen.power();

//display the state of the television System.out.println("A " + bigScreen.getScreenSize() +

bigScreen.getManufacturer() + " has been turned on."); //prompt the user for input and store into station

System.out.print("What channel do you want? "); station = keyboard.nextInt();

//change the channel on the television bigScreen.setChannel(station);

//increase the volume of the television bigScreen.increaseVolume();

//display the the current channel and volume of the television System.out.println("Channel: " + bigScreen.getChannel() +

  1. Volume: " + bigScreen.getVolume());

System.out.println("Too loud!! I am lowering the volume."); //decrease the volume of the television bigScreen.decreaseVolume(); bigScreen.decreaseVolume(); bigScreen.decreaseVolume(); bigScreen.decreaseVolume(); bigScreen.decreaseVolume(); bigScreen.decreaseVolume();

//display the current channel and volume of the television System.out.println("Channel: " + bigScreen.getChannel() +

  1. Volume: " + bigScreen.getVolume()); System.out.println(); //for a blank line

//TASK #5

}

}

33

Solutions

Expert Solution

Please find the answer below :-

Television.java file :-

TelevisionDemo.java file :-

Thanks..


Related Solutions

Lab to be performed in Java. Lab: 1.) Write a class named TestScores. The class constructor...
Lab to be performed in Java. Lab: 1.) Write a class named TestScores. The class constructor should accept an array of test scores as its argument. The class should have a method that returns the average of the test scores. If any test score in the array is negative or greater than 100, the class should throw an IllegalArgumentException. Write a driver class to test that demonstrates that an exception happens for these scenarios 2.) Write a class named InvalidTestScore...
- Create a java class named SaveFile in which write the following: Constructor: The class's constructor...
- Create a java class named SaveFile in which write the following: Constructor: The class's constructor should take the name of a file as an argument A method save (String line): This method should open the file defined by the constructor, save the string value of line at the end of the file, and then close the file. - In the same package create a new Java class and it DisplayFile in which write the following: Constructor: The class's constructor...
In C++ Write a class named TestScores. The class constructor should accept an array of test...
In C++ Write a class named TestScores. The class constructor should accept an array of test scores as its argument. The class should have a member function that returns the average of the test scores. If any test score in the array is negative or greater than 100, the class should throw an exception. Demonstrate the class in program.
Learning Objectives: To be able to code a class structure with appropriate attributes and methods. To...
Learning Objectives: To be able to code a class structure with appropriate attributes and methods. To demonstrate the concept of inheritance. To be able to create different objects and use both default and overloaded constructors. Practice using encapsulation (setters and getters) and the toString method. Create a set of classes for various types of video content (TvShows, Movies, MiniSeries). Write a super or parent class that contains common attributes and subclasses with unique attributes for each class. Make sure to...
Class Exercise: Constructor using JAVA Let’s define a Class together and have a constructor while at...
Class Exercise: Constructor using JAVA Let’s define a Class together and have a constructor while at it. - What should the Class object represent? (What is the “real life object” to represent)? - What properties should it have? (let’s hold off on the methods/actions for now – unless necessary for the constructor) - What should happen when a new instance of the Class is created? - Question: What are you allowed to do in the constructor? - Let’s test this...
Java- Write a class called Rectangle that inherits from Shape. Write a constructor that has length,...
Java- Write a class called Rectangle that inherits from Shape. Write a constructor that has length, width and colour as arguments. Define enough functions to make the class not abstract
Please write code in java and comment . thanks Item class A constructor, with a String...
Please write code in java and comment . thanks Item class A constructor, with a String parameter representing the name of the item. A name() method and a toString() method, both of which are identical and which return the name of the item. BadAmountException Class It must be a RuntimeException. A RuntimeException is a subclass of Exception which has the special property that we wouldn't need to declare it if we need to use it. It must have a default...
Write the code to create a class Square implementing the interface Figure with constructor to initialize...
Write the code to create a class Square implementing the interface Figure with constructor to initialize with a size and toString method. Write another class SquareUser that creates and array of Squares and initializing with sides 1, 2,3, 4 and 5. Also write the code to call the toString method of squares in a loop to print the string for all squares in the array.
Lab Assignment Objectives 'Be able to overload combined binary operators as member operator functions. Show how...
Lab Assignment Objectives 'Be able to overload combined binary operators as member operator functions. Show how to overload binary operators as friend functions. Show how to convert from a fundamental type to a user-defined type using a constructor. Understand exception handling mechanisms using try-catch block statements. Understand the Application Complex Numbers A complex number, c, is an ordered pair of real numbers (doubles). For example, for any two real numbers, s and t, we can form the complex number: This...
Lab Assignment Objectives 'Be able to overload combined binary operators as member operator functions. Show how...
Lab Assignment Objectives 'Be able to overload combined binary operators as member operator functions. Show how to overload binary operators as friend functions. Show how to convert from a fundamental type to a user-defined type using a constructor. Understand exception handling mechanisms using try-catch block statements. Understand the Application Complex Numbers A complex number, c, is an ordered pair of real numbers (doubles). For example, for any two real numbers, s and t, we can form the complex number: This...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT