Hello, I'm trying to make a very basic + - calculator in java using constructors after watching thenewboston video tutorial I tried to experiment with it. My litle project is almost finished my only problem is that the boolean I use is being reset after I assign it. If someone could take a look at I would really appriciate it.
Also ignore the names of the class didn't bother to change it after the tutorial video.

package bucky;
public class tuna {
    private double num1;
    private double num2;
    private double awnser;
    public boolean plus;

    public tuna(double numbr1,double numbr2){
        num1 = numbr1;
        num2 = numbr2;
    }

    public tuna(String opr) {

        if(opr.contentEquals("+")){
            plus = true;
        }
        else if(opr.contentEquals("-")){
            plus = false;
        }
    }

    public void saying(){
        System.out.print(plus);
        if(plus == true){
            awnser = num1 + num2;
        }else if(plus == false){
            awnser = num1 - num2;
        }
        System.out.println(awnser);
    }
}

Recommended Answers

All 2 Replies

There's nothing in your code that would reset the value of plus. However with your constructors the way they are, there is no way to create a tuna object that has the numbers as well plus set to a meaningful value.

So I'm guessing when you use your code, you're actually creating two tuna objects: one with the correct value for plus and one with the correct values for num1 and num2. Since each object has its own instance variables, one object's value for plus won't affect the behavior of the other object.

affect the behavior of the other object.

I don't quite understand what you are telling do you mean that because of the two tunaObjects that I'm unable to change the boolean? Or do you mean that my mistake has to be somewhere else?
Sorry, I'm a bit confused.

-Edit-
Oh wait I understand now thanks for the help n.n "Since each object has its own instance variables" didn't really understand this but now I do.

Since each object has its own instance variables

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.