Question

Help please 1) Define a Java class for defining the activity of a TV set. The class contains A private int data field named channel that specifies what channel is currently set for the tuner (channels range from 1 to 120, default is 1) a. b. A private int data field named volumeLevel that specifies the volume level of the TV (range is 1 to 7; default is 1) A private boolean data field named on that determines whether the set is on or off (default off) c. d. Include a no-arg constructor that creates a default TV e. Include getter (accessor) methods for each data field f. Include a volumeUp method to increase the volume by one level (up to the max) and a volumeDown method to decrease the volume by one level (lowest IS g. Include a channelUp method to increase the channel by one (up to 120) and a channelDown method to decrease the channel by one (minimum of 1) and a setChannel method to set the channel to a valid channel (between 1 and 120) Include a turnOn method to turn on the TV and a turnOff method to turn it h. i. Write a toStringmethod (returns a String) that outputs the current state of the data fields in a string (e.g., TV set: on Volume: 3 Channel: Write the UML diagram for the class. To keep it all in one place, include the UML diagram as documentation in your source code for the class (i.e., comment lines). Write a test program (TestTV) that creates two TV objects and demonstrates the use of the methods defined tor the class

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Television class

package abc1;

public class Television {

//Member variables as stated in question

private int channel;

private int volumeLevel;

private boolean status;

//getter methods

int getChannel() {

return channel;

}

int getVolume() {

return volumeLevel;

}

boolean getStatus() {

return status;

}

//no-arg constructor and default values of the variables

Television(){

channel = 1;

volumeLevel = 1;

status = false;

}

void volumeUp() {

if(volumeLevel<7) {

volumeLevel++;

}

System.out.println("Volume is " + volumeLevel);

}

void volumeDown() {

if(volumeLevel>0) {

volumeLevel--;

}

System.out.println("Volume is " + volumeLevel);

}

void channelUp() {

channel = (getChannel() +1);

if(channel>120)

channel = (channel % 120) -1 ;

System.out.println("Channel is " + getChannel() );

}

void channelDown() {

channel = channel - 1;

if(channel<0)

channel = channel + 121;

System.out.println("Channel is " + channel );

}

void setChannel(int i) {

if(i>=0 && i<=120)

channel = i;

}

void turnOn() {

status = true;

System.out.println("TV is switched on.");

}

void turnOff() {

status = false;

System.out.println("TV is switched off. ");

}

public String toString() {

String str = "";

str = str + "TV set: ";

if(status==true)

str = str + "on ";

else

str = str + "off ";

str = str + " Volume: " + volumeLevel + " ";

str = str + " Channel: " + channel;

return str;

}

}

Test program

package abc1;

public class TestTv {

public static void main(String[] args) {

Television tv1, tv2;

tv1 = new Television();

tv2 = new Television();

System.out.println("Is TV 1 switched on.");

System.out.println(tv1.getStatus());

System.out.println("Is TV 2 switched on.");

System.out.println(tv2.getStatus());

System.out.println("Turning on TV 1 ");

tv1.turnOn();

System.out.println("Turning on TV 2 ");

tv2.turnOn();

System.out.println("Checking channel no on TV 1 ");

System.out.println(tv1.getChannel());

System.out.println("Checking channel no on TV 2 ");

System.out.println(tv2.getChannel());

System.out.println("Checking volume level on TV 1 ");

System.out.println(tv1.getVolume());

System.out.println("Checking volume level on TV 2 ");

System.out.println(tv2.getVolume());

System.out.println("Is TV 1 swithced on. ");

System.out.println(tv1.getStatus());

System.out.println("Is TV 2 switched on. ");

System.out.println(tv2.getStatus());

System.out.println("Setting TV 1 to channel 0 ");

tv1.setChannel(0);

System.out.println(tv1.toString());

System.out.println("Setting TV 2 to channel 120 ");

tv2.setChannel(120);

System.out.println(tv2.toString());

System.out.println("Increasing channel no on TV 1 ");

tv1.channelUp();

System.out.println("Increasing channel no on TV 2 ");

tv2.channelUp();

System.out.println("Decreasing channel no on TV 1 ");

tv1.channelDown();

System.out.println("Decreasing channel no on TV 2 ");

tv2.channelDown();

System.out.println("Increasing volume on TV 1 ");

tv1.volumeUp();

System.out.println("Increasing volume on TV 2");

tv2.volumeUp();

System.out.println("Decreasing volume on TV 1 ");

tv1.volumeDown();

System.out.println("Decreasing volume on TV 2 ");

tv2.volumeDown();

System.out.println(tv1.toString() + "\n" + tv2.toString());

}

}

Code screenshot for Television class

Code screenshot for Test class

Sample output

Add a comment
Know the answer?
Add Answer to:
Help please 1) Define a Java class for defining the activity of a TV set. The...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • ** IN JAVA **: Design and implement class Radio to represent a radio object. The class defines th...

    ** IN JAVA **: Design and implement class Radio to represent a radio object. The class defines the following attributes (variables) and methods:Assume that the station and volume settings range from 1 to 10.A private variable of type int named station to represent a station number. Set toA private variable of type int named volume to represent the volume setting. Set to 1.A private variable of type boolean named on to represent the radio on or off. Set to false.A...

  • 1. Please write the following program in Python 3. Also, please create a UML and write...

    1. Please write the following program in Python 3. Also, please create a UML and write the test program. Please show all outputs. (The Fan class) Design a class named Fan to represent a fan. The class contains: Three constants named SLOW, MEDIUM, and FAST with the values 1, 2, and 3 to denote the fan speed. A private int data field named speed that specifies the speed of the fan. A private bool data field named on that specifies...

  • SOLVE IN PYTHON: Exercise #2: Design and implement class Radio to represent a radio object. The c...

    SOLVE IN PYTHON: Exercise #2: Design and implement class Radio to represent a radio object. The class defines the following attributes (variables) and methods: Assume that the station and volume settings range from 1 to 10. A private variable of type int named station to represent a station number. Set to 1. A private variable of type int named volume to represent the volume setting. Set to 1. A private variable of type boolean named on to represent the radio...

  • PYTHON Exercise #2: Design and implement class Radio to represent a radio object. The class defines...

    PYTHON Exercise #2: Design and implement class Radio to represent a radio object. The class defines the following attributes (variables) and methods: Assume that the station and volume settings range from 1 to 10. 1. A private variable of type int named station to represent a station number. Set to 1. 2. A private variable of type int named volume to represent the volume setting. Set to 1. 3. A private variable of type boolean named on to represent the...

  • please write in Java and include the two classes Design a class named Fan (Fan.java) to...

    please write in Java and include the two classes Design a class named Fan (Fan.java) to represent a fan. The class contains: An int field named speed that specifies the speed of the fan. A boolean field named fanStatus that specifies whether the fan is on (default false). A double field named radius that specifies the radius of the fan (default 5). A string field named color that specifies the color of the fan (default blue). A no-arg (default) constructor...

  • using visual studios C# implementing using system; be sure I can copy and paste. Exercise #2:...

    using visual studios C# implementing using system; be sure I can copy and paste. Exercise #2: Design and implement class Radio to represent a radio object. The class defines the following attributes (variables) and methods: Assume that the station and volume settings range from 1 to 10. 1. A private variable of type int named station to represent a station number. Set to 1. 2. A private variable of type int named volume to represent the volume setting. Set to...

  • Note: According to the question, please write source code in java only using the class method....

    Note: According to the question, please write source code in java only using the class method. Sample Run (output) should be the same as displayed in the question below. Make sure the source code is working properly and no errors.​ Exercise #2: Design and implement class Radio to represent a radio object. The class defines the following attributes (variables) and methods Assume that the station and volume settings range from 1 to 10 1. A private variable of type int...

  • THE FOLLOWING PROGRAM IS NEEDED IN JAVA LANGUAGE Design a class torepresent account, include the following...

    THE FOLLOWING PROGRAM IS NEEDED IN JAVA LANGUAGE Design a class torepresent account, include the following members: -Data Members a)Name of depositor-Stringb)Account Number –intc)Type of account –Boolean d)Balance amount -doublee)AnnualInterestrate -double Methods: -(a)To assign initial values (use constructor)(b)To deposit an amount.(c)TO withdraw amount with the restriction the minimum balance is 50 rs. Ifyouwithdraw amount reduced the balance below 50 then print the error message.(d)Display the name and balance of the account.(e)Get_Monthly_intrestRate() -Return the monthly interestrate whichis nothing but Annualintrestrate/12. Annualinterestrate...

  • (The Account class) Design a class named Account that contains: A private int data field named...

    (The Account class) Design a class named Account that contains: A private int data field named id for the account (default 0). A private double data field named balance for the account (default 0). A private double data field named annualInterestRate that stores the current interest rate (default 0). Assume all accounts have the same interest rate. A private Date data field named dateCreated that stores the date when the account was created. A no-arg constructor that creates a default...

  • 9.7 (The Account class) Design a class named Account that contains: • A private int data...

    9.7 (The Account class) Design a class named Account that contains: • A private int data field named id for the account (default o). • A private double data field named balance for the account (default o). • A private double data field named annualInterestRate that stores the current interest rate (default o). Assume that all accounts have the same interest rate. • A private Date data field named dateCreated that stores the date when the account was created. •...

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
ADVERTISEMENT