DAT 501
Additional Exercises

  1. Write an execution trace of starting the following application:
    public class Test
    { private double d= 3.14;
      
      public Test()
      { int d= 2;
        s= d+s;
      }
    
      private String s= "X" + d;
    
      public void m()
      { s= d+s;
      }
    
     public static void main(String[] args)
     { Test T= new Test();
       T.m();
     }
    }
    
  2. In the previous exercise, what happens if the declarations of double d and String s change places? (Write an execution trace if you aren't sure.)
  3. Order as many as possible of the following types by the subtyping relation <=. For example, int <= double and MyFrameWriter <= JFrame , but not String <= boolean.
    boolean byte CelsiusToFahrenheit CelsiusToFahrenheit2 char double float int Integer JFrame JPanel long TestPanel MyPanel String
  4. Study the subtyping relation <=. For example, is it transitive? Is it symmetric?
  5. Using the syntax in 2.8.1 and 3.11.4, perform a syntactic analysis of class CelsiusToFahrenheit (fig. 8 in Chap. 3). For example, the first line of the main method is a STATEMENT of the form DECLARATION, starting with a TYPE, which is a PRIMITIVE_TYPE, which is a the keyword int. We can write this as a so-called syntax tree:
        .---------CLASS
       /            |
          .-------METHOD
         /         |
    METHOD_HEADER  METHOD_BODY
                   |
                STATEMENT
                   |
               DECLARATION-----.
               /    |      \    \
              /     |       \    \
            TYPE IDENTIFIER  =  INITIAL_EXPRESSION
             |
       PRIMITIVE_TYPE
             |
            int
    
    The exercise is to complete the tree for the entire class.
  6. Annotate every method invocation in the main methods of C and D below, as described in [S] 5.12.5.
    public class A
    {
      public void f(double a) { System.out.println("A"); }
    }
    
    public class B extends A
    {
      public void f(float a) { System.out.println("B"); }
    }
    
    public class C extends B
    {
      private void f(int a) { System.out.println("C"); }
    
      public static void main(String[] args)
        { C c= new C();
          c.f(7);
          c.f(7.0);
    
          A a= new C();
          a.f(7);
        }
    }
    
    public class D
    {
      public static void main(String[] args)
        { C c= new C();
          c.f(7);
          c.f(7.0);
      
          A a= new C();
          a.f(7);
        }
      
    }
    
  7. Which methods are invoked when you execute the main methods of C and D from the previous exercise? Use the section Executing method invocations from [S] 5.12.5 to explain why.
  8. Annotate every method invocation in the main method of G below, as described in [S] 5.12.5. Which methods are invoked when you execute the main methods of E? Use the section Executing method invocations from [S] 5.12.5 to explain why.
    public class E
    {
      public void m(int x) { System.out.println("E"); }
    }
    
    public class F extends E
    {
      public void m(int x) { System.out.println("F"); }
    }
    
    public class G
    {
      public static void main(String[] args)
        { E e= new E();
          E f= new F();
    
          e.m();
          f.m();
        }
    }
    
  9. Consider the storage configuration depicted in [S] 6.2.3. Assume that the user's next command is `S'. Draw the new storage configuration immediately before the next (recursive) call to processTransactions().
  10. [Rosen] presents an inference rule for the program segment
    while condition S
    on page 223. Suggest an inference rule for the program segment
    do S while condition
    (See [Schmidt] 7.15.2 for a description of the do-while loop.)
  11. Using the inference rule you constructed in the previous exercise, prove that the following program segment,
    i:= 1
    factorial:= 1
    do
      factorial:= factorial*i
      i:= i+1
    while i<=n
    terminates with factorial = n! when n is a positive integer. Hint: Imitate Example 4 of [Rosen, Sec. 3.5].
  12. Consider the following Java classes
    public class A 
    {
      public A(int x)
      { System.out.println(x); }
    
      public A()
      { this(0); }
    
      public void f()
      { System.out.println("Hej");
    }
    
    public class B extends A
    {
      public B(int x)
      { super(8); }
    
      public B(double x)
      { this(7);
        this.f();
        super.f();
        f();
      }
    }
    

    What is written by the following invocations:

    A a1= new A(5);
    A a2= new A();
    B b1= new B(5);
    B b2= new B(5.0);
    
  13. For the example in the previous exercise, write an execution trace. Especially, be precise about at what adresses the various `this' and `super' receivers are.
  14. Consider the following program, given an array
    int[] b
    and an integer
    int x
    .
    int i= 0;
    while (i < b.length && b[i] != x)
      i= i+1; 
    
    What is the value of
    i
    after termination? Prove that, formally.