Television

Write a Java program that simulates the operation of a Television. The program should be able to change the channel by increasing or decreasing the channel by one or by inputting the specific channel. It should also be able to adjust the volume by increasing or decreasing the volume by one.

Using class decomposition this program can be decomposed into two classes: a Television class and a WatchTV class. The WatchTV class will be a client of the Television class. It will also contain the main method for the program.

Directions

Television class

The Television class will store the television's current channel and volume setting. It can be decomposed into the following methods.

Create a file named Television.java and add the following code.

public class Television
{
    // constants
    public final int MAX_CHANNEL = 100;
    public final int MIN_CHANNEL = 1;
    public final int MAX_VOLUME  = 10;
    public final int MIN_VOLUME  = 1;
    public final int DEFAULT_CHANNEL = 3;
    public final int DEFAULT_VOLUME  = 5;
    
    // instance variables
    private int channel;
    private int volume;
    
    // constructors
    public Television()
    {
        
    }
    
    public Television(int c, int v)
    {
        
    }
    
    // accessor methods
    public int getChannel()
    {
        
    }
    
    public int getVolume()
    {
        
    }
	
    // mutator methods
	
    /* This method increases the channel by one.
     * If the channel increase exceeds the value of MAX_CHANNEL
     * then the channel should be set to MIN_CHANNEL.
     */
    public void increaseChannel()
    {
        
    }
    
    /* This method decreases the channel by one.
     * If the channel decrease exceeds the value of MIN_CHANNEL
     * then the channel should be set to MAX_CHANNEL.
     */
    public void decreaseChannel()
    {
        
    }
    
    /* This method changes the channel to the value specified
     * in the parameter list. If the value of the parameter is 
     * not a valid channel number then the channel should 
     * remain unchanged.
     * @param c the channel to be changed to
     */
    public void selectChannel(int c)
    {
       
    }
    
    /* This method increases the volume by one.
     * If MAX_VOLUME is reached a call to increaseVolume 
     * should have no effect.
     */
    public void increaseVolume()
    {
        
    }
    
    /* This method decreases the volume by one.
     * If MIN_VOLUME is reached a call to decreaseVolume 
     * should have no effect.
     */
    public void decreaseVolume()
    {
        
    }
}

Instance Variables

Two instance variables have been defined named channel and volume.

Constructors

  1. Implement the two constructors.

Accessor Methods

  1. Implement the accessor methods getChannel and getVolume.

Mutatator Methods

  1. Implement the increaseChannel method. This method increases the channel by one. If the channel increase exceeds the value of MAX_CHANNEL then the channel should be set to MIN_CHANNEL (wrap around to the beginning).
  1. Implement the decreaseChannel method. This method decreases the channel by one. If the channel decrease exceeds the value of MIN_CHANNEL then the channel should be set to MAX_CHANNEL (wrap around to the end).
  1. Implement the selectChannel method. This method changes the channel to the value specified in the parameter list. If the value of the parameter is not a valid channel number then the channel should remain unchanged.
  1. Implement the increaseVolume method. This method increases the volume by one. If MAX_VOLUME is reached a call to increaseVolume should have no effect.
  1. Implement the decreaseVolume method. This method decreases the volume by one. If MIN_VOLUME is reached a call to decreaseVolume should have no effect.


Client Program - WatchTV class

The WatchTV class has been provided for you. No modifications are required.

Create a file named WatchTV.java and add the following code.

import java.util.*;

public class WatchTV
{
    public static void main(String[] args)
    {
        Television tv = new Television();
        Scanner keyboard = new Scanner(System.in);
        int sel = 0;
        
        System.out.println("**********************");
        System.out.println("*   My Television    *");
        System.out.println("**********************");
        
        do
        {
           System.out.println();
           System.out.println("   Current Settings");
           System.out.println("=====================");
           System.out.println(" Channel = " + tv.getChannel());
           System.out.println(" Volume  = " + tv.getVolume());
           System.out.println("=====================");
           System.out.println("1. Increase Channel");
           System.out.println("2. Decrease Channel");
           System.out.println("3. Select Channel");
           System.out.println("4. Increase Volume");
           System.out.println("5. Decrease Volume");
           System.out.println("6. Turn TV off");
           System.out.println();
           System.out.print("Make Selection -->");
           sel = keyboard.nextInt();
           
            if(sel == 1)
            {
                tv.increaseChannel();
            }
            if(sel == 2)
            {
                tv.decreaseChannel();
            }
            if(sel == 3)
            {
                System.out.println();
                System.out.print("Enter channel -->");
                int channel = keyboard.nextInt();
                tv.selectChannel(channel);
            }
            if(sel == 4)
            {
                tv.increaseVolume();
            }
            if(sel == 5)
            {
                tv.decreaseVolume();
            }   
        }
        while(sel != 6);
        
        System.out.println();
        System.out.println("TV is off, thanks for watching!");
        System.out.println();
        System.out.println();
    }
}

Source File

Television.java

WatchTV.java

Sample Run

**********************
*   My Television    *
**********************

   Current Settings
=====================
 Channel = 3
 Volume  = 5
=====================
1. Increase Channel
2. Decrease Channel
3. Select Channel
4. Increase Volume
5. Decrease Volume
6. Turn TV off

Make Selection -->1

   Current Settings
=====================
 Channel = 4
 Volume  = 5
=====================
1. Increase Channel
2. Decrease Channel
3. Select Channel
4. Increase Volume
5. Decrease Volume
6. Turn TV off

Make Selection -->1

   Current Settings
=====================
 Channel = 5
 Volume  = 5
=====================
1. Increase Channel
2. Decrease Channel
3. Select Channel
4. Increase Volume
5. Decrease Volume
6. Turn TV off

Make Selection -->1

   Current Settings
=====================
 Channel = 6
 Volume  = 5
=====================
1. Increase Channel
2. Decrease Channel
3. Select Channel
4. Increase Volume
5. Decrease Volume
6. Turn TV off

Make Selection -->3

Enter channel -->51

   Current Settings
=====================
 Channel = 51
 Volume  = 5
=====================
1. Increase Channel
2. Decrease Channel
3. Select Channel
4. Increase Volume
5. Decrease Volume
6. Turn TV off

Make Selection -->4

   Current Settings
=====================
 Channel = 51
 Volume  = 6
=====================
1. Increase Channel
2. Decrease Channel
3. Select Channel
4. Increase Volume
5. Decrease Volume
6. Turn TV off

Make Selection -->4

   Current Settings
=====================
 Channel = 51
 Volume  = 7
=====================
1. Increase Channel
2. Decrease Channel
3. Select Channel
4. Increase Volume
5. Decrease Volume
6. Turn TV off

Make Selection -->5

   Current Settings
=====================
 Channel = 51
 Volume  = 6
=====================
1. Increase Channel
2. Decrease Channel
3. Select Channel
4. Increase Volume
5. Decrease Volume
6. Turn TV off

Make Selection -->5