Spring Semester 2005


Lab Notes Nine: Wrap-up of Object-Oriented Concepts

Let's go through a set of examples to clarify object-oriented concepts 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?

For this lab try to obtain answers to all these questions from your AI.

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)); 
  }
}

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


Last updated: May 16, 2004 by Adrian German for A201