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); }}
en fait, cela semble être un exercice de classe. Voulez-vous vraiment que les gens sur StackOverflow résolvent vos devoirs pour vous? – Nessuno
Rappelez-vous que 'if (power == true)' est mieux écrit 'if (power)' – user1717259
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