0

i can't get the value of a variable(volume) that's inside the constructor, the other 2 variables(boolean on and play) i can manage no problem. When i try to call the openMenu() method in my object it doesn't display the volume variable, it should start at 50 when i create a stance, i tested the volumUp and volumDown, and they add to the volume, but the initial value is not set.

public class ControleClass implements controle_Interface {

private int volume;
private boolean on;
private boolean play;



public void  ControleClass (){
    this.volume = 50;
    this.on = true;
    this.play = false;

}

public int getVolume() {
    return this.volume;
}

public boolean isOn() {
    return this.on;
}

public boolean isPlay() {
    return this.play;
}

public void setVolume(int volume) {
    this.volume = volume;
}

public void setOn(boolean on) {
    this.on = on;
}

public void setPlay(boolean play) {
    this.play = play;
}

@Override
public void on() {
    this.setOn(true);
}

@Override
public void off() {
    this.setOn(false);
}

**@Override
public void openMenu() {
    System.out.println("Is on? " + this.isOn()); **//it shows this status np**
    System.out.println("Is playing? " + this.isPlay());//Also this one
    System.out.println("Volume " + this.getVolume());//But not this one
    for (int i = 0; i < this.getVolume(); i += 5){
        System.out.println("V ");
    }**

}

@Override
public void closeMenu() {
    System.out.println("Closing menu...");
}

@Override
public void volumUp() {
    if (this.isOn() && this.volume < 100){
      this.setVolume(getVolume() + 5); 
    }
}

@Override
public void volumDown() {
    if (this.isOn() && this.volume >= 0){
        this.setVolume(getVolume() - 5);
    }
}

@Override
public void muteOn() {
    if(this.isOn()){
       this.setVolume(0);
    }
}

@Override
public void muteOff() {
    if(this.isOn()){
        this.setVolume(50);
    }
}

@Override
public void play() {
    if(this.isOn() && !(this.isPlay())){
        this.setPlay(true);


    }
}

@Override
public void pause() {
   if(this.isOn() && this.isPlay()){
       this.setPlay(false);
   }
}
Big Nerd
  • 17
  • 1
  • 6
  • 1
    Change ´public void ControleClass (){´ to ´public ControleClass(){´ – KilledByCheese Aug 29 '17 at 01:05
  • thanks i did and it works, but i don't get why it showed the 2 booleans values but didn't show the int, i know (now) that what i was trying to do was flawed but how come it "kinda" worked? – Big Nerd Aug 29 '17 at 01:12
  • If you create your ControleClass with 'new ControleClass()' the default constructor is called (https://stackoverflow.com/questions/4488716/java-default-constructor) take a closer look at this https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html under the topic Default Values is the answer - "Fields that are declared but not initialized will be set to a reasonable default by the compiler." – KilledByCheese Aug 29 '17 at 04:53

0 Answers0