|
Spring Semester 2003 |
Let's go through a set of examples to clarify object-oriented concepts even further.
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?
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;
}
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?
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;
}
}
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;
}
}
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;
}
}
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;
}
}
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;
}
}
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;
}
}
what?
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;
}
}
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;
}
}
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;
}
}
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;
}
}
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;
}
}
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);
}
}
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);
}
}
Define all terms involved, as briefly and completely as you can.![]()
Then please answer the following question:![]()
What's this?
![]()
Can you summarize the diagram briefly?
A201/A597 LAB ASSIGNMENT FIVE
What you need to do:
In lab, the AI will
In your papers try to give honest, direct answers.
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!