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

Java 2 Just Click! Solutions

Chapter 10 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 10-1 ArrayBounds/ArrayBounds.java Page 190

Return to top
001: class ArrayBounds {
002:  public static void main(String args[]) {
003:   int intArray[] = new int[10]; // Create array
004:   try {
005:    int q = intArray[5];   // no error
006:    int p = intArray[11];  // throws exception
007:   } catch (ArrayIndexOutOfBoundsException e) {
008:    System.out.println("Array index out of bounds");
009:   }
010:  }
011: }
Return to top

Listing 10-2 Triangle/Triangle.java Page 192

Return to top
001: class Triangle {
002:  public static void main(String args[]) {
003:   // Create a triangular array
004:   int triangular[][];
005:   triangular = new int[8][];
006:   for (int i = 0; i < triangular.length; i++)
007:    triangular[i] = new int[i + 1];
008: 
009:   // Assign values at random to the array
010:   for (int i = 0; i < triangular.length; i++)
011:    for (int j = 0; j < triangular[i].length; j++)
012:     triangular[i][j] = (int)(Math.random() * 100);
013: 
014:   // Display the array's contents
015:   for (int i = 0; i < triangular.length; i++) {
016:    for (int j = 0; j < triangular[i].length; j++)
017:     System.out.print(" /t" + triangular[i][j]);
018:    System.out.println();
019:   }
020:  }
021: }
Return to top

Listing 10-3 ObjectArray/ObjectArray.java Page 194

Return to top
001: class StringClass {
002:  private String s;
003:  // Constructor
004:  StringClass(String s) { 
005:   this.s = s; 
006:  }
007:  void ShowString() {
008:   System.out.println(s);
009:  }
010: }
011: 
012: class ObjectArray {
013:  public static void main(String args[]) {
014:   // Construct an array of class objects
015:    StringClass WeekDays[] = {
016:     new StringClass("Domingo"),
017:     new StringClass("Lunes"),
018:     new StringClass("Martes"),
019:     new StringClass("Miercoles"),
020:     new StringClass("Jueves"),
021:     new StringClass("Viernes"),
022:     new StringClass("Sabado")
023:    };
024:   // Call a method for each arrayed object
025:   System.out.println("Weekdays in Spanish");
026:   for (int i = 0; i < WeekDays.length; i++)
027:    WeekDays[i].ShowString();
028:  }
029: }
Return to top

Listing 10-4 ArrayCopy/ArrayCopy.java Page 196

Return to top
001: import TestClass;  // Import submodule
002: 
003: class ArrayCopy {
004:  // Declare the two arrays
005:  public static int[] apples, oranges;
006: 
007:   // Array copy method #1
008:  public static void CopyMethod1() {
009:   System.out.println("/nArray copy method #1");
010:   oranges = apples;
011: // oranges[0]++;    // Enable to change test
012:   TestClass.CompareArrays(apples, oranges);
013:  }
014: 
015:  // Array copy method #2
016:  public static void CopyMethod2() {
017:   System.out.println("/nArray copy method #2");
018:   oranges = new int[apples.length];
019:   System.arraycopy(apples, 0, oranges, 0, apples.length);
020:  oranges[0]++;    // Enable to change test
021:   TestClass.CompareArrays(apples, oranges);
022:  }
023: 
024:  // Array copy method #3
025:  public static void CopyMethod3() {
026:   System.out.println("/nArray copy method #3");
027:   oranges = (int[])apples.clone();
028: // oranges[0]++;    // Enable to change test
029:   TestClass.CompareArrays(apples, oranges);
030:  }
031: 
032:  public static void main(String args[]) {
033:   // Construct and initialize the first array
034:   apples = new int[8];
035:   for (int i = 0; i < apples.length; i++)
036:    apples[i] = (int)(Math.random() * 100);
037:   // Copy three ways and test each copy
038:   CopyMethod1();
039:   CopyMethod2();
040:   CopyMethod3();
041:  }
042: }
Return to top

Listing 10-5 ArrayCopy/TestClass.java Page 197

Return to top
001: class TestClass {
002:  public static void CompareArrays(int apples[], int oranges[])
003:  {
004:   // Display the array values
005:   int i;
006:   System.out.print("apples : ");
007:   for (i = 0; i < apples.length; i++)
008:    System.out.print(apples[i] + " /t");
009:   System.out.print("/noranges: ");
010:   for (i = 0; i < oranges.length; i++)
011:    System.out.print(oranges[i] + " /t");
012:   System.out.println();  // Start new line
013: 
014:   // Test if the array references are the same
015:   if (apples == oranges)
016:    System.out.println("Array references are identical");
017:   else
018:    System.out.println("Array references are NOT identical");
019: 
020:   // Test if the array contents are the same
021:   boolean identical = true;
022:   for (i = 0; i < apples.length; i++)
023:    if (apples[i] != oranges[i])
024:     identical = false;
025:   if (identical)
026:    System.out.println("Array contents are the same");
027:   else
028:    System.out.println("Array contents are NOT the same");
029:  }
030: }
Return to top

Listing 10-6 ArraysSort.txt Page 202

Return to top
001: // Arrays class sorting methods
002: public static void sort(long[] a);
003: public static void sort(long[] a, int fromIndex, int toIndex);
004: public static void sort(int[] a);
005: public static void sort(int[] a, int fromIndex, int toIndex);
006: public static void sort(short[] a);
007: public static void sort(short[] a, int fromIndex, int toIndex);
008: public static void sort(char[] a);
009: public static void sort(char[] a, int fromIndex, int toIndex);
010: public static void sort(byte[] a);
011: public static void sort(byte[] a, int fromIndex, int toIndex);
012: public static void sort(double[] a);
013: public static void sort(double[] a, int fromIndex, int toIndex);
014: public static void sort(float[] a);
015: public static void sort(float[] a, int fromIndex, int toIndex);
016: public static void sort(Object[] a);
017: public static void sort(Object[] a, int fromIndex, int toIndex);
018: public static void sort(Object[] a, Comparator c);
019: public static void sort(Object[] a, int fromIndex, int toIndex, Comparator c);
Return to top

Listing 10-7 SortStrings/SortStrings.java Page 203

Return to top
001: import java.util.Arrays;
002: 
003: class SortStrings {
004:  // Display an array of Strings
005:  public static void ShowStrings(String[] a, String msg) {
006:   System.out.println(msg);
007:   for (int i = 0; i < a.length; i++)
008:    System.out.println(a[i]);
009:  }  
010:  // Create, sort, and display an array of StringClass objects
011:  public static void main(String args[]) {
012:   String colors[] = {
013:    "rojo", "azul", "verde", "negro", "blanco", "cafe", "gris"
014:   };
015:   ShowStrings(colors, "/nBefore sorting");
016:   Arrays.sort(colors);
017:   ShowStrings(colors, "/nAfter sorting");  
018:  }
019: }
Return to top

Listing 10-8 SortObjects/SortObjects.java Page 204

Return to top
001: import java.util.Arrays;
002: 
003: class StringClass implements Comparable {
004:  private String s;
005:  StringClass(String s) { 
006:   this.s = s; 
007:  }
008:  void ShowString() {
009:   System.out.println(s);
010:  }
011:  public int compareTo(Object other) {
012:   StringClass sc = (StringClass)other;
013:   return s.compareTo(sc.s);
014:  } 
015: }
016: 
017: class SortObjects {
018: 
019:  // Display an array of StringClass objects
020:  public static void ShowStrings(StringClass[] a, String msg) {
021:   System.out.println(msg);
022:   for (int i = 0; i < a.length; i++)
023:    a[i].ShowString();
024:  }  
025: 
026:  // Create, sort, and display an array of StringClass objects
027:  public static void main(String args[]) {
028:   StringClass colors[] = {
029:    new StringClass("rojo"),
030:    new StringClass("azul"),
031:    new StringClass("verde"),
032:    new StringClass("negro"),
033:    new StringClass("blanco"),
034:    new StringClass("cafe"),
035:    new StringClass("gris")
036:   };
037:   ShowStrings(colors, "/nBefore sorting");
038:   Arrays.sort(colors);
039:   ShowStrings(colors, "/nAfter sorting");  
040:  }
041: }
Return to top

Listing 10-9 SortComparator/SortComparator.java Page 206

Return to top
001: import java.util.Arrays;
002: import java.util.Comparator;
003: 
004: // Class that implements the Comparator interface
005: class StringCompare implements Comparator {
006:  public int compare(Object o1, Object o2) {
007:   String s1 = (String)o1;
008:   String s2 = (String)o2;
009:   return s1.compareTo(s2);
010:  }
011: }
012: 
013: class SortComparator {
014:  // Display an array of Strings
015:  public static void ShowStrings(String[] a, String msg) {
016:   System.out.println(msg);
017:   for (int i = 0; i < a.length; i++)
018:    System.out.println(a[i]);
019:  }  
020:  // Create, sort, and display an array of StringClass objects
021:  public static void main(String args[]) {
022:   String colors[] = {
023:    "rojo", "azul", "verde", "negro", "blanco", "cafe", "gris"
024:   };
025:   ShowStrings(colors, "/nBefore sorting");
026:   // Construct the Comparator object
027:   StringCompare CompareObject = new StringCompare();
028:   // Sort the array using the Comparator object
029:   Arrays.sort(colors, CompareObject);
030:   ShowStrings(colors, "/nAfter sorting");  
031:  }
032: }
Return to top

Listing 10-10 ArraysSearch.txt Page 207

Return to top
001: // Arrays class searching methods
002: public static int binarySearch(long[] a, long key);
003: public static int binarySearch(int[] a, int key);
004: public static int binarySearch(short[] a, short key);
005: public static int binarySearch(char[] a, char key);
006: public static int binarySearch(byte[] a, byte key);
007: public static int binarySearch(double[] a, double key);
008: public static int binarySearch(float[] a, float key);
009: public static int binarySearch(Object[] a, Object key);
010: public static int binarySearch(Object[] a, Object key, Comparator c);
Return to top

Listing 10-11 ArraysEqual.txt Page 208

Return to top
001: // Arrays class equality testing methods
002: public static boolean equals(long[] a, long[] a2);
003: public static boolean equals(int[] a, int[] a2);
004: public static boolean equals(short[] a, short a2[]);
005: public static boolean equals(char[] a, char[] a2);
006: public static boolean equals(byte[] a, byte[] a2);
007: public static boolean equals(boolean[] a, boolean[] a2);
008: public static boolean equals(double[] a, double[] a2);
009: public static boolean equals(float[] a, float[] a2);
010: public static boolean equals(Object[] a, Object[] a2);
Return to top

Listing 10-12 ArraysFill.txt Page 209

Return to top
001: // Arrays class filling methods
002: public static void fill(long[] a, long val);
003: public static void fill(long[] a, int fromIndex, int toIndex, long val);
004: public static void fill(int[] a, int val);
005: public static void fill(int[] a, int fromIndex, int toIndex, int val);
006: public static void fill(short[] a, short val);
007: public static void fill(short[] a, int fromIndex, int toIndex, short val);
008: public static void fill(char[] a, char val);
009: public static void fill(char[] a, int fromIndex, int toIndex, char val);
010: public static void fill(byte[] a, byte val);
011: public static void fill(byte[] a, int fromIndex, int toIndex, byte val);
012: public static void fill(boolean[] a, boolean val);
013: public static void fill(boolean[] a, int fromIndex, int toIndex, boolean val);
014: public static void fill(double[] a, double val);
015: public static void fill(double[] a, int fromIndex, int toIndex,double val);
016: public static void fill(float[] a, float val);
017: public static void fill(float[] a, int fromIndex, int toIndex, float val);
018: public static void fill(Object[] a, Object val);
019: public static void fill(Object[] a, int fromIndex, int toIndex,Object val);
Return to top

Listing 10-13 ArraysList.txt Page 211

Return to top
001: // Arrays class List methods
002: public static List asList(Object[] a);
003:   public int size();
004:   public Object[] toArray();
005:   public Object get(int index);
006:   public Object set(int index, Object element);
007:   public int indexOf(Object o);
008:   public boolean contains(Object o);
Return to top

Listing 10-14 ArraysList/ArraysList.java Page 211

Return to top
001: import java.util.Arrays;
002: import java.util.List;
003: 
004: class ArraysList {
005:  public static void main(String args[]) {
006:   // Create array of strings
007:   String fruits[] = {
008:    "apple", "banana", "cherry", "pear"
009:   };
010:   // Convert array to a List object
011:   List fruitList = Arrays.asList(fruits);
012:   // Call various List methods for the array  
013:   int size = fruitList.size();
014:   System.out.println("List size = " + size);
015:   String s = (String)fruitList.get(2);
016:   System.out.println("List element #2 = " + s);
017:   if (fruitList.contains("banana"))
018:    System.out.println("fruitList contains banana");
019:  }
020: }
Return to top