|
|
The example problem includes an illustration of the passing of instances as arguments. It also gives an illustration of the use of the abs method from the Math class, using a float data type, and how to "return" an object from a method (we looked at returning standard data items in the previous lecture.
1. INTRODUCTION TO TYPE CONVERSION |
It is sometimes necessary to convert a data item of one type to another type. For example when it is necessary to perform some arithmetic using data items of different types (so called mixed mode arithmetic). Under certain circumstances Type conversion can be carried out automatically, in other cases it must be "forced" manually (explicitly).
In Java type conversions are performed automatically when the type of the expression on the right hand side of an assignment operation can be safely promoted to the type of the variable on the left hand side of the assignment. Thus we can safely assign: The -> symbol used here should be interpreted as "to a". For example: |
// 64 bit long integer long myLongInteger; // 32 bit standard integer int myInteger; myLongInteger = myInteger; The extra storage associated with the long integer, in the above example, will simply be padded with extra zeros. |
The above will not work the other way round. For example we cannot automatically convert a long to an int because the first requires more storage than the second and consequently information may be lost. To force such a conversion we must carry out an explicit conversion (assuming of course that the long integer will fit into a standard integer). This is done using a process known as a type cast: myInteger = (int) myLongInteger |
This tells the compiler that the type of myLongInteger must be temporarily changed to a int when the given assignment statement is processed. Thus, the cast only lasts for the duration of the assignment. Java type casts have the following form: (T) N where T is the name of a numeric type and N is a data item of another numeric type. The result is of type T. |
Given a data item of type double or a float we can change the type of this item so that it becomes a data item of type long or int using a cast operation as described above. What happens in this case is that the exponent part of the real number is simply omitted. For example 99.9999 will become 99. In cases such as this it might be more desirable to say that the integer equivalent of 99.9999 is 100 (i.e. round up). We cannot achieve this using a cast, however the Java Math class contains methods to achieve this, namely the methods rint and round. |
The first rounds a double up or down to its nearest integer equivalent (without converting the type). The second has two versions, one to round and convert a float to an int, and one to round and convert a double to a long. Consider the Java code given in Table 1. The code allows a user to input a double which is then output; first as an integer using a cast, then as an integer using the round method, and then as double rounded to the nearest integer. |
// ROUNDING EXAMPLE APPLICATION CLASS // Frans Coenen // Tuesday 13 April 1999 // The University of Liverpool, UK // Revised Rgursday 22 June 2005 import java.util.*; class RoundingEx { // ------------------- FIELDS ------------------------ // Create BufferedReader class instance private static Scanner input = new Scanner(System.in); // ------------------ METHODS ----------------------- /* Main method */ public static void main(String[] args) { // Input System.out.print("Input a double precision number = "); double inputDouble = input.nextDouble(); // Output System.out.println("Input converted to an int using a " + "cast = " + (int) inputDouble); System.out.println("Input converted to an int using 'round' " + "method = " + Math.round(inputDouble)); System.out.println("Input converted to an \"int\" using 'rint' " + "method = " + Math.rint(inputDouble)); } } |
Table 1: Rounding example program
Some example output is given in Table 2. Note that a value of 1.5 is rounded upwards.
$ java RoundingEx Input a double precision number = 1.2 Input converted to an int using a cast = 1 Input converted to an int using 'round' method = 1 Input converted to an "int" using 'rint' method = 1.0 $ java RoundingEx Input a double precision number = 1.8 Input converted to an int using a cast = 1 Input converted to an int using 'round' method = 2 Input converted to an "int" using 'rint' method = 2.0 $ java RoundingEx Input a double precision number = 1.5 Input converted to an int using a cast = 1 Input converted to an int using 'round' method = 2 Input converted to an "int" using 'rint' method = 2.0 |
Table 2: Example output produced by code presented in Table 1
Note: In the following example problem we are going to use a cast to convert a double to a float. The example will also illustrate how we can pass instances as arguments (to date we have only considered the passing of primitive data items as opposed to more complex higher level data items.
Data validation: Finally we should carry out some tests using deliberately spurious data.
3. TEST HARNESS FOR 2-D GEOGRAPHIC DISTANCE APPLICATION |
A test harness to carry out the required testing is presented in Table 5. Note: to indicate to Java that the numeric literals are floats we need to include an f, e.g. 4.0f.
// GEOGRAPHIC 2-D DISTANCE TEST // Frans Coenen // Wednesday 3 March 1999 // University of Liverpool import java.io.*; class Geo2DdistTest { // ---------------- FIELDS ------------------- // ---------------- METHODS ------------------ /* Main method, input X coordinate for point 1 */ public static void main(String argv[]) throws IOException { inputYcoordForPoint1(4.0f); inputYcoordForPoint1(0.0f); inputYcoordForPoint1(-4.0f); } // Input Y coordinate for point 1 public static void inputYcoordForPoint1(float x_coord1) { inputXcoordForPoint2(x_coord1,4.0f); inputXcoordForPoint2(x_coord1,0.0f); inputXcoordForPoint2(x_coord1,-4.0f); } // Input X coordinate for point 2 public static void inputXcoordForPoinr2(float x_coord1, float y_coord1) { inputYcoordForPoint2(x_coord1,y_coord1,3.0f); inputYcoordForPoint2(x_coord1,y_coord1,0.0f); inputYcoordForPoint2(x_coord1,y_coord1,-3.0f); } // Input Y coordinate for point 2 public static void inputYcoordForPoinr2(float x_coord1, float y_coord1, float x_coord2) { claculateDistance(x_coord1,y_coord1,x_coord2,3.0f); claculateDistance(x_coord1,y_coord1,x_coord2,0.0f); claculateDistance(x_coord1,y_coord1,x_coord2, -3.0f); } // Calculate distance public static void claculateDistance(float x_coord1, float y_coord1, float x_coord2, float y_coord2) { float geoDistance; // Echo x and y for point 1 to screen and then create the instance // point1 System.out.print("Point1: x = " + x_coord1 + " y = " + y_coord1); Point_2D point1 = new Point_2D(x_coord1,y_coord1); // Echo x and y for point 2 to screen and then create the instance // point2 System.out.print(", Point2: x = " + x_coord2 + " y = " + y_coord2); Point_2D point2 = new Point_2D(x_coord2,y_coord2); // Calculate distance and output geoDistance = point1.geoDist_2D(point2); System.out.println(", Distance" + geoDistance); } } |
Table 5: Test harness implementation for geographic 2-D distance application
Created and maintained by Frans Coenen. Last updated 10 February 2015