Question

Java

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

// The purpose of this class is to model a television

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

Chapter 6 Lab Classes and Objects 53

Gaddis_516907_Java 4/10/07 2:10 PM Page 53

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.

54 Lab Manual to Accompany Starting Out with Java 5: From Control Structures to Objects

Gaddis_516907_Java 4/10/07 2:10 PM Page 54

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.

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. Compile and debug. Do not run.

Chapter 6 Lab Classes and Objects 55

Gaddis_516907_Java 4/10/07 2:10 PM Page 55

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 www.aw.com/cssupport

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

56 Lab Manual to Accompany Starting Out with Java 5: From Control Structures to Objects

Gaddis_516907_Java 4/10/07 2:10 PM Page 56

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

Chapter 6 Lab Classes and Objects 57

Gaddis_516907_Java 4/10/07 2:10 PM Page 57

Code Listing 6.1 (TelevisionDemo.java)

import java.util.Scanner;

/** This class demonstrates the Television class*/

public class TelevisionDemo

{

public static void main(String[] args)

{

//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() +

" 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() +

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

System.out.println(); //for a blank line

//HERE IS WHERE YOU DO TASK #5

}

}

1 0
Add a comment Improve this question Transcribed image text
✔ Recommended Answer
Answer #1
Dear,
// Television Class


// The purpose of this class is to model a television
// Your name and today's date
public class Television {

    private final String MANUFACTURER ; //manufacturer of the TV
    private final int SCREEN_SIZE ; //Screen size of the TV
    private boolean powerOn;    //power on/off button
    private int channel;    //channel
    private int volume;     //volume to increase/decrease

    //two arguments constructor initializes all the attributes of the
    //television class
    public Television(String manu,int screen){
        MANUFACTURER = manu;
        SCREEN_SIZE = screen;
        powerOn = false;
        volume = 20;
        channel = 2;       
    }

    //returns the current volume
    public int getVolume(){
        return volume;
    }
    //return the current channel
    public int getChannel(){
        return channel;
    }
    //returns the manufacturer of the TV
    public String getManufacturer(){
        return MANUFACTURER;
    }
    //returns the screen-size
    public int getScreenSize(){
        return SCREEN_SIZE;
    }
    //sets the channel to a given number
    public void setChannel(int ch){
        channel=ch;
    }
    //sets the TV power on/off
    public void power(){
        powerOn = !powerOn;
    }
    //increases the TV volume by 1
    public void increaseVolume(){
        volume+=1;
    }
    //decreases the TV volume by 1
    public void decreaseVolume(){
        volume-=1;
    }
} //end of the Television class


// TelevisionDemo.java


import java.util.Scanner;

/** This class demonstrates the Television class*/
public class TelevisionDemo {

    public static void main(String[] args) {

        //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() + " inch "+
                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() +
                " 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() +
                " Volume: " + bigScreen.getVolume());

        System.out.println(); //for a blank line

        //HERE IS TASK #5
        Television portable=new Television("Sharp", 19);
        portable.power();

        //display the state of the television
        System.out.println("A " + portable.getScreenSize() + " inch "+
                portable.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
        portable.setChannel(station);
        //decrease the volume of the television.
        portable.decreaseVolume();
        portable.decreaseVolume();

        //display the current channel and volume of the
        //television

        System.out.println("Channel: " +
                portable.getChannel() +
                " Volume: " + portable.getVolume());
    }
}

Output:

run:
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

A 19 inch Sharp has been turned on.
What channel do you want? 7
Channel: 7 Volume: 18
BUILD SUCCESSFUL (total time: 12 seconds)


Add a comment
Know the answer?
Add Answer to:
Java
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Similar Homework Help Questions
  • Topic: Java Programming Hello, I have the following code attached below, and keep getting told that...

    Topic: Java Programming Hello, I have the following code attached below, and keep getting told that "Television" on the 23rd line "cannot be resolved to a type". It happens on this line of code... Television portable = new Television("Sharp", 19); Can you tell me how to fix this? Beginning of code... import java.util.Scanner; public class TelevisionDemo { {} public static void main(String[] args) { // //create a Scanner object to read from the keyboard Scanner keyboard = new Scanner(System.in); //...

  • java This lab is intended to give you practice creating a class with a constructor method,...

    java This lab is intended to give you practice creating a class with a constructor method, accessor methods, mutator methods, equals method , toString method and a equals method. In this lab you need to create two separate classes, one Student class and other Lab10 class. You need to define your instance variables, accessor methods, mutator methods, constructor, toString method and equals method in Student class. You need to create objects in Lab10 class which will have your main method...

  • Write a java class definition for a circle object. The object should be capable of setting...

    Write a java class definition for a circle object. The object should be capable of setting radius, and computing its area and circumference. Use this to create two Circle objects with radius 10 and 40.5, respectively. Print their areas and circumference. Here is the Java class file (Circle.java). Compile it. public class Circle{ //Instance Variables private double PI = 3.1459; private double radius; //Methods public Circle ( ) { }    //get method (Accessor Methods ) public double getRadius (...

  • Task #3 Arrays of Objects 1. Copy the files Song java (see Code Listing 7.1), Compact...

    Task #3 Arrays of Objects 1. Copy the files Song java (see Code Listing 7.1), Compact Disc.java (see Code Listing 7.2) and Classics.txt (see Code Listing 7.3) from the Student Files or as directed by your instructor. Song.java is complete and will not be edited. Classics.txt is the data file that will be used by Compact Disc.java, the file you will be editing. 2. In Compact Disc.java, there are comments indicating where the missing code is to be placed. Declare...

  • package _solution; /** This program demonstrates how numeric types and operators behave in Java Do Task...

    package _solution; /** This program demonstrates how numeric types and operators behave in Java Do Task #1 before adding Task#2 where indicated. */ public class NumericTypesOriginal { public static void main (String [] args) { //TASK #2 Create a Scanner object here //identifier declarations final int NUMBER = 2 ; // number of scores int score1 = 100; // first test score int score2 = 95; // second test score final int BOILING_IN_F = 212; // boiling temperature double fToC;...

  • JAVA Code Requried Copy the file java included below. This program will compile, but, when you...

    JAVA Code Requried Copy the file java included below. This program will compile, but, when you run it, it doesn’t appear to do anything except wait. That is because it is waiting for user input, but the user doesn’t have the menu to choose from yet. We will need to create this. Below the main method, but in the Geometry class, create a static method called printMenu that has no parameter list and does not return a value. It will...

  • PYTHON Task 1 Create a class called Window It has 2 public properties 1 private property...

    PYTHON Task 1 Create a class called Window It has 2 public properties 1 private property 1 constructor that takes 3 arguments pass each argument to the appropriate property Task 2 In a new file import the Window class Instantiate the Window object Output the two public properties Task 3 Alter the Window class in Task 1 Add an accessor and mutator (the ability to access and change) to the private property Task 4 Create a class named Instrument Give...

  • JAVA I. Using the Event Class created previously, create an array of objects;        II. Demonstrate...

    JAVA I. Using the Event Class created previously, create an array of objects;        II. Demonstrate passing array reference to a method;        III. Demonstrate creating array of objects that prints out only x 0.0 for all objects. PROJECT 5C - ARRAYS Create a new file called " EventArray5C". There are some "challenging" directions in this project. . 1.Prepare a document box (tell me that task(s) the application is to accomplish, how it will accomplish the tasks, the author of...

  • Please do it in JAVA!!! Thank you. Directions below Array Reverse :Step 1. In the reverse...

    Please do it in JAVA!!! Thank you. Directions below Array Reverse :Step 1. In the reverse method of AList, implement your algorithm from the pre-lab exercises. Iteration is needed. Checkpoint: Compile and run ArrayListExtensionsTest. The checkReverse tests should pass. If not, debug and retest. Array Cycle :Step 2. In the cycle method of AList, implement your algorithm from the pre-lab exercises. This method needs some form of iteration, but it may not be explicit. It can use the private methods...

  • Java Project Requirements: Account class Superclass Instance variables clearPassword String Must be at least 8 characters...

    Java Project Requirements: Account class Superclass Instance variables clearPassword String Must be at least 8 characters long encryptedPassword : String key int Must be between 1 and 10(inclusive) accountId - A unique integer that identifies each account nextIDNum – a static int that starts at 1000 and is used to generate the accountID no other instance variables needed. Default constructor – set all instance variables to a default value. Parameterized constructor Takes in clearPassword, key. Calls encrypt method to create...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT