-1
    /*
     * Write Java code to execute the following fraction,
     * if a = 3 , b = 5, c = 6, d = 4 (declare these as int type variables).
     * numerator  = a * b / (d + c)
     * denominator = c % d - b + a
     * results = numerator/denominator.
     * output 'results'
     */
    int a = 3;
    int b = 5;
    int c = 6;
    int d = 4;

    double numerator = ((float)a * (float)b / ((float) d + (float) c));

    double denominator = ((float)c % (float)d - (float)b + (float) a);

    double results = numerator / denominator;

    System.out.println("Results: " + results);





    /* Expected output should be -0.1666666716337204
     * Show your hand calculation to verify correctness of 'results'.
     */

The comments are the goal of the project. I have to make a simple program that take's those numbers and puts it into the formula (as shown). However, I can't seem to get the number to match exactly with the expected output. This assignment was giving the day we learned about "Casting" in Java. So I assume it's something to do with that. I've gotten as close as -.1666666666666667. Is it something simple i'm looking over?

  • 6
    This would the be the right time to (learn to) use a debugger to inspect the (intermediate) values you have (and compare to what you'd expect). A small hint `numerator` is quite likely not the value you want it to be – UnholySheep Sep 08 '17 at 13:39
  • The numerator from what I can see should be 1.5. At the moment in the example I gave its 1.0. I've gotten it to be 1.5 before, I believe the denominator is where the problem is yes? I'm using NetBeans IDE, is there a good debugger built-in or should I use an external one. – TheBouv SE Sep 08 '17 at 13:51
  • Given those values and the formula `denominator` is always going to be 0 (which will lead to an `infinity` value for `result`) - this isn't even a programming problem, it's just basic mathematics (which means I'm not sure how your teacher expects an output of `-0.1666666716337204`, as that is not what this calculation should yield) – UnholySheep Sep 08 '17 at 14:04
  • I think I need to change the expression. With like parenthesis and stuff to produce that output. Well if it isn't a programming thing, I guess i'll just ask my professor. Thanks! – TheBouv SE Sep 08 '17 at 14:15
  • Your denominator is 0: c % d - b + a = 6 % 4 - 5 + 3 = 2 - 5 + 3 = 0. – Mark Sep 08 '17 at 14:19
  • Possible duplicate of [Float and double datatype in java](https://stackoverflow.com/questions/27598078/float-and-double-datatype-in-java) – Bernhard Barker Sep 08 '17 at 14:27
  • I'm aware @Mark. However I don't know how to fix the situation. – TheBouv SE Sep 08 '17 at 14:40
  • You're not supposed to fix it, you don't fix "2 + 2 =4". These are the given number and infinity is the correct result. The numbers you were given might be off. – Mark Sep 08 '17 at 15:03

1 Answers1

0

To understand better, break the equation into parts.

    float left;
    float right;
    int a = 3;
    int b = 5;
    int c = 6;
    int d = 4;

    // Your equation ...
    //double numerator = ((float)a * (float)b / ((float) d + (float) c));
    left = ((float)a * (float)b) ;
    right = ((float) d + (float) c);
    double numerator = left / right;
    System.out.println("Numerator (" + numerator + ") = "  + left + " / " + right );

   // Prints: "Numerator (1.5) = 15.0 / 10.0"

    left = (float)c % (float)d;
    right = (float)b + (float) a;
    //double denominator = ((float)c % (float)d - (float)b + (float) a);
    double denominator = left - right;
    System.out.println("Denominator (" + denominator + ") = "  + left + " - " + right );

    // Prints "Denominator (-6.0) = 2.0 - 8.0"

    double results = numerator / denominator;
    System.out.println("Results (" + results + ") = "  + numerator + " / " + denominator );

    // Prints "Results (-0.25) = 1.5 / -6.0"

    System.out.println("Results: " + results);
    // Prints: "Results: -0.25"
older coder
  • 614
  • 1
  • 4
  • 12
  • I need to get the result of -0.1666666716337204 by changing the expression though. – TheBouv SE Sep 08 '17 at 15:34
  • I can change the numerator to use PEMDAS to get a result of -1.625. I've played around with parenthesis to get various values, but not your answer. – older coder Sep 08 '17 at 16:03
  • I think it has to do with casting to get a more precise decimal or something. This assignment was given in a packet about Casting/Overflow/Underflow – TheBouv SE Sep 08 '17 at 16:16