Home  Listings  By Name  By Subject  Email  www.tomswan.com  Help 

Java 2 Just Click! Solutions

Chapter 9 Listings

© 2001 by Tom Swan. All rights reserved. Updated: 6/4/01 12:20:54 PM

Return to Listings page
Switch to Solutions by name page
Switch to Solutions by subject page


Listing 9-1 Math.txt Page 159

Return to top
001: // Math class constructor
002: private Math() {}
003: 
004: // Math class constants
005: public static final double E = 2.7182818284590452354;
006: public static final double PI = 3.14159265358979323846;
007: 
008: // Math class methods
009: public static double sin(double a);
010: public static double cos(double a);
011: public static double tan(double a);
012: public static double asin(double a);
013: public static double acos(double a);
014: public static double atan(double a);
015: public static double toRadians(double angdeg);
016: public static double toDegrees(double angrad);
017: public static double exp(double a);
018: public static double log(double a);
019: public static double sqrt(double a);
020: public static double IEEEremainder(double f1, double f2);
021: public static double ceil(double a);
022: public static double floor(double a);
023: public static double rint(double a);
024: public static double atan2(double a, double b);
025: public static double pow(double a, double b);
026: public static int round(float a);
027: public static long round(double a);
028: public static double random();
029: public static int abs(int a);
030: public static long abs(long a);
031: public static float abs(float a);
032: public static double abs(double a);
033: public static int max(int a, int b);
034: public static long max(long a, long b);
035: public static float max(float a, float b);
036: public static double max(double a, double b);
037: public static int min(int a, int b);
038: public static long min(long a, long b);
039: public static float min(float a, float b);
040: public static double min(double a, double b);
Return to top

Listing 9-2 AbsValue/AbsValue.java Page 161

Return to top
001: class AbsValue {
002:  public static void main(String args[]) {
003:   int v = -100;
004:   char ch = (char)Math.abs(v);
005:   System.out.println("char(" + Math.abs(v) + ") = " + ch);
006:  }
007: }
Return to top

Listing 9-3 MinMax/MinMax.java Page 162

Return to top
001: class MinMax {
002:  public static void main(String args[]) {
003:   long v1 = 99;
004:   long v2 = v1 * 2;
005:   System.out.println("v1=" + v1 + " v2=" + v2);
006:   System.out.println("Maximum value = " + Math.max(v1, v2));
007:   System.out.println("Minimum value = " + Math.min(v1, v2));
008:  }
009: }
Return to top

Listing 9-4 CeilFloor/CeilFloor.java Page 162

Return to top
001: class CeilFloor {
002:  public static void main(String args[]) {
003:   double d = 10.0;
004:   while (d < 11.0) {
005:    System.out.println("d=" + d + " ceil(d)=" + Math.ceil(d) +
006:     " floor(d)=" + Math.floor(d));
007:    d += 0.1;
008:   }
009:  }
010: }
Return to top

Listing 9-5 PowerDemo/PowerDemo.java Page 163

Return to top
001: class PowerDemo {
002:  public static void main(String args[]) {
003:   if (args.length < 2) {
004:    System.out.println("Enter two values as follows:");
005:    System.out.println("java PowerDemo 2 8");
006:   } else {
007:    try {
008:     int j = Integer.parseInt(args[0]);
009:     int k = Integer.parseInt(args[1]);
010:     System.out.println(j + " ^^ " + k + " = " + Math.pow(j, k));
011:    }
012:     catch (NumberFormatException e) {
013:     System.out.println("Error in argument " + e.getMessage());
014:    }
015:   }
016:  }
017: }
Return to top

Listing 9-6 Remainder/Remainder.java Page 164

Return to top
001: class Remainder {
002:  public static void main(String args[]) {
003:   double arg1 = 3.14159;
004:   double arg2 = 2;
005:   double result = Math.IEEEremainder(arg1, arg2);
006:   System.out.println(arg1 + " / " + arg2 + " = " + result);
007:  }
008: }
Return to top

Listing 9-7 Round/Round.java Page 165

Return to top
001: class Round {
002:  public static void main(String args[]) {
003:   double arg = 3.14159;
004:   double doubleResult = Math.rint(arg);
005:   int    intResult    = (int)Math.round(arg);
006:   long   longResult   = Math.round(arg);
007:   System.out.println("double rint(arg) = " + doubleResult);
008:   System.out.println("(int)round(arg)  = " + intResult);
009:   System.out.println("(long)round(arg) = " + longResult);
010:  }
011: }
Return to top

Listing 9-8 CosDemo/CosDemo.java Page 166

Return to top
001: class CosDemo {
002:  public static void main(String args[]) {
003:   double fp, result;
004:   for (fp = -1.0; fp <= 1.0; fp += 0.1) {
005:    result = Math.cos(fp);
006:    System.out.println("fp = " + fp + ", cosine = " + result);
007:   }
008:  }
009: }
Return to top

Listing 9-9 RandomDemo/RandomDemo.java Page 167

Return to top
001: class RandomDemo {
002:  public static void main(String args[]) {
003:   double doubleResult;
004:   int intResult;
005:   for (int i = 1; i < 10; i++) {
006:     doubleResult = Math.random();
007:     System.out.print(doubleResult + " /t");
008:     intResult = (int)(doubleResult * 100);
009:     System.out.println(intResult);
010:   }
011:  }
012: }
Return to top

Listing 9-10 Random.txt Page 168

Return to top
001: // Random class constructors
002: public Random();
003: public Random(long seed);
004: 
005: // Random class methods
006: synchronized public void setSeed(long seed);
007: public void nextBytes(byte[] bytes);
008: public int nextInt();
009: public int nextInt(int n);
010: public long nextLong();
011: public float nextFloat();
012: public double nextDouble();
013: synchronized public double nextGaussian();
Return to top

Listing 9-11 RandGen/RandGen.java Page 169

Return to top
001: import java.util.Random;
002: 
003: class RandGen {
004:  public static void main(String args[]) {
005:   Random generator = new Random();
006:   int rows, cols;
007:   StringBuffer buffer;
008:   for (rows = 1; rows <= 8; rows++) {
009:    buffer = new StringBuffer(128);
010:    for (cols = 1; cols <= 3; cols++) {
011:     buffer.append(generator.nextDouble() + " /t");
012: //  buffer.append(generator.nextInt() + " /t");
013:    }
014:    System.out.println(buffer);
015:   }
016:  }
017: }
Return to top

Listing 9-12 RandomBytes/RandomBytes.java Page 171

Return to top
001: import java.util.Random;
002: 
003: class RandomBytes {
004:  static int SIZE = 64;    // Number of bytes to generate
005:  static byte byteArray[]; // The array of bytes
006: 
007:  // Display byte array
008:  public static void showArray(String label) {
009:   System.out.println("/n/n" + label);
010:   for (int i = 0; i < byteArray.length; i++) {
011:    if (i % 8 == 0) 
012:     System.out.println();    // Start new row
013:    else
014:     System.out.print('/t');  // Start new column
015:    System.out.print(byteArray[i]);
016:   }
017:  }
018: 
019:  public static void main(String args[]) {
020:   Random generator = new Random();
021:   byteArray = new byte[SIZE];
022:   showArray("Before randomizing");
023:   generator.nextBytes(byteArray);  // Fill array
024:   showArray("After randomizing");
025:  }
026: }
Return to top

Listing 9-13 RandomSeed/RandomSeed.java Page 172

Return to top
001: import java.util.Random;
002: 
003: class RandomSeed {
004:  static long SEED = 12345;
005:  public static void main(String args[]) {
006:   Random generator = new Random(SEED);
007:   System.out.println("/nInitial sequence:");
008:   for (int i = 0; i < 32; i++)
009:    System.out.print(generator.nextInt(100) + " /t");
010:   generator.setSeed(SEED);  // Reseed the generator
011:   System.out.println("/n/nAfter reseeding generator:");
012:   for (int i = 0; i < 32; i++)
013:    System.out.print(generator.nextInt(100) + " /t");
014:  }
015: }
Return to top

Listing 9-14 Boolean.txt Page 173

Return to top
001: // Boolean wrapper class fields
002: public static final Boolean TRUE;
003: public static final Boolean FALSE;
004: 
005: // Boolean wrapper class constructors
006: public Boolean(boolean value);
007: public Boolean(String s);
008: 
009: // Boolean wrapper class methods
010: public boolean booleanValue();
011: public static Boolean valueOf(String s);
012: public String toString();
013: public int hashCode();
014: public boolean equals(Object obj);
015: public static boolean getBoolean(String name);
Return to top

Listing 9-15 BooleanDemo/BooleanDemo.java Page 174

Return to top
001: class BooleanDemo {
002:  public static void main(String args[]) {
003:   // Shows that TRUE and FALSE are objects
004:   Boolean boolObject = new Boolean(true);
005:   if (boolObject.equals(Boolean.TRUE))
006:    System.out.println("boolObject is true");
007:   // But that true and false are native values
008:   boolean boolValue = true;
009:   if (boolValue = Boolean.TRUE.booleanValue())
010:    System.out.println("boolValue is true");
011:  }
012: }
Return to top

Listing 9-16 GetProperty/GetProperty.java Page 176

Return to top
001: class GetProperty {
002:  static String DEBUGGINGPROP = "Debugging.prop";
003:  static String NAMEPROP = "Program.name.prop";
004:  static String NAMEVALUE = "GetProperty";
005: 
006:  public static void main(String args[]) {
007:   System.setProperty(DEBUGGINGPROP, "true"); // Boolean prop
008:   System.setProperty(NAMEPROP, NAMEVALUE);   // Other prop
009: 
010:   boolean result;
011:   String valueStr;
012: 
013:   // Get true or false value of boolean property
014:   result = Boolean.getBoolean(DEBUGGINGPROP);
015:   System.out.println(DEBUGGINGPROP + " = " + result);
016: 
017:   // Get value of non-boolean property
018:   valueStr = System.getProperty(NAMEPROP);
019:   result = Boolean.getBoolean(NAMEPROP);
020:   System.out.println(NAMEPROP + " value = " + valueStr);
021:   System.out.println(NAMEPROP + " result = " + result);
022:  }
023: }
Return to top

Listing 9-17 IntCommon.txt Page 177

Return to top
001: // Common to Short, Byte, Integer, and Long classes
002: public byte byteValue();
003: public short shortValue();
004: public int intValue();
005: public long longValue();
006: public float floatValue();
007: public double doubleValue();
008: public String toString();
009: public int hashCode();
010: public boolean equals(Object obj);
011: public int compareTo(Object o);
Return to top

Listing 9-18 Integer.txt Page 179

Return to top
001: // Integer wrapper class constructors
002: public Integer(int value);
003: public Integer(String s);
004: 
005: // Integer wrapper class methods
006: public static String toString(int i, int radix);
007: public static String toHexString(int i);
008: public static String toOctalString(int i);
009: public static String toBinaryString(int i);
010: public static String toString(int i);
011: public static int parseInt(String s, int radix);
012: public static int parseInt(String s);
013: public static Integer valueOf(String s, int radix);
014: public static Integer valueOf(String s);
015: public int compareTo(Integer anotherInteger);
016: 
017: // Integer wrapper class property methods
018: public static Integer getInteger(String nm);
019: public static Integer getInteger(String nm, int val);
020: public static Integer getInteger(String nm, Integer val);
021: public static Integer decode(String nm);
Return to top

Listing 9-19 ConvertInt/ConvertInt.java Page 179

Return to top
001: class ConvertInt {
002:  public static void main(String args[]) {
003:   if (args.length < 1)
004:    System.out.println("ex. java ConvertInt 1234");
005:   else 
006:    try {
007:     int intValue = Integer.parseInt(args[0]);
008:     System.out.println("Default = " 
009:      + Integer.toString(intValue));
010:     System.out.println("Hex = " 
011:      + Integer.toHexString(intValue));
012:     System.out.println("Octal = " 
013:      + Integer.toOctalString(intValue));
014:     System.out.println("Binary = " 
015:      + Integer.toBinaryString(intValue));
016:     System.out.println("base 32 = " 
017:      + Integer.toString(intValue, 32));
018:    } catch (NumberFormatException e) {
019:     System.out.println(
020:      "Format error in argument " + e.getMessage());
021:   }
022:  }
023: }
Return to top

Listing 9-20 Long.txt Page 181

Return to top
001: // Long wrapper class constructors
002: public Long(long value);
003: public Long(String s);
004: 
005: // Long wrapper class methods
006: public static String toString(long i, int radix);
007: public static String toHexString(long i);
008: public static String toOctalString(long i);
009: public static String toBinaryString(long i);
010: public static String toString(long i);
011: public static long parseLong(String s, int radix);
012: public static long parseLong(String s);
013: public static Long valueOf(String s, int radix);
014: public static Long valueOf(String s);
015: public static Long decode(String nm);
016: public int compareTo(Long anotherLong);
017: 
018: // Long wrapper class property methods
019: public static Long getLong(String nm);
020: public static Long getLong(String nm, long val);
021: public static Long getLong(String nm, Long val);
Return to top

Listing 9-21 Byte.txt Page 181

Return to top
001: // Byte wrapper class constructors
002: public Byte(byte value);
003: public Byte(String s);
004: 
005: // Byte wrapper class methods
006: public static String toString(byte b);
007: public static byte parseByte(String s);
008: public static byte parseByte(String s, int radix);
009: public static Byte valueOf(String s, int radix);
010: public static Byte valueOf(String s);
011: public static Byte decode(String nm);
012: public int compareTo(Byte anotherByte);
Return to top

Listing 9-22 Short.txt Page 182

Return to top
001: // Short wrapper class constructors
002: public Short(short value);
003: public Short(String s);
004: 
005: // Short wrapper class methods
006: public static String toString(short s);
007: public static short parseShort(String s);
008: public static short parseShort(String s, int radix);
009: public static Short valueOf(String s, int radix);
010: public static Short valueOf(String s);
011: public static Short decode(String nm);
012: public int compareTo(Short anotherShort);
Return to top

Listing 9-23 FloatCommon.txt Page 182

Return to top
001: // Common to Float and Double classes
002: public static final double POSITIVE_INFINITY;
003: public static final double NEGATIVE_INFINITY;
004: public static final double NaN;
005: public static final double MAX_VALUE;
006: public static final double MIN_VALUE;
007: public boolean isNaN();
008: public boolean isInfinite();
Return to top

Listing 9-24 Float.txt Page 183

Return to top
001: // Float wrapper class constructors
002: public Float(float value);
003: public Float(double value);
004: public Float(String s);
005: 
006: // Float wrapper class methods
007: public static String toString(float f);
008: public static Float valueOf(String s);
009: public static float parseFloat(String s);
010: static public boolean isNaN(float v);
011: static public boolean isInfinite(float v);
012: public int compareTo(Float anotherFloat);
013: 
014: // Float wrapper class bit converters
015: public static native int floatToIntBits(float value);
016: public static native float intBitsToFloat(int bits);
Return to top

Listing 9-25 ParseFloat/ParseFloat.java Page 184

Return to top
001: class ParseFloat {
002:  public static void main(String args[]) {
003:   if (args.length < 1) 
004:    System.out.println("ex. java ParseFloat 3.14159");
005:   else {
006:    try {
007:     float f = Float.parseFloat(args[0]);
008:     System.out.println("Value == " + f);
009:    } catch (NumberFormatException e) {
010:     System.out.println("Error in argument " + e.getMessage());
011:    }
012:   }
013:  }
014: }
Return to top

Listing 9-26 Double.txt Page 184

Return to top
001: // Double wrapper class constructors
002: public Double(double value);
003: public Double(String s);
004: 
005: // Double wrapper class methods
006: public static String toString(double d);
007: public static Double valueOf(String s);
008: public static double parseDouble(String s);
009: static public boolean isNaN(double v);
010: static public boolean isInfinite(double v);
011: public int compareTo(Double anotherDouble);
012: 
013: // Double wrapper class bit converters
014: public static native long doubleToLongBits(double value);
015: public static native long doubleToRawLongBits(double value);
016: public static native double longBitsToDouble(long bits);
Return to top