0

The question require me to write a Java program to show the results of the following cast operator expressions:

(double) (23 / 14) + 7.65

My Code:

public class op { 
public static void main(String [] args) {
int num =  23/14;
double r1 = (double) num;
double result = r1 + 7.65;
System.out.println("Results: "+ result);
  }
}

I don't think I have done correctly, what are the problems of my code? By the way, can someone tell me what are the differences between long, double, int, float? How do we know when to use these primitive data types? I read an explanation here: https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html but is there any 'human-version' of the explanation?

Thank you for your help.

nightowl_nicky
  • 341
  • 1
  • 5
  • 14

4 Answers4

2

The problem is due to the used types.

Since you divide two integers (23 and 14), the result is considered and int as well. Therefor, 23/14 = 1.642857142857143, which is truncated to fit in an int result, more specifically, 1. result is the sum of 1 (int) and 7.65 (double). Since one of them is a double, to other is converted to the "upper" type as well (double) and the operation becomes 1.0+7.65 = 8.65.

The result is correct, because you asked the result of (double) (23 / 14) + 7.65 which means the result of casting the result of the operations in brackets to double summed with 7.65. Which is 8.65 as previously explained.

If you want to use a division using doubles, consider:

double r1 = 1.0 * 23/14;
Adrian B.
  • 1,592
  • 1
  • 20
  • 38
  • Just make sure again, so the result should be 8.65 but not 9.2928....? – nightowl_nicky Mar 04 '16 at 15:08
  • It's all in the way you interpret the initial request. If the request is supposed to be written in java, the result should be 8.65 as the int division is done before the cast. If it was given in natural language, it might refer to the division as doubles, in which case the result should be 9.29... – Adrian B. Mar 04 '16 at 15:36
  • 1
    in fact, you can do just it: `double r1 = 23/14.0` – pedrohreis Mar 04 '16 at 18:27
1

Lets see step-by-step:

int num =  23/14; // int division of 23/14 results in 1

So, here num = 1

When you cast num to double value of r1 is setted to 1.0.

double result = r1 + 7.65; //1.0 + 7.65 = 8.65
pedrohreis
  • 1,030
  • 2
  • 14
  • 33
1

ok, int is short term for INTEGER which are natural numbers that we use normally but with no decimal places and if your number has some value in between roughly -2 billion to +2 billion. if your range exceeds that and you still want an integer then go for long data type.

floats are for decimal values like 3.147 with a range of +10*38 to -10*38 or so, but if your range exceeds this(practically this happens rarely) go for double.

coming to the code you put here , if you divide a int by another int (like 23/14) you get only get the integer part of the answer(only '1' in 23/14=1.642...) , next when you cast it to double you get 1.0 and next you are going to add that to 7.65 which will make the ultimate answer as 8.65 hope this answers your Q....

Infamous
  • 744
  • 11
  • 25
0

You could change this int num = 23/14

to double num = ((double) 23)/14

or double num = (23 * 1.0)/14

Bhushan Bhangale
  • 10,921
  • 5
  • 43
  • 71