Data Type Conversion

 Introduction.  Now that we have seen some primitive data types we need to be able to convert between them.  In Java and many other programming languages this is called casting.  These conversions, particularly when they are numeric in nature, are done with the following sort of operation. (dataTypeName) expression   It can be very important to be able to switch variables and values between data types.  Oftentimes, even the simplest mathematical expressions need to work with data that was originally of different types. The following program takes the inputs for a linear equation in one variable and finds the solution.  We will start with integer inputs, then cast these to floats to see the solution, then cast them to double to see how this impacts our solutions.  You should call the application program DataCasting.java.

 /* program to get inputs from a user to solve an equation of the form ax + b = c */ import javax.swing.JOptionPane; public class DataCasting { public static void main( String args[] ) { String firstUserEntry, secondUserEntry, thirdUserEntry; int a, b, c, x; float floatA, floatB, floatC, floatX; double doubleA, doubleB, doubleC, doubleX; // Prompt user for input JOptionPane.showMessageDialog( null, "You are about to solve an equation of the form ax + b = c", "Integer Product", JOptionPane.INFORMATION_MESSAGE); firstUserEntry = JOptionPane.showInputDialog("Please enter an integer for a:"); secondUserEntry = JOptionPane.showInputDialog("Please enter an integer for b:"); thirdUserEntry = JOptionPane.showInputDialog("Please enter an integer for c:"); // convert these string values to integers a = Integer.parseInt(firstUserEntry); b = Integer.parseInt(secondUserEntry); c = Integer.parseInt(thirdUserEntry); // computing the integer solution x = (c - b)/a; // displaying the integer solution JOptionPane.showMessageDialog( null, "The integer solution for " + a + "x + " + b + " = " + c + " is " + x, "Integer Solution", JOptionPane.INFORMATION_MESSAGE); // casting the integer values to floats floatA = (float) a; floatB = (float) b; floatC = (float) c; // computing the floating point solution floatX = (floatC - floatB)/floatA; // displaying the floating point solution JOptionPane.showMessageDialog( null, "The float solution for " + floatA + "x + " + floatB + " = " + floatC + " is " + floatX, "Floating Point Solution", JOptionPane.INFORMATION_MESSAGE); // casting the integer values to doubles doubleA = (double) a; doubleB = (double) b; doubleC = (double) c; // computing the double precision solution doubleX = (doubleC - doubleB)/doubleA; // displaying the double precision solution JOptionPane.showMessageDialog( null, "The double precision solution for " + doubleA + "x + " + doubleB + " = " + doubleC + " is " + doubleX, "Double Precision Solution", JOptionPane.INFORMATION_MESSAGE); System.exit(0); } // end method main } // end class DataCasting

 This program has our usual basic structure for applications.  Rather than going over this again I will focus on the code which can be divided into five sections. initial description, imports and declarations notice the initial comment description is enclosed between  /*  and  */  so that you don't need the  //  on every comment line obtaining inputs from the user parsing them to integers computing and displaying the integer solution casting, computing and displaying the floating point solution casting, computing and displaying the  double precision solution You need to follow the usual steps of move to the directory containing DataCasting.java javac DataCasting.java java DataCasting Then you should try this program with a variety of input values, but in particular use a = 3,  b = 7,  c = 13 Notice the strange solution you get when using integers.  Also notice there is no apparent difference between the floating point solution and the double precision solution.  This is because the double precision isn't important in this simple of a situation. The DataCasting Applet.  Now for the experience you want to create the following applet.  Make sure to notice how similar it is to the application.  You should call the file DataCastingApplet.java.

 /* applet to get inputs from a user to solve an equation of the form ax + b = c  */ import javax.swing.JOptionPane; import javax.swing.JApplet; public class DataCastingApplet extends JApplet { public void init( ) { String firstUserEntry, secondUserEntry, thirdUserEntry; int a, b, c, x; float floatA, floatB, floatC, floatX; double doubleA, doubleB, doubleC, doubleX; // Prompt user for input JOptionPane.showMessageDialog( null, "You are about to solve an equation of the form ax + b = c", "Integer Product", JOptionPane.INFORMATION_MESSAGE); firstUserEntry = JOptionPane.showInputDialog("Please enter an integer for a:"); secondUserEntry = JOptionPane.showInputDialog("Please enter an integer for b:"); thirdUserEntry = JOptionPane.showInputDialog("Please enter an integer for c:"); // convert these string values to integers a = Integer.parseInt(firstUserEntry); b = Integer.parseInt(secondUserEntry); c = Integer.parseInt(thirdUserEntry); // computing the integer solution x = (c - b)/a; // displaying the integer solution JOptionPane.showMessageDialog( null, "The integer solution for " + a + "x + " + b + " = " + c + " is " + x, "Integer Solution", JOptionPane.INFORMATION_MESSAGE); // casting the integer values to floats floatA = (float) a; floatB = (float) b; floatC = (float) c; // computing the floating point solution floatX = (floatC - floatB)/floatA; // displaying the floating point solution JOptionPane.showMessageDialog( null, "The float solution for " + floatA + "x + " + floatB + " = " + floatC + " is " + floatX, "Floating Point Solution", JOptionPane.INFORMATION_MESSAGE); // casting the integer values to doubles doubleA = (double) a; doubleB = (double) b; doubleC = (double) c; // computing the double precision solution doubleX = (doubleC - doubleB)/doubleA; // displaying the double precision solution JOptionPane.showMessageDialog( null, "The double precision solution for " + doubleA + "x + " + doubleB + " = " + doubleC + " is " + doubleX, "Double Precision Solution", JOptionPane.INFORMATION_MESSAGE); } // end method init } // end class DataCastingApplet

 Notice how you must import the JApplet class in the swing package extend the class over JApplet change the main( ) method to init( ) take out the System.exit(0); Not much else is different. You also need a file called DataCastingApplet.html.  It should look like the following.



 The steps for running the applet are the following move to the directory containing DataCastingApplet.java and DataCastingApplet.html javac DataCastingApplet.java appletviewer DataCastingApplet.html You should try a variety of outputs, but a = 13,  b = 67,  c = 23 shows some distinctions between solutions in each precision level.