1

I need an help to figure out what happened. I am an Italian speaker so some variable I have name it in Italian. After some time of no-coding, I've started to code something basic and easy. I made a Class named "Prova" which was a calculator. 4 methods: add, minus, product and divide. First three method had no anomaly, the last one had. I made a runnable class and saw some strange result

Here's the code:

    public class Prova {
    public int add(int addendo1, int addendo2) {
       int res;
       res = addendo1+addendo2;
       return res;
    }
    public int minus(int minuendo, int sottraendo) {
       int res;
       res = minuendo-sottraendo;
       return res;
    }
    public int multiply(int fattore1, int fattore2) {
       int res;
       res = fattore1*fattore2;
       return res;
    }
    public double divide(int divisore, int dividendo) {
       double res;
       res = divisore/dividendo;
       return res;
    }`

Here's the command: public static void main(String[] args) {

    Prova somma = new Prova();
    System.out.println(somma.add(5, 5)+"\n");
    System.out.println(differenza.minus(20, 11)+"\n");
    System.out.println(new Prova().product(2, 4)+"\n\n");
    System.out.println(new Prova().divide(100, 1)+'\n');
    System.out.println(new Prova().divide(100, 2)+'\n');
    System.out.println(new Prova().divide(300, 3)+'\n');
    System.out.println(new Prova().divide(400, 4)+'\n');
    System.out.println(new Prova().divide(500, 5)+'\n');
    System.out.println(new Prova().divide(600, 6)+'\n');
}

And here my results:

- 10        //somma.add(5,5) 
- 9         //differenza.minus(20,11)
- 8         //multiply(2,4) 
- 110.0     //divide(100,1)
- 60.0      //divide(100,2)
- 110.0     //divide(300,3)
- 110.0     //divide(400,4)
- 110.0     //divide(500,5)
- 110.0     //divide(600,6)

Other tries with divide method had given

14/2 = 17.0;
1/1 = 11.0;
2/2 = 11.0;
3/3 = 11.0;

The operator works fine outside my Class Prova.divide(). My question is what happened here? I can't see what I did wronf

Kyo
  • 25
  • 2
  • Integer math. Change `res = divisore/dividendo;` to `res = divisore/(double)dividendo;` – Elliott Frisch Dec 06 '18 at 01:27
  • As both of these `divisore/dividendo` are integers, then you will do integer division – Scary Wombat Dec 06 '18 at 01:27
  • 2
    You're essentially trying to concatenate a newline character `\n` to a `double`, but it's actually adding `10`, as `(int) '\n'` is `10`. You can use `"\n"` instead so it will actually concatenate a newline character. I was about to post it as an answer, but it was marked as a duplicate. – Jacob G. Dec 06 '18 at 01:28

1 Answers1

3

I can tell that you want to concatenate a newline character ('\n') to your result. Because you're returning your result as a double, you cannot simply add '\n' to the result, as it will use its corresponding int value (10). This shows why you receive 110 when you divide 100 by 1 and then add '\n'.

To fix this, I would change '\n' to "\n" so your result will be coerced to a String first.

Also, if you decide to divide anything where the result is a fraction, you'll run into unexpected integer division. See: Why is the result of 1/3 == 0?

Note: Because you're using System.out.println, there isn't really a need to append newline characters anyway!

Jacob G.
  • 26,421
  • 5
  • 47
  • 96
  • 2
    Very nicely spotted; but one additional suggestion. Prefer `System.lineSeparator()` to `"\n"` (or use `printf` and `%n`). – Elliott Frisch Dec 06 '18 at 01:44