# Thread: Wondering why all var needed to be doubles

1. Member Join Date
Apr 2013
Location
USA
Posts
7
Rep Power
0

## Wondering why all var needed to be doubles

I just started getting serious about learning Java 3 days ago. I dont have a lot of direction but I am using several tools like youtube tuts and Oracle's Java tuts and a bit of venturing out on my own to test myself how much I've absorbed. So anyways, I decided to make a mini calc that gives you the slope when given a set of coords and made another class to practice using more than one class.

Java Code:
```public class apples {
public static void main(String args[]){
slope slopeObject = new slope();
slopeObject.slopeMessage();

}

}```
and the other class

Java Code:
```import java.util.Scanner;

public class slope {
public void slopeMessage(){
Scanner calc = new Scanner(System.in);

double y1, y2, x1, x2;
System.out.println("Enter first Y coordinate ");
y1 = calc.nextInt();
System.out.println("Enter second Y coordinate ");
y2 = calc.nextInt();
System.out.println("Enter first X coordinate ");
x1 = calc.nextInt();
System.out.println("Enter second X coordinate ");
x2 = calc.nextInt();

double yDif = (y2 - y1);
double xDif = (x2 - x1);
double m = (yDif / xDif);

System.out.println ("The slope is " +m);
calc.close();
}

}```

So my question is why do the coordinate variables and the xDif, yDif var have to be doubles? I kept getting 0.0 as an answer, when slope was less than 1, when I left the variables as int and saved the variable m as a double. Thanks ahead of time!  Reply With Quote

2. Moderator   Join Date
Feb 2009
Location
New Zealand
Posts
4,717
Rep Power
17

## Re: Wondering why all var needed to be doubles

Java makes a distinction between integer (eg int) and floating point (eg double) types for numerical quantities.

The thing about int is that, although it is limited to a certain large finite range, all int operations are precise. By precise, I mean that nine plus seven is sixteen and, sure enough, 9+7 gives 16 unambiguously. Contrast that with nine divided by seven. The rule with int arithmetic is to throw away the remainder, so the answer is unambiguously one.

Floating point arithmetic attempt to capture the mathematical reality that nine divided by seven is a shade over one and a quarter. It does this, and the results are perfectly usable but they will be imprecise in the sense that not every one of the infinite number of numeric quantities can be specified unambiguously. Results end up only being accurate to a certain number of significant figures.

You just have to get used to this and not use floating point quantities if you think "rounding error" may be a problem. And get used to the fact that there ar two different sorts of division - when ints only are involved the result will throw away the remainder. Incidently there is a % operator that gives you the remainder: 9%7 is 2, 52%7 is 3, etc.  Reply With Quote

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•