Setting a boolean value in Constructor, changing it in a method, return it in another

Viewed 113

I'm a newbie yet, so please feel free to accuse me of asking silly things xD. I just started coding. So I want to specify my question to make it clear for you. I'm stuck regarding this: We need a constructor (public DoggoII) which sets our value to false. Then we need a method (makeGoodBoi()) to set the value to true and then I need another method (isGoodBoi()) to return the value of the private field goodBoi and System.out.print some stuff later. Consider the rest of the code as done. Can someone give me a hint or something on how to do that? Because I'm kinda lost.

The question is if I have a fault that I can't find and how to return a boolean value in another method in general. Thanks!

public class Doggo {
private String name;
private boolean goodBoi;

public Doggo(String name){
    this.name = name;
}

public String getName() {
    return name;
}

public void makeBark() {
    System.out.println(name + " said: Woof woof");
}


public Doggo (boolean goodBoi){
    this.goodBoi= false;
}

public void makeGoodBoi(){
    this.goodBoi = !this.goodBoi;
}

public void isGoodBoi(){
    if (makeGoodBoi()){
        return;
    }
}

public void whosAGoodBoi() {
    if (isGoodBoi()) {
        System.out.println(name + " is such a good boii");
    } else {
        System.out.println(name + " is not a good boi :(");
    }
}


public static void main(String[] args) {
    Doggo dog = new Doggo("Casper");
    System.out.println(dog.getName());
    dog.makeBark();
    
}

}

2 Answers

Just a basic getter, use boolean as return type instead of void.

public boolean isGoodBoi() {
    return goodBoi;
}

Since goodBoi is a class member and by default boolean class members are false, so you don’t have to do anything except add a getter

public boolean isGoodBoi() {
       return goodBoi;
}

This will send whatever current value of class member is.

So getting this would be as simple as;

DOGGO_OBJECT.isGoodBoi();

Then we need a method (makeGoodBoi()) to set the value to true and then I need another method (isGoodBoi()) to return the value of the private field goodBoi and System.out.print some stuff later.

public void makeGoodBoi() {
 this.goodBoi = true;
}
Related