-1

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); }}
Elivan K.
  • 285
  • 1
  • 3
  • 13
  • 4
    Actually this seems like a class exercise. Do you really want people on StackOverflow to solve your homework for you? – Sören Titze Mar 04 '15 at 14:47
  • 1
    Remember that `if(power==true)` is better written `if(power)` – user1717259 Mar 04 '15 at 14:47
  • It is not a class exercise but a project to send to the school in an online study. Yes I would really like people on Stack Overflow to help me learn how to solve my problem, that is why I have published my question. My teacher wrote to me that I need to add if(power = = true). – Elivan K. Mar 04 '15 at 17:23

3 Answers3

2

Create 3 methods in pseudocode:

TurnOn()
  power = true;

TurnOff()
  power = false;

IsOn()
  return power;

Then in Main or caller:

    if(tv.IsOn())
    {
      while(i<5)
        tv.changeChannel(i++);
      tv.changeVolume(x);
    }
Lews Therin
  • 10,907
  • 4
  • 48
  • 72
  • Thank you for your answers, I am a genuinely beginner so I do not know how to invoke the method TurnOn TurnOff in main method and I do not know what is a pseudo code other than it means not real. – Elivan K. Mar 04 '15 at 17:19
  • 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: – Elivan K. Mar 04 '15 at 18:24
  • @ElivanBScho What do you mean? The while loop in my answer is an example of how to changeChannel. – Lews Therin Mar 04 '15 at 18:59
2

Add this to your Television class.

void powerOn() {
    power = true;
}

void powerOff() {
    power = false;
}
Promination
  • 156
  • 6
0

So, I ended up solving this issue by adding to the method in class Television. The parameter if(power==true) and in the main method I invoked/called it after System.out.println.

Here is my final 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.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.powerOn();               tv.changeVolume(6);
    tv.changeChannel(2);        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");

}
    }




     //this is the blueprint
 class Television {

    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 
        if (power==true)
        channel = newValue; 
 }
        void changeVolume (int newValue){ //method to change the volume 
            if (power==true)
            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);
    }
    }
Elivan K.
  • 285
  • 1
  • 3
  • 13