BasicData.java |
| /* Program to demonstrate basic Java data types Version 1.0 Written by: STF (c) University of Otago, Dept. of Computer Science, 2001. Updated by James Little for Exercise 2 cosc241 8/3/01. And again for exercise 2 15/3/01. */ package jlittle_ex2; import basicIO.*; import java.text.*; import java.io.*; /** * This is a Basic Data class... to increase my knowledge of basic (java) data type */ public class BasicData { public static void runTypeTests() { // integers byte largestByte = Byte.MAX_VALUE; byte smallestByte = Byte.MIN_VALUE; short largestShort = Short.MAX_VALUE; short smallestShort = Short.MIN_VALUE; int largestInteger = Integer.MAX_VALUE; int smallestInteger = Integer.MIN_VALUE; long largestLong = Long.MAX_VALUE; long smallestLong = Long.MIN_VALUE; // real numbers float largestFloat = Float.MAX_VALUE; float smallestFloat = Float.MIN_VALUE; double largestDouble = Double.MAX_VALUE; double smallestDouble = Double.MIN_VALUE; // display them all Out.println("Range of byte is from " + smallestByte + " to " + largestByte); Out.println("Range of short is from " + smallestShort + " to " + largestShort); Out.println("Range of int is from " + smallestInteger + " to " + largestInteger); Out.println("Range of long is from " + smallestLong + " to " + largestLong); Out.println("Range of float is from " + smallestFloat + " to " + largestFloat); Out.println("Range of double is from " + smallestDouble + " to " + largestDouble); } // end runTypeTests method /** * Run some tests using approximations to PI */ public static void runPITests() { double circleRadius = 10.0; double circumference = 2*Math.PI*circleRadius; DecimalFormat df = new DecimalFormat("0.#"); Out.println("Circumference of a circle with 10m radius is: " + df.format((circumference)) + " m."); } // end runPITests method /** * Run some tests with squares of numbers */ public static void runSquareTests() { int x; // can't convert int to short, so I made x int. short y; // y can be short because its range is only <200 short start = 150; int end = 200; for (y=start; y<end; y++) { x = y * y; // where x must go to 39601.. it cannot be short Out.println("The square of " + y + " is " + x); } // end for loop } // end runSquareTests method /** * Run some tests finding intersections between lines. * Refer to Line.java and Line.class documentation for details. */ public static void runLineTests() { Line line1; Line line2; Line line3; Line line4; Point intersectPt; // line 1 is y=-x+4 line1 = new Line(1, 1, -4); // line 2 is y=x+2 line2 = new Line(-1, 1, -2); // line 3 is x=3 line3 = new Line(-1, 0, 3); // line 4 is y=-x+1 line4 = new Line(1, 1, -1); intersectPt = new Point(); // lines 1 and 2 should intersect if (line1.intersects(line2, intersectPt)) Out.println("CORRECT: Intersection found at (" + intersectPt.getX() + ", " + intersectPt.getY() + ")"); else Out.println("ERROR: No intersection found for lines 1 and 2"); // lines 1 and 4 should not intersect (they are parallel) if (line1.intersects(line4, intersectPt)) Out.println("ERROR: Intersection found for lines 1 and 4"); else Out.println("CORRECT: No intersection found for lines 1 and 4"); // vertical line 3 should intersect with line 2 if (line3.intersects(line2, intersectPt)) Out.println("CORRECT: Intersection found at (" + intersectPt.getX() + ", " + intersectPt.getY() + ")"); else Out.println("ERROR: No intersection found for lines 3 and 2"); // line 2 should intersect with line 4 if (line2.intersects(line4, intersectPt)) Out.println("CORRECT: Intersection found at (" + intersectPt.getX() + ", " + intersectPt.getY() + ")"); else Out.println("ERROR: No intersection found for lines 2 and 4"); } // end runLineTests method /** * Run some tests to illustrate Java data types and calculations */ public static void main(String[] args) { runTypeTests(); runPITests(); runSquareTests(); runLineTests(); Out.println("END OF OUTPUT"); } // end main method } // end BasicData class |
James Little |