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

Java 2 Just Click! Solutions

Chapter 12 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 12-1 TheInterface/TheInterface.java Page 232

Return to top
001: class MyException extends Exception { }
002: 
003: // Declare the interface (normally would be public)
004: interface MyInterface {
005:  void myMethod() throws MyException;
006: }
007: 
008: // Implement the interface
009: class MyImplementation
010:  implements MyInterface {
011:  public void myMethod() throws MyException {
012:   System.out.println("in myMethod()");
013:   throw new MyException();
014:  }
015: }
016: 
017: // Main program class
018: class TheInterface {
019:  public static void main(String args[]) {
020:   MyImplementation m = new MyImplementation();
021:   try { 
022:    m.myMethod();  // Exception always thrown
023:   } catch (MyException e) {
024:    System.out.println("MyException caught");
025:   }
026:  }
027: }
Return to top

Listing 12-2 InterfaceDemo/TContainerInterface.java Page 234

Return to top
001: class ContainerFullException extends Exception { }
002: class NoSuchObjectException extends Exception { }
003: 
004: public interface TContainerInterface {
005:  void PutObject(Object obj)
006:   throws ContainerFullException;
007:  Object GetObject(int n)
008:   throws NoSuchObjectException;
009:  int GetCount();
010:  void Sort();
011: }
Return to top

Listing 12-3 InterfaceDemo/TContainer.java Page 235

Return to top
001: import TContainerInterface;
002: import java.util.Arrays;
003: 
004: class TContainer implements TContainerInterface {
005:  int count;          // Number of strings in array
006:  String strArray[];  // The array of strings
007: 
008: // Constructor (not declared in the interface)
009:  TContainer(int n) {
010:   if (n <= 0) n = 1;
011:   count = 0;
012:   strArray = new String[n];
013:  }
014: 
015: // Put an object into the container
016:  public void PutObject(Object obj)
017:   throws ContainerFullException {
018:   if (count >= strArray.length)
019:    throw new ContainerFullException();
020:   strArray[count++] = (String)obj;
021:  }
022: 
023: // Return object n from the container
024:  public Object GetObject(int n)
025:   throws NoSuchObjectException {
026:   if (n < 0 || n >= count)
027:    throw new NoSuchObjectException();
028:   return strArray[n];
029:  }
030: 
031: // Return number of objects in container
032:  public int GetCount() {
033:   return count;
034:  }
035: 
036: // Sort objects in the container  
037:  public void Sort() {
038:   if (count > 1)
039:    Arrays.sort(strArray, 0, count);
040:  }
041: }
Return to top

Listing 12-4 InterfaceDemo/InterfaceDemo.java Page 237

Return to top
001: import TContainerInterface;
002: import TContainer;
003: 
004: class InterfaceDemo {
005: 
006: // Show objects in container
007:  public static void ShowAllObjects(
008:   TContainerInterface C, String label) {
009:   System.out.println(label);
010:   try {
011:    for (int i = 0; i <= C.GetCount() - 1; i++)
012:     System.out.print(C.GetObject(i) + "  ");
013:    System.out.println();
014:   } catch (NoSuchObjectException e) {
015:    // Should never execute
016:    System.out.println("/n *** Error in for loop!");
017:   }
018:  }
019: 
020: // Main program demonstrates using the container
021:  public static void main(String args[]) {
022:   TContainer container = new TContainer(100);
023:   try {
024:    container.PutObject("Mexico");
025:    container.PutObject("Canada");
026:    container.PutObject("United States");
027:    container.PutObject("Honduras");
028:    container.PutObject("Bahamas");
029:    container.PutObject("England");
030:    container.PutObject("Germany");
031:    container.PutObject("France");
032:    ShowAllObjects(container, "Before sorting");
033:    container.Sort();
034:    ShowAllObjects(container, "After sorting");
035:   } catch (ContainerFullException e) {
036:    System.out.println("Container overflow error");
037:   }
038:  }
039: }
Return to top

Listing 12-5 CloneDemo/CloneDemo.java Page 240

Return to top
001: // A simple string container class
002: class IntContainer implements Cloneable {
003:  private int size;  // Size (capacity) of array
004:  private int intArray[];
005:  public IntContainer(int n) {
006:   intArray = new int[n];
007:   size = n;
008:  }
009:  public int getValue(int n) { 
010:   return intArray[n]; 
011:  }
012:  public void putValue(int index, int value) { 
013:   intArray[index] = value;
014:  }
015:  public int size() {
016:   return size;
017:  }
018: /*
019: // The WRONG way to clone
020:  public Object clone() throws CloneNotSupportedException {
021:   return super.clone();  // ???
022:  }
023: */
024: // The RIGHT way to clone
025:  public Object clone() throws CloneNotSupportedException {
026:   IntContainer temp = (IntContainer)super.clone();
027:   temp.intArray = (int[])intArray.clone();
028:   return temp;
029:  }
030: }   
031: 
032: // Main program class
033: class CloneDemo {
034: 
035:  // Display values in two containers side-by-side
036:  public static void showContainers(String msg, 
037:   IntContainer c1, IntContainer c2) {
038:   System.out.println("/n" + msg);
039:   for (int i = 0; i < c1.size(); i++) {
040:    System.out.print(i + " : " + c1.getValue(i) + "   /t");
041:    System.out.println(c2.getValue(i));
042:   }
043:  }
044: 
045:  public static void main(String args[]) {
046:   // Construct a container and randomize its content
047:   IntContainer original = new IntContainer(10);
048:   for (int i = 0; i < original.size(); i++)
049:    original.putValue(i, (int)(Math.random() * 100));
050:   try {
051:    // Clone the container
052:    IntContainer clone = (IntContainer)original.clone();
053:    showContainers("Before change", original, clone);  
054:    // Modify a value in the clone at index 1
055:    clone.putValue(1, clone.getValue(1) * 2);
056:    showContainers("After change", original, clone);
057:   } catch (CloneNotSupportedException e) {
058:    System.out.println(e.getMessage());
059:   }
060:  }
061: }
Return to top