I have a project for my Java class in eclipse online study and I am having problem in understanding something. In my books there is no explanation to this situation and my teacher is useless.
My project is to create an object class Television and to change to power from On to Off and only when the power is on I must change the channel 5 times and the volume one time.
I understand that I have to create a Boolean method if(power==true)
but I do not know how to do that and how to combine it with my code.
Here is my code:
public class TelevisionDemo {
public static void main(String[] args) {
//create television object
Television tv = new Television ();
//invoke call methods on the object tv
tv.changeChannel (1);
tv.changeVolume (8);
tv.printStatus ();
System.out.println("I will change the the volume one time and the channel 5 times");
tv.changeChannel(2); tv.changeVolume(6); tv.printStatus();
tv.changeChannel(3); tv.printStatus();
tv.changeChannel(4); tv.printStatus();
tv.changeChannel(8); tv.printStatus();
tv.changeChannel(5); tv.printStatus();
}
}
//this is the blueprint
class Television {
boolean power = true; //create a method for tv powerOnOff
int channel = 0;
int volume = 0;
void changeChannel (int newValue){//method to change the channel
channel = newValue;
}
void changeVolume (int newValue){ //method to change the volume
volume = newValue;
}
void printStatus(){ //printing the status of the Television, channel and volume
System.out.println("Channel: " + channel + " Volume: " + volume);
}
}
I have created a method powerOn
, powerOff
and invoked/called it in the main method but I still don't know how to all the parameter that let me only change channel and volume when the TV power is On.
Any one can help clear this issue for me please?
Here is my code:
public class TelevisionDemo {
public static void main(String[] args) {
Television tv = new Television ();//create television object
//invoke call methods on the object tv
tv.powerOn();
tv.powerOff();
tv.changeChannel (1);
tv.changeVolume (2);
tv.printStatus ();
System.out.println("I will change the the volume one time and the channel 5 times");
tv.changeChannel(2); tv.changeVolume(6); tv.printStatus();
tv.changeChannel(3); tv.printStatus();
tv.changeChannel(4); tv.printStatus();
tv.changeChannel(8); tv.printStatus();
tv.changeChannel(5); tv.printStatus();
System.out.println("I will change the status of the television");
tv.powerOff();
System.out.println("The television is now closed");}}
class Television { //this is the blueprint
boolean power = true; //create a method for tv powerOnOff
int channel = 0;
int volume = 0;
void powerOn(){ //method for power On
power = true; }
void powerOff (){//method for power Off
power = false; }
void changeChannel (int newValue){//method to change the channel
channel = newValue;}
void changeVolume (int newValue){ //method to change the volume
volume = newValue;}
void printStatus(){ //printing the status of the Television, channel and volume
System.out.println("The TV staus is powerOn: " + "Channel: " + channel + " Volume: " + volume); }}