2/21/08

To Notes

CSC 212 -- 2/21/08

 

Review Questions

  1. Define these terms:

    Ans: Inheritance is when a new class is created that contains all of the public and protected instance variables and methods of an existing class called the base class. Whereas public members are available for any class to use, protected members are only available for the inheriting class to use.

    Composition is where one class contains an object from another class.

    If class A is the base class and B is the derived class, A is also called the superclass and B is called the subclass.

  2. Explain how to use the is-a test.

    Ans: If x is-a y makes sense, where x is an object of A and y is an object of B, then A inherits B.

  3. Explain how to use the has-a test.

    Ans: If the object x of class A has-a y where y is an object of class B, then this is an example of composition.

  4. True or false. Constructors are never inherited.

    Ans: true.

  5. What is the super keyword used for?

    Ans: For two things (1) invoke a constructor from the base class and (2) invoke a method from the base class where that method is overridden in the derived class.

  6. True of false. The Object class is the parent of every class.

    Ans: True the Object class is directly or indirectly the parent class of every Java class.

  7. What is the output?

    Ans: 3.

  8. The first line of the file array.txt contains the number of rows and columns in a two-dimensional array, followed by the array of doubles itself. Write a main method that will instantiate the array and populate it with values from the input file.

    Ans:

    public static void main(String[ ] args) throws FileNotFoundException
    {
        Scanner s = new Scanner(new FileReader("array.txt"));
        int width = s.nextInt( );
        int height = s.nextInt( );
        int[ ][ ] array = new int[height][width];
        for(int j = 0; j < height; j++)
            for(int i = 0; i < height; i++)
                array[j][i] = s.nextInt( );
    
        for(int j = 0; j < height; j++)
        {
            for(int i = 0; i < height; i++)
                System.out.printf("%5.2f", array[j][i]); 
            System.out.println( );
        }
    }
    
  9. Variable Trace: show the changes in the reference and instance variables and predict the output.

    Trace1 Example:

      +---+           +---+            +---+
    a1| O------+    a2| O------+     b1| O------+
      +---+    |      +---+    |       +---+    |
               V               V                V
             w   h           w   h          w   h   d
           ----+----       ----+----      ----+---+----
             3   4          10   11        20  21  420
             3  11          11   18        23  28    3
             Output:   11x3
                       18x11
                       28x23x3
    
 

More Examples

 

The Object Class