Assignment #2: More Simple math programs

The point of this is to look at the other differences between int and double.

Take this program:

public class Math2 {
	public static void main (String args[]) {
		//variables that may be needed:
		int a = 12;
		int b = 5;
		int c;
		double x = 12.0;
		double y,z;
		
		
	} 
} 

and change it so that you can answer the following:

Write down answers to these questions and hand them in (call it Assignment #2)

  1. What happens when you divide an int by zero? (e.g. c = a/0;)
  2. What happens when you divide double by zero? (e.g. y = x/0;)
    Print out the result in order to see it.
  3. What is the result of a/b ? Why?
  4. Why does z = a/b still not give you the answer with decimals? z is a double!
  5. Make c be the biggest int possible (see the page on data types). Now add 10 to it. What is the result?
  6. Make a byte type variable and subtract 200 from it. (e.g. byte bb = 14 - (byte)200;) What happens?
  7. Why does this (the stuff in the previous two questions) happen with integer type variables?
  8. Let y = 0.1 + 0.1 + 0.1; Print out the value of y. Why is it not what is expected?
  9. Normally you would test to see if y is 0.3 by writing:
    if (y == 0.3) {
    	System.out.println("equal");
    } else {
    	System.out.println("not equal");
    } 
    
    Obviously we can NEVER reliably check to see if a double is equal to some value by using == .
    So, how to we see if y is equal to 0.3 ? (Hint: it involves Math.abs() )
  10. Give an example of a line of code that results in z being set to "NaN" (Not a Number).