|
MIDTERM EXAM
Second Summer 2003 |
class One {
int a;
static int b;
void fun(One other) {
this.a = this.a + 2;
other.a = other.a + 3;
One.b = One.b + 1;
System.out.println
(this.a + " " + other.a + " " + One.b);
}
public static void main(String[] args) {
One potato = new One();
One tomato = new One();
tomato.fun(potato);
tomato.fun(potato);
tomato.fun(tomato);
potato.fun(potato);
potato.fun(tomato);
tomato.fun(new One());
(new One()).fun(potato);
(new One()).fun(new One());
}
}
lh102sun.cs.indiana.edu%javac One.java
lh102sun.cs.indiana.edu%java One
2 3 1
4 6 2
9 9 3
11 11 4
13 12 5
14 3 6
2 16 7
2 3 8
lh102sun.cs.indiana.edu%
class Two {
public static void main(String[] args) {
System.out.println(2 / 3 + " " + 2 % 3);
System.out.println(('A' - 'C') * 10 % 8);
int x = 1, y = 1;
if (x > 2) { y = 2; y = y + 1; } else y = y + 1;
System.out.println(y);
if (x < 2) y = 2; else y = y + 1; y = y - 1;
System.out.println(y);
for (int i = -7; i <= 12; i = i + 4)
System.out.print("?");
System.out.println("!");
for (int i = -12; i <= 6; i = i + 9) {
System.out.print("?");
System.out.println("!");
}
if (false) x = 2; else if (true) x = 3; else x = 1;
if (x == 3) System.out.println("How are you?");
System.out.println("Not bad...");
for (int i = 0; i < 3; i++) {
x = x + y; y = x - y; x = x - y;
System.out.println("(" + x + ", " + y + ")");
}
if (false && false || true)
System.out.println("Good bye!");
}
}
lh102sun.cs.indiana.edu%javac Two.java
lh102sun.cs.indiana.edu%java Two
0 2
-4
2
1
?????!
?!
?!
?!
How are you?
Not bad...
(1, 3)
(3, 1)
(1, 3)
Good bye!
lh102sun.cs.indiana.edu%
class Three {
public static void main(String[] args) {
int x = 3 % 5, y = 5 / 3;
while (x >= 0 || y >= 0 && (y - x) < 100) {
x = x / 3 - 7 * y;
y = 3 * y - 2 / x;
System.out.println("(" + x + ", " + y + ")");
}
Three tomato = new Three();
Three potato = new Three(1);
for (potato.fun(); tomato.a * Three.b < 6;
tomato.fun()) {
potato.nuf();
tomato.nuf();
x = x + 1;
}
}
int a;
static int b;
void fun() {
this.a = this.a + 1;
Three.b = Three.b - 2;
System.out.println("(" + a + ", " + b + ")");
}
void nuf() {
this.a = this.a - 2;
Three.b = Three.b + 1;
System.out.println("(" + a + ", " + b + ")");
}
Three() {
System.out.println("-----------------------");
}
Three(int value) { this.a = value; }
}
lh102sun.cs.indiana.edu%javac Three.java
lh102sun.cs.indiana.edu%java Three
(-6, 3)
(-23, 9)
(-70, 27)
(-212, 81)
-----------------------
(2, -2)
(0, -1)
(-2, 0)
(-1, -2)
(-2, -1)
(-3, 0)
(-2, -2)
(-4, -1)
(-4, 0)
(-3, -2)
lh102sun.cs.indiana.edu%
class Four {
public static void main(String[] args) {
System.out.println(false && true);
System.out.println(true && (false || true));
System.out.println(! (true || false));
System.out.println(! (false && true));
System.out.println(! true && (false || ! true));
System.out.println
(! (false && (! true || false)));
System.out.println(
! (false && (! true || ! (true && ! false))));
for (int i = 0; i < 5; i++) {
Four.fun(i);
System.out.println();
}
}
static void fun(int x) {
if (x > 0) System.out.print(x + 1);
else if (x > 1) System.out.print(x);
if (x > 2) System.out.print(x - 1);
else if (x > 3) System.out.print(2 * x);
else System.out.print(x * x);
}
}
lh102sun.cs.indiana.edu%javac Four.java
lh102sun.cs.indiana.edu%java Four
false
true
false
true
false
true
true
0
21
34
42
53
lh102sun.cs.indiana.edu%
class Five {
public static void main(String[] args) {
for (int i = 0; i < 10; i++) {
System.out.println(Five.fun(i));
}
}
static int fun(int x) {
if (x % 2 == 0) return (x + 1);
else if (x % 3 == 1) return (x);
if (x % 5 == 2) return (x - 1);
else if (x > 3) return (x);
else return (2 * x);
}
}
lh102sun.cs.indiana.edu%javac Five.java
lh102sun.cs.indiana.edu%java Five
1
1
3
6
5
5
7
7
9
9
lh102sun.cs.indiana.edu%