2015-03-04 1 views
-2

J'ai un projet pour mon cours Java sur l'étude en ligne d'Eclipse et j'ai des difficultés à comprendre quelque chose. Dans mes livres il n'y a aucune explication à cette situation et mon professeur est inutile. Mon projet est de créer une classe d'objets Télévision et de passer à l'alimentation de On à Off et seulement quand l'alimentation est allumée je dois changer le canal 5 fois et le volume une fois. Je comprends que je dois créer une méthode booléenne if (power == true) mais je ne sais pas comment le faire et comment le combiner avec mon code. Voici mon code:Mise sous tension ou hors tension du code Java

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); 
    } 
} 

J'ai créé une méthode powerOn POWEROFF et invoqué/appelé dans la méthode mais je ne sais toujours pas comment tous les paramètres qui me permet de changer seul canal et le volume lorsque le téléviseur est allumé. Tout le monde peut aider à résoudre ce problème s'il vous plaît? Voici mon 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); }} 
+4

en fait, cela semble être un exercice de classe. Voulez-vous vraiment que les gens sur StackOverflow résolvent vos devoirs pour vous? – Nessuno

+1

Rappelez-vous que 'if (power == true)' est mieux écrit 'if (power)' – user1717259

+0

Ce n'est pas un exercice de classe, mais un projet à envoyer à l'école dans une étude en ligne. Oui, je voudrais vraiment que les gens sur Stack Overflow m'aident à apprendre comment résoudre mon problème, c'est pourquoi j'ai publié ma question. Mon professeur m'a écrit que je devais ajouter if (power = = true). – Eli

Répondre

2

Créer 3 méthodes pseudo-code:

TurnOn() 
    power = true; 

TurnOff() 
    power = false; 

IsOn() 
    return power; 

Puis, en principal ou appelant:

if(tv.IsOn()) 
    { 
     while(i<5) 
     tv.changeChannel(i++); 
     tv.changeVolume(x); 
    } 
+0

Merci pour vos réponses, je suis vraiment débutant donc je ne sais pas comment appeler la méthode TurnOn TurnOff dans la méthode principale et je ne sais pas ce qu'est un pseudo code autre que cela ne signifie pas réel. – Eli

+0

J'ai créé une méthode powerOn powerOff et l'ai invoquée/appelée dans la méthode principale mais je ne sais toujours pas comment tous les paramètres me permettent de changer de canal et de volume quand le téléviseur est allumé. Tout le monde peut aider à résoudre ce problème s'il vous plaît? Voici mon code: – Eli

+0

@ElivanBScho Que voulez-vous dire? La boucle while dans ma réponse est un exemple de comment changerChannel. –

2

Ajouter à votre Cours de télévision

void powerOn() { 
    power = true; 
} 

void powerOff() { 
    power = false; 
} 
0

Je fini par résoudre ce problème en ajoutant à la méthode dans la classe de télévision paramètre si (puissance == true) et dans la principale méthode que j'invoquais/appelé après System.out.println et voici mon code final: 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); 
    } 
    }