/* this class shows the various combinations of
 * integer and double division and shows what
 * type results from the division
*/
public class DivisionTest {
	
	public static void main (String[] args ) {
		
		int a, b,c;     // no decimal variables
		double x, y, z; // decimals allowed variables
		
		// integer division
		a = 1;
		b=2;
		c = a / b;   
		// a/b gives 0.5 BUT 
		// only the integer part is kept ( c = 0 )
		System.out.println( "a = " + a + "   b = " + b );
		System.out.println( "two integers: a/b = " + c );
		System.out.println();
		// double division
		x = 1.0;
		y=2.0;
		z= x/y;
		// x/y gives 0.5 and the variable 
		// keeps the decimal part too ( z = 0.5 )
		System.out.println( "x = " + x + "   y = " + y );
		System.out.println( "two doubles: x/y " + z );
		System.out.println();
		// mixed division
		// legal if you put the answer into the largest
		// variable type
		//  e.g. you can divide integer by double
		//       which gives a double result
		double dMix = a / y;
		System.out.println("mixed division result put in double variable");
		System.out.println( "a = " + a + "   y = " + y );
		System.out.println( "integer/double: a/y = " + dMix );
		System.out.println();
		// if you divide integer by integer you first get
		// an integer result ( 1 / 2 = 0 )
		// then this is put into a double variable
		// the result is changed to a double ( 0.0 )
		dMix = a / b;
		System.out.println("integer division put in double variable");
		System.out.println( "a = " + a + "   b = " + b );
		System.out.println( "integer/integer a/b = " + dMix );
		System.out.println();
		// but you CAN'T put a double into an integer
		// a double is too big to fit into an integer variable
		// int iMix = a / y;  will give an error
		int iMix = a / (int) y; // is okay
		// why? the (int) before the y is called a type-cast
		//      this tell java to treat the y variable
		//		as an integer instead of a double
		System.out.println("mixed division result put in integer variable");
		System.out.println("only allowed using type-casting");
		System.out.println( "a = " + a + "   y = " + y );
		System.out.println( "integer/double put in integer variable: a/ (int)y = " + iMix );
		System.out.println();
		//here is another example of using type-casting
		x = 5;
		iMix = (int) x / b;
		System.out.println("mixed division result put in integer variable");
		System.out.println("only allowed using type-casting");
		System.out.println( "x = " + x + "   b = " + b );
		System.out.println( "double/integer put in integer variable: (int) x/b = " + iMix );
		System.out.println();
		// treat integers as doubles for division
		System.out.println("use type-casting, treat integers into doubles");
		dMix = (double) a / (double) b;
		System.out.println( "a = " + a + "   b = " + b );
		System.out.println( "two doubles: a/b = " + dMix );
		System.out.println();
	} // main
	
} // DivisionTest