0

So I, have this code to trace and I had the results of 2.25 and 2.75. But when I compile it, I get 1.5 and 2.0. Why is that? Do the parentheses have anything to do with that?

public class TraceClass {
    private double valBefore;
    private double valAfter;

    public TraceClass(double valIn) {
        this.valBefore = valIn;
        this.valAfter = 0.0;
    }

    public void doIt(boolean which){
        if (which == true) {
            this.valAfter = ((int) this.valBefore) + .5;
        }
        else {
            this.valAfter = (int) (this.valBefore + .5);
        }
    }

    public double getValAfter(){
        return this.valAfter;
    }
}
public class MainClass {
    public static void main(String[] args) {

        TraceClass traceObj = new TraceClass(1.75);

        traceObj.doIt(true);
        double temp = traceObj.getValAfter();
        System.out.println("Result is " + temp);

        traceObj.doIt(false);
        temp = traceObj.getValAfter();
        System.out.println("Result is " + temp);
    }
}

edit: this is code that my teacher gave out ask practice for stack tracing. i got 2.25 because I added 1.75+.5= 2.25. But then I accidentally added .5 to 2.25 to get 2.75 edit2: typo

4
  • May I ask - why are you using int at all? Commented Mar 4, 2019 at 21:38
  • 1
    Welcome to StackOverflow! You should improve your question by explaining what you did exactly to get the results 2.25 and 2.75. In what way did you execute the code? Commented Mar 4, 2019 at 21:41
  • Now I am irritated - did you realy get 2.25 or was it rather 1.25? Commented Mar 4, 2019 at 22:29
  • So.... if I understand correctly, you only expected to get the 2.25 and 2.75 results, but never actually got those results by executing the code? And you want to know why? The existing answers should explain why you get 1.5 and 2.0. If that is not what you intended to ask, then you should clarify your question. Commented Mar 5, 2019 at 0:35

3 Answers 3

3

Both the parentheses and the casts to int affect the result, and the order matters.

For the true case, valBefore is casted to int first, yielding the integer value 1 (it is truncated). Then .5 is added, a double value, so 1 is widened to 1.0 and 1.5 results.

For the false case, valBefore is added to 0.5 first, and 1.5 + .5 is 2.0. Then that result is cased to int which yields 2. The assignment back to the double variable valAfter widens it back to double -- 2.0.

Sign up to request clarification or add additional context in comments.

Comments

0

You are casting your variable to int for some strange reason which means you are adding 1 (integer part of 1.75) and 0.5 = 1.5 and in the second case you are casting the result of the addition to int so you get 1.5 + 0.5 = 2 (then casted to double again), so the second time you do not lose anything since the result happened to be an even integer.

Just remove all cast to int, it makes no sense when calculating with double values

1 Comment

Yeah this is my professor's code that they wanted us to trace. I was confused with the calculations too. I guess they wanted us to see the difference in the two. Thanks by the way!
0

When you cast the double value into an int, it just does not count the decimal parts. I just convert an int to double. you don't need to cast either.

import org.junit.jupiter.api.Test;

public class DoubleIssueStackOverflow {

@Test
public void test_first(){
    TraceClass traceObj = new TraceClass(1.75);

    traceObj.doIt(true);
    double temp = traceObj.getValAfter();
    System.out.println("Result is " + temp);

    traceObj.doIt(false);
    temp = traceObj.getValAfter();
    System.out.println("Result is " + temp);

}
public class TraceClass {
    private double valBefore;
    private double valAfter;

    public TraceClass(double valIn) {
        this.valBefore = valIn;
        this.valAfter = 0.0;
    }

    public void doIt(boolean which) {
       if (which == true) {
            this.valAfter = this.valBefore + .5;
        } else {
            this.valAfter = this.valBefore + .5;
        }
    }

    public double getValAfter() {
        return this.valAfter;
    }
}

}

1 Comment

Well right ... But I think the teacher intentionally put the casts in that method. To point out the different results depending on the different casts. If you remove them there is no point in the which parameter of the method and the "if" makes no sense anymore... :)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.