CSCI A201/A597 and I210

Lab Notes Five

Second semester 2000-2001


Wrap-up of chapter 3.
Goals for this lab:

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; 
} 
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; 
    } 
} 
5. Same question about this one.

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. What does the following program print and why (or how)?
public class Six {
    public static void main(String[] args) {
	Calculator calc = new Calculator(); 
	int value = calc.fun(1 + calc.fun(1 + calc.fun(1)));  
	System.out.println(value); 
    } 
}

class Calculator {
    int fun(int x) {
	int result;
        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 x = new Calculator(); 
	int value = 1 + x.fun(1 + x.fun(1 + x.fun(1 + x.fun(1))));  
	System.out.println(value); 
    } 
}

class Calculator {
    int fun(int x) {
	int result;
        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) {
	Tomato t = new Tomato();
	t.f(); 
	t.f(); 
	t.g(); 
	System.out.println(t.n); 
	t.g();
	t.g(); 
	t.f(); 
	System.out.println(t.n); 
    }
}

class Tomato {
    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) {
	Lettuce a = new Lettuce();
	Lettuce b = new Lettuce();
	a.fun();
	a.fun();
	b.fun(); 
	a.fun();
	b.fun();
	b.fun();
	b.fun();
	a.fun();
    } 
}

class Lettuce {
    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) {
	Orange a = new Orange();
	Orange b = new Orange();
	a.fun();
	a.fun();
	b.fun(); 
	a.fun();
	b.fun();
	b.fun();
	b.fun();
	a.fun();
    } 
}

class Orange {
    int n;
    static int m;
    void fun() {
	n = n + 1;
	m = m + 1;
	System.out.println("n = " + n + ", m = " + m);
    }
}
18. Here's a summary of chapter 3:
19. And here's a diagram for this:
20. More generally the picture looks like this:

Last updated: Feb 8, 2001 by Adrian German for A201