Create a Java program called "IntDivExample.java" with the following code (copy and paste if necessary):
public class IntDivExample { public static void main(String[] args) { // Initialize variables int aInt = 11; int bInt = 2; double aDbl = 11.0; double bDbl = 2.0; System.out.println("\nVariables:"); System.out.println("int aInt = " + aInt + ";"); System.out.println("int bInt = " + bInt + ";"); System.out.println("double aDbl = " + aDbl + ";"); System.out.println("double bDbl = " + bDbl + ";"); // Examples of FP division System.out.println("\nFloating-point division:"); System.out.println("aDbl / bDbl = " + (aDbl / bDbl)); System.out.println("aInt / bDbl = " + (aInt / bDbl)); System.out.println("aDbl / bInt = " + (aDbl / bInt)); // Examples of integer division and remainder System.out.println("\nInteger division and remainder:"); System.out.println("aInt / bInt = " + (aInt / bInt)); System.out.println("aInt % bInt = " + (aInt % bInt)); // Examples of casting to avoid integer division System.out.println("\nCasting:"); System.out.println("(double)aInt / bInt = " + ((double)aInt / bInt)); System.out.println("aInt / (double)bInt = " + (aInt / (double)bInt)); } }
Compile and run the code. Notice how the result of integer division differs from that of floating-point division. Take about 10 minutes to experiment with this code. Change the value of the variables and see how the program's output changes.