Q:

Create the class TV with the following UML

0

Create the class TV with the following UML.

Class TV contains the following data members:

  • private channel: int between 0 and 99
  • private volumeLevel: int between 0 and 8
  • private on: boolean if on is true then TV is On, if on is false then its Off

 

Uml class diagram:

Tv

-volumeLevel: int

-channel: int

-on: boolean

+ turnOn(): void

+ turnOff():void

+ volumeLevelUp(int vol):void

+ volumeLevelDown(int vol):void

+ channelUp(int ch):void

+ channelDown(int ch):void

+ toString():String

Class TV contains the following methods:

  • public void turnOn(); Sets the variable on to true.
  • public void turnOff(); Sets the variable on to false.
  • public void volumeLevelUp(int vol); Increases volumeLevel by vol amount. However, volumeLevel must always be between 0 -8 for example, if volumeLevel is 5 and you called volumeLevelUp(3) then new volumeLevel Should be 5+3 = 8. (But volumeLevel must not exceed 8)
  • public void volumeLevelDown(int vol); Decreases volumeLevel by vol amount. However, volumeLevel must always be between 0 -8. for example, if volumeLevel is 5 and you called volumeLevelDown(3) then new volumeLevel Should be 5-3 = 2. (But volumeLevel must not go below zero)
  • public void channelUp(int ch); Increases channel number by ch. However, channel number must be between 0 – 99 (i.e. if channel is 90 and you go up 15 channels your new channel should be 5)
  • public void channelDown(int ch); Decreases channel number by ch. However, channel number must be between 0 – 99 (i.e. if channel is 10 and you go down 15 channels your new channel should be 95)
  • public String toString(); Returns all current information of the TV as a String. IF the TV is on it should return a string indicating TV is on in addition to volume and channel information. IF TV is off, it should return a message saying that. An example of the returned string is given below.

 

TV is on and current channel is 5 and current volume level is 8. 

 

Create a class testTV that will do the following:

Declare an object tv1 of the class TV, then display the following menu:

These are your choices:

1)Turn TV On              2)Turn TV Off             3)Channel Up           4)Channel Down         5)Increase Volume        6)Decrease Volume            7)Exit

Enter your choice :>

Then chose the following options(after each option is performd, you must print the status of the TV):

  • Turn on TV
  • Channel up by 50
  • Channel up by 60
  • Increase volume by 4
  • Increase volume by 7
  • Decrease volume by 3
  • Channel down by 90
  • Turn off TV

 

Feel free to add your own tests. (Note that java always initializes your int data members to 0, so the value of the channel and volumeLevel when you create the TV object is 0)

 

Sample Run

These are your choices:

1)Turn TV On             2)Turn TV Off             3)Channel Up               4)Channel Down            5)Increase Volume               6)Decrease Volume          7)Exit

Enter your choice :> 1

TV is On and current channel is 0 and volume level is 0 These are your choices:

1)Turn TV On            2)Turn TV Off            3)Channel Up          4)Channel Down            5)Increase Volume          6)Decrease Volume           7)Exit

Enter your choice :> 3

How many channels up? :>50

TV is On and current channel is 50 and volume level is 0 These are your choices:

1)Turn TV On             2)Turn TV Off             3)Channel Up           4)Channel Down            5)Increase Volume             6)Decrease Volume           7)Exit

Enter your choice :> 3

How many channels up? :>60

TV is On and current channel is 10 and volume level is 0

These are your choices:

1)Turn TV On           2)Turn TV Off         3)Channel Up        4)Channel Down         5)Increase Volume          6)Decrease Volume    7)Exit

Enter your choice :> 5

How much you like to increase? 4

TV is On and current channel is 10 and volume level is 4

These are your choices:

1)Turn TV On        2)Turn TV Off        3)Channel Up        4)Channel Down          5)Increase Volume         6)Decrease Volume         7)Exit

Enter your choice :> 5

How much you like to increase? 7

TV is On and current channel is 10 and volume level is 8

These are your choices:

1)Turn TV On        2)Turn TV Off         3)Channel Up          4)Channel Down        5)Increase Volume        6)Decrease Volume       7)Exit

Enter your choice :> 6

How much you like to decrease? 3

TV is On and current channel is 10 and volume level is 5

These are your choices:

1)Turn TV On           2)Turn TV Off            3)Channel Up           4)Channel Down         5)Increase Volume      6)Decrease Volume  7)Exit

Enter your choice :> 4

How many channels down? :>90

TV is On and current channel is 20 and volume level is 5

These are your choices:

1)Turn TV On         2)Turn TV Off         3)Channel Up         4)Channel Down           5)Increase Volume        6)Decrease Volume       7)Exit

Enter your choice :> 2

TV is Off

These are your choices:

1)Turn TV On         2)Turn TV Off          3)Channel Up        4)Channel Down         5)Increase Volume          6)Decrease Volume         7)Exit

Enter your choice :> 7

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Class Tv:

public class Tv {
    private int volumeLevel;
    private int channel;
    private boolean on;
    
    public void turnOn()
    {
        on=true;
    }
    public void turnOff()
    {
        on=false;
    }
    public void volumeLevelUp(int vol)
    {
        volumeLevel=volumeLevel+vol;
        if(volumeLevel>8)
            volumeLevel=8;
    }
    public void volumeLevelDown(int vol)
    {
        volumeLevel=volumeLevel-vol;
        if(volumeLevel<0)
            volumeLevel=0;
    }
    public void channelUp(int ch)
    { 
        channel=channel+ch;
        if(channel>99)
            channel=channel-99-1;
    }
    public void channelDown(int ch)
    {
        channel=channel-ch;
        if(channel<0)
            channel=channel+99-1;
    }
    public String toString()
    {
        String o="off";
        if(on==true)
           o="on"; 
        return "TV is "+o+" and current channel is "+channel+" and current volume\n" +
"level is "+volumeLevel+".";
    }
}

 

TestTv:

import java.util.Scanner;

public class TestTv {

    public static void main(String[] args) {
        Tv tv1=new Tv();
        int choice;
        do{
        System.out.println("These are your choices:\n" +
"1)Turn TV On 2)\tTurn TV Off \t3)Channel Up \t4)Channel Down\n" +
"\n5)Increase Volume \t6)Decrease Volume \t7)Exit\n" +
"Enter your choice :>");
        Scanner input=new Scanner(System.in);
        choice=input.nextInt();
        switch(choice)
        {
            case 1:
                tv1.turnOn();
                break;
            case 2:
                tv1.turnOff();
                break;
            case 3:
                System.out.print("enter the channel up level: ");
                int channelup=input.nextInt();
                tv1.channelUp(channelup);
                break;
            case 4:
                System.out.print("enter the channel down level: ");
                int channeldown=input.nextInt();
                tv1.channelDown(channeldown);                
                break;
            case 5:
                System.out.print("enter the volume up level: ");
                int volumeup=input.nextInt();
                tv1.volumeLevelUp(volumeup);                
                break;
            case 6:
                System.out.print("enter the volume down level: ");
                int volumedown=input.nextInt();
                tv1.volumeLevelDown(volumedown);                
                break;
            case 7:
                System.out.println("exit...");
                break;
            default:
                System.out.println("invalid entry!");
        }
        System.out.println(tv1.toString());
        }
        while(choice!=7);
    }
}

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now