Title: Arithmetic Primitive Sizes, Casting, Shortcuts
Course: CPS 161: An Intro to Programming with Java - DL MM MASTER

Arithmetic Primitive Sizes, Casting, Shortcuts

- Mixed arithmetic... Operations on different data types are done with the more precise data type


  int i_answer;
  double d_answer;


  i_answer = 5/2;  // i_answer = 2;
  d_answer = 5/2;  // d_answer = 2; Note integer arithmetic between 2 integers
  d_answer = 5.0/2.0;  // d_answer = 2.5 arithmetic between 2 doubles
  d_answer = 5 / 2.0;  // d_answer = 2.5 arithmetic between a double and int.  Operation done with higher precision data type
  d_answer = 5.0 / 2;  // d_answer = 2.5 arithmetic between a double and int.  Operation done with higher precision data type

  int ix;
  double dx, dy;


// Figure out the results of the following:

  ix = 4 + 6/4;
  dx = ix +20/3;
  dx = 10/3;
  dx = 10.0/3.0;
  dx = 10.0/3.0 + 5/2 + ix/3;
  dy = dx/2.0 + ix/4 + ix/4.0;


- Primitive "Sizes" and Casting operator


  int x;
  double d;
  d = 9.0/4.0; // No problem
  d = 9/4;  // No problem.  Arithmetic operation will be done as integers, so the answer will be 2.  The value of 2 is then stored into the double d.

  x = 9.0/4.0; // Compilation error because something is lost(fractional part) when storing a double in an int
  x = (int)(9.0/4.0); // Proper way to avoid compilation error
                      // This is calling a cast, and you are effectively telling the compiler that you are
                      // aware that something could be lost in storing a double into an int, but you are OK with the possible loss

  In general, we have "size of ordering" for data number like data types of:
byte --> short --> int --> long --> float --> double


byte b;
short s;
int i;
long l;
float f;
double d;

// All of the following work just fine because we are storing a "smaller" primitive into a "larger" primitive
b = 2;
s = b;
i = s;
l = i;
f = l;
d = f;

// However:
d = 2;
f = d; // compile error
f = (float)d; // This is OK, because of the cast
l = f; // compile error
l = (long) f; // This is OK, because of the cast
i = l; // compile error
i = (int) l;  // This is OK, because of the cast
s = i; // compile error
s = (short) i; // This is OK, because of the cast
b = s; // compile error
b = (byte) s; // This is OK, because of the cast

// Java quirk:

f = 2.0;  // Compile error because Java considers 2.0 a double
f = (float) 2.0; // This works
f = 2.0f; // This also works, because 2.0f is considered a floating point constant

- Mixed arithmetic, casting practice .... See if you can figure out the values after each operation:


  double d;
  int i, j;

  d = 10.0/3  + 10/3;
  i = 3;
  d = 10.0/i  +  10/i;
  j = (int) ( 10.0/i + 10/i);



- More = short cut operators


  int x;
  x = 2;
  x += 3;  // Equivalent to x = x +3;
  x *= 2;   // Equivalent to x = x * 2;
  x /= 2;   //  Equivalent to x = x/2;
  x %= 2 //   Equivalent to x = x % 2;
  x *= 10 + 4/3 -2; // Equivalent to x = x * (10 + 4/3 -2);


- Increment and Decrement Operators


  int z, x1 =3, x2 = 3, y1 = 3, y2=3, x=3, y=3;

  z = ++x1;  // z has a value of 4, x1 has a value of 4
  z = x2++;  // z has a value of 3, x2 has a value of 4
  z = --y1;  // z has a value of 2, y1 has a value of 2
  z = y2--;  // z has a value of 3, y2 has a value of 2


// See if you can figure these out:

  z = x++   +  y++;
  z = ++x   +  ++y;
  z = ++x   +  y++;
  z = x--   +   y--;
  z = --x   +   --y;

Java Practice(JPHB): Enter your answers in a text file with numbers for the questions 1, 2, 3, .....

1. (2 points) Convert each of the following algebraic formulas to a Java expression:

For example the first 2 would be:
3*x
3*x + y
2. (1 point) What is the output of the following program lines:


  double number = (1/3) * 3;
  System.out.println("(1/3) * 3 is equal to " + number);


3. (1 point) What is the output produced by the following lines of program code:


  int quotient, remainder;
  quotient = 7/3;
  remainder = 7 % 3;
  System.out.println("quotient = " + quotient);
  System.out.println("remainder = " + remainder);


4. (1 point) What is the output produced by the following code?


  int result = 11;
  result /= 2;
  System.out.println("result is " + result);


5. (1 points) Given the following fragment that purports to convert degrees Celsius to degrees Fahrenheit, answer the following questions:


	double celsius = 20;
	double fahrenheit;
	fahrenheit = (9/5) * celsius + 32.0;
	


	a.)  What value is assigned to fahrenheit?
	b.) Explain what is actually happening, and what the programmer likely wanted.
	c.) Rewrite the code as the programmer intended.
	

6. (1 point) What is the output produced by the following lines of program code?


  int n = (int) 3.9;
  System.out.println("n== " + n);


7. (1 point) What is the output produced by the following lines of program code?


  int n = 3;
  n++;
  System.out.println("n==" + n);
  n--;
  System.out.println("n==" + n);


8. (1 points) What is the output of the following expressions:


In Java, *,/,% are higher priority operators.  The +, - operators are lower priority operators.
Java evaluates higher priority operators before lower priority operators.  Within the same
priority level, operators are applied "left to right".

What is the output of the following.  Try to figure this out without eclipse first and then try the
expressions in eclipse to check your answer.  Make sure you understand why the answers are what
they are:

System.out.println((3 + 3)*(7/2));
System.out.println((3 + 3)*7/2);
System.out.println(3 + 3*7/2);
System.out.println(3 + 3*(7/2));


9. (1 points) Given the following definitions:


  int ix=1;
  double dx=1.123;
  long lbig =123456789;
  byte b = 3;

  //Which of the following will compile properly (add comment on the right hand side of the line)?

  ix = dx;
  lbig =b;
  dx = ix;
  ix = (double)dx;
  ix = (int) dx;
  b = (byte)dx;





Last Modified: February 5, 2015 11:14 AM