/* * Sample program to illustrate typecasting. * * Input two integers using two pop-up windows * Output their ratio using one pop-up window. * */ import javax.swing.JOptionPane; class TypeCastDemo { public static void main(String [] args){ int numerator,denominator; double result1,result2; String operand1 = JOptionPane.showInputDialog("Enter numerator"); String operand2 = JOptionPane.showInputDialog("Enter denominator"); numerator = Integer.parseInt(operand1); denominator = Integer.parseInt(operand2); if(denominator == 0){ JOptionPane.showMessageDialog(null,"Sorry cannot divide by 0"); System.exit(0); } result1 = numerator / denominator; // typecasting numerator to a double. result2 = (double) numerator / denominator; JOptionPane.showMessageDialog(null, "result1 is " + result1 +"\nresult2 is " + result2); System.exit(0); } } /** NOTES 1) \n means new line 2) (double) is a typecast operator. This usgae will temporarily convert numerator to type double. 3) The Integer class is part of package java.lang. Any class that belongs to package java.lang does not need to be imported. */