Spring Semester 2002


Lab Notes Five: Wrap-up of Chapter 3

Let's go through a set of examples to clarify chapter 3 even further.

  1. What's this program doing?

    Can you draw a diagram to illustrate what happens when you run it?

    public class One {
        public static void main(String[] args) {
    	Potato p = new Potato(); 
    	Potato q;
    	q = new Potato(); 
    	p = q;  
        } 
    } 
    
    class Potato {
    
    }
    How do the Potatoes get created?

  2. What is this next program doing?

    Can you diagram it? What's new?

    public class Two {
        public static void main(String[] args) {
    	Pair u = new Pair(); 
    	Pair v;
    	v = new Pair(); 
            u.a = 1;
            u.b = 2;
            u.a = u.a + u.b; 
            u.b = 1 - u.a; 
        } 
    } 
    
    class Pair {
        int a;
        int b;
    } 
  3. Why does this next program not compile?

    What's wrong with it?

    public class Three {
        public static void main(String[] args) {
    	Pair u = new Pair(); 
    	Pair v = new Pair(1, 2); 
        }
    } 
    
    class Pair {
        int a;
        int b; 
    } 
    Can you fix it?

  4. What does the next program print and why (or how).

    public class Four {
        public static void main(String[] args) {
    	Calculator m = new Calculator(); 
    	int value = m.fun(3); 
    	System.out.println(value); 
        } 
    } 
    
    class Calculator {
        int fun(int x) {
    	int result;
            result = 3 * x + 1; 
            return result; 
        } 
    } 
    Same question if we change the Calculator as follows:
    class Calculator {
        int fun(int x) {
    	int result;
            result = g(x) + 1; 
            return result; 
        } 
        int g(int x) {
            int result;
            result = 3 * x; 
            return result; 
        }
    } 
  5. What does the following program print and why (or how)?
    public class Five {
        public static void main(String[] args) {
    	Calculator c = new Calculator(); 
    	int value = c.fun(1) + c.fun(c.fun(2)); 
    	System.out.println(value); 
        } 
    } 
    
    class Calculator {
        int fun(int x) {
    	int result;
            result = 3 * x + 1; 
            return result; 
        } 
    }
  6. Same question about this one:

    public class Six {
        public static void main(String[] args) {
    	Calculator calc = new Calculator(); 
    	int value = calc.fun(
                          calc.fun(
                            calc.fun(
                              calc.fun(
                                calc.fun(5)))));  
    	System.out.println(value); 
        } 
    }
    
    class Calculator {
        int fun(int x) {
    	int result;
            if (x % 2 == 0) 
              result = x / 2; 
            else 
              result = 3 * x + 1; 
            return result; 
        } 
    } 
  7. What's the output of the following program and why?

    public class Seven {
        public static void main(String[] args) {
    	Oracle a = new Oracle(); 
    	System.out.println(a.odd(5));
    	System.out.println(a.odd(6));
    	System.out.println(a.odd(7));
    	System.out.println(a.odd(8));
    	System.out.println(a.odd(9));
        } 
    } 
    
    class Oracle {
        boolean odd(int n) {
    	boolean result; 
    	if (n % 2 == 0) {
    	    result = false; 
    	} else {
    	    result = true; 
    	} 
    	return result; 
        }
    }
  8. Let's now review a previous step once again.

    What's the output of this program and why (or how)?

    public class Eight {
        public static void main(String[] args) {
            Calculator calc = new Calculator(); 
            int value = calc.fun(
                          calc.fun(
                            calc.fun(
                              calc.fun(
                                calc.fun(27)))));  
            System.out.println(value); 
        } 
    }
                  
    class Calculator {
        int fun(int x) {
            int result;
            if (x % 2 == 0) 
              result = x / 2; 
            else 
              result = 3 * x + 1; 
            return result; 
        } 
    } 
  9. Now fasten your seat belts and look closely.

    What's the output of this program and why (or how)?

    public class Nine {
        public static void main(String[] args) {
    	Alien x = new Alien(); 
    	int value = x.what(4); 
    	System.out.println(value); 
        } 
    }
    
    class Alien {
        int what(int x) {
    	int result;
    	if (x == 1) { 
    	    result = 1;
    	} else {
    	    result = x + what(x - 1);
    	}
            return result; 
        } 
    } 
  10. What's a good name for the method what?

  11. What's the output of this program?

    public class Eleven {
        public static void main(String[] args) {
    	Alien x = new Alien(); 
    	int value = x.what(10); 
    	System.out.println(value); 
        } 
    }
    
    class Alien {
        int what(int x) {
    	int result;
    	if (x == 1) { 
    	    result = 1;
    	} else {
    	    result = x + what(x - 1);
    	}
            return result; 
        } 
    }
  12. What's the output of this program and why (or how)?

    public class Twelve {
        public static void main(String[] args) {
    	Alien x = new Alien(); 
    	int value = x.what(10); 
    	System.out.println(value); 
        } 
    }
    
    class Alien {
        int what(int x) {
    	int result;
    	if (x == 1) { 
    	    result = 1;
    	} else {
                System.out.println(x); 
    	    result = x + what(x - 1);
    	}
            return result; 
        } 
    } 
  13. What's the output of this program and why (or how)?

    public class Thirteen {
        public static void main(String[] args) {
    	Alien x = new Alien(); 
    	int value = x.what(10); 
    	System.out.println(value); 
        } 
    }
    
    class Alien {
        int what(int x) {
    	int result;
    	if (x == 1) { 
    	    result = 1;
    	} else {
    	    result = x + what(x - 1);
                System.out.println(x); 
    	}
            return result; 
        } 
    } 
  14. What's the output of this program and why?

    public class Fourteen {
        public static void main(String[] args) {
    	A a = new A();
    	a.fun();
    	a.fun();
    	a.fun();
    	System.out.println(a.n);
        }
    }
    
    class A {
        int n;
        void fun() {
    	n += 1;
        }
    }
  15. What's the output of this program and why?

    public class Fifteen {
        public static void main(String[] args) {
    	Vegetable tomato = new Vegetable();
    	tomato.f(); 
    	tomato.f(); 
    	tomato.g(); 
    	System.out.println(tomato.n); 
    	tomato.g();
    	tomato.g(); 
    	tomato.f(); 
    	System.out.println(tomato.n); 
        }
    }
    
    class Vegetable {
        int n;
        void f() {
    	n = n + 1;
        }
        void g() {
            n = n + 1; 
        } 
    
    }
  16. What is the output of this program and why?

    public class Sixteen {
        public static void main(String[] args) {
    	Vegetable tomato = new Vegetable();
    	Vegetable potato = new Vegetable();
    	tomato.fun();
    	tomato.fun();
    	potato.fun(); 
    	tomato.fun();
    	potato.fun();
    	potato.fun();
    	potato.fun();
    	tomato.fun();
        } 
    }
    
    class Vegetable {
        int n;
        int m;
        void fun() {
    	n = n + 1;
    	m = m + 1;
    	System.out.println("n = " + n + ", m = " + m);
        }
    }
  17. What is the output of this program and why?

    public class Seventeen {
        public static void main(String[] args) {
    	Vegetable tomato = new Vegetable();
    	Vegetable potato = new Vegetable();
    	tomato.fun();
    	tomato.fun();
    	potato.fun(); 
    	tomato.fun();
    	potato.fun();
    	potato.fun();
    	potato.fun();
    	tomato.fun();
        } 
    }
    
    class Vegetable {
        int n;
        static int m;
        void fun() {
    	n = n + 1;
    	m = m + 1;
    	System.out.println("n = " + n + ", m = " + m);
        }
    }
  18. Please describe briefly what this picture represents:
    Define all terms involved, as briefly and completely as you can.

  19. Take a look at the diagram below:
    Then please answer the following question:
    What's this?

  20. More generally the picture looks like this:

    Can you summarize the diagram briefly?

What comes next is your:

A201/A597 LAB ASSIGNMENT FIVE

Next time you need to work, and prepare the following, to show to your AIs:

Please do not write more than one sheet of paper front and back.

Please write legibly so I can understand, this will be 40% of your Lab Five grade! This will also determine what the AIs will ask you during the lab, so the more answers you get right the less questions you will be asked during the lab. In your papers try to give honest answers.

After I collect your papers

I will ask the AIs to emphasize those questions that gave you most trouble (we will identify them on an individual basis). This way next Thursday I can also offer a bit of review on these questions, if need be, and depending on what your papers reveal.

In addition, please explain why this doesn't work. Then fix it in two different ways.

public class Math {
  public static void main(String[] args) {
    int a = 5;
    int b = 7; 
    System.out.println(Math.max(a, b)); 
  }
}
(This problem was presented on the "What's New?" page on Jan 17.)

If you have any questions or need any help please let us know!


Last updated: Feb 4, 2002 by Adrian German for A201