5.11 Problems

 < Day Day Up > 



5.11 Problems

  1. What happens if a class implements an interface but does not define the methods specified? What happens if it defines the methods but does not implement the interface?

  2. Identify all of the errors in the program in Exhibit 15. Are the problems occurring at compile time or runtime?

    Exhibit 15: Interface Example

    start example

     interface ExampleInterface {   public void method1(); } class Class1 implements ExampleInterface {   public void method1() {}   public void method2() {} } class Class2 {   public void method1() {}   public void method2() {} } public class Problem52 {   public static void main(String args[]) {     Class1 c1 = new Class1();     Class2 c2 = new Class2();     Object o1;     ExampleInterface ei1;     ExampleInterface ei2 = new ExampleInterface();     o1 = c1;     o1.method1();     ei1 = c1;     ei1 = o1;     ei1 = (ExampleInterface) o1;     ei1.method1();     o1 = c2;     o1.method1();     ei1 = c2;     ei1 = o1;     ei1 = (ExampleInterface) o1;     ei1.method1();   } } 

    end example

  3. Explain how Java implements polymorphism with interfaces; that is, how does Java determine the method in the correct class to call when an interface is used?

  4. Complete the implementation of the SortedPrintTable in Exhibit 10 (Program5.4b) by including the delete method. Modify the Person class in Program in Exhibit 11 (Program5.4c) to test your program.

  5. Show that the SortedPrintTable is usable by different types of objects by implementing a table of cars using the Car class in Exhibit 7 (Program5.3d). Note that you will have to implement PrintableSortable in the Car class to make it work.

  6. While instance variables cannot be defined for an interface, it is possible to define static and final variables. Why do interfaces allow static and final variables to be defined? What are some examples of defining static or final variables in an interface?

  7. What methods are in the java.lang.Comparable interface? How is the Comparable interfaced used with the Java collection classes?

  8. A tagged interface is an interface in Java that does not have any methods defined. One example of a tagged interface is java.io.Serializable. What are some other examples of tagged interfaces? Why does Java have tagged interfaces?

  9. Change the ExpressionTree program to include nodes that have variables. To implement a variable, store an object of type Float in a Hashtable. For the key to the Hashtable, use a string that names the variable. Now create a new type of node called a VariableNode, which stores the name of the variable and whose evaluate method looks up the value for the node.

  10. Write a simple calculator that can be used to do simple arithmetic. The calculator should be able to do "+," "-," "*," and "/." It should also be able to use either numbers or variables in equations and be able to store and retrieve variables. To do this from a command prompt, read in the line of text and use the StringTokenizer to break the string down into tokens and delimiters (make sure the StringTokenizer is set to return delimiters as well as tokens and does not require spaces between all tokens). Then, assume that the first token is either a variable to store the result into or the word "output", which will write the result using System.out. A simple example of using the calculator would be:

       A1 = 2.0   B2 = 4.0 * A1   output A1 B2 

    This would produce the result:

       A1 = 2.0, B2 = 8.0 

    You can tell if a token is an operator, number, or variable as follows. First check if it is an operator. If not, then see if the first character is a number (digit). If it is, the token is a number, else it is a variable.

  11. A Bubble Sort is implemented by comparing two adjacent elements and swapping them if they are not in the correct order. The program in Exhibit 16 implements a Bubble Sort for an integer array. Implement the Bubble Sort so that it can be used with the compareTo method from the java.lang.Comparable interface. Create a Person class that implements the java.lang.Comparable interface, and implement an array that contains several Person objects. Show that your Bubble Sort works by sorting this array of Person objects in ascending order. Change the compareTo method so that the array is sorted in descending order.

    Exhibit 16: Implementing a Bubble Sort for an Integer Array

    start example

     public class Problem511 {   public static void swap(int array[], int i, int j) {     int temp = array[i];     array[i] = array[j];     array[j] = temp;   }   public static void bubbleSort(int array[]) {     boolean swapMade = false;     for (int i = 0; i < (array.length-1); i++) {       swapMade = false;       for (int j = (i+1); j < array.length; j++) {         if (array[i] > array[j]) {           swapMade = true;           swap(array, i, j);         }       }       if (!swapMade)         break;     }   }   public static void main(String args[]) {     int array[] = {5, 8, 1, 4, 9, 6, 2, 3, 7};     bubbleSort(array);     for (int i = 0; i < array.length; i++) {       System.out.println(array[i]);     }   } } 

    end example

  12. Explain why the definition below for the class Problem5.6 is incorrect. Why does Java implement this check?

       public class Problem56 implements PrintableSortable {     boolean eq(Sortable s) {     }     boolean gt(Sortable s) {     }     void print() {     }   } 



 < Day Day Up > 



Creating Components. Object Oriented, Concurrent, and Distributed Computing in Java
The .NET Developers Guide to Directory Services Programming
ISBN: 849314992
EAN: 2147483647
Year: 2003
Pages: 162

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net