-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathp_difficult_object_arrays.java
More file actions
83 lines (72 loc) · 1.79 KB
/
p_difficult_object_arrays.java
File metadata and controls
83 lines (72 loc) · 1.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
// This test case ask for interpreting array access.
class p_array {
public static void main(String [] args) {
if ((new Sorter().bubblesortDemo()) == true) {
System.out.println("Sort successful");
} else {
System.out.println("Sort failed");
}
}
}
class Sorter {
public boolean bubblesortDemo() {
Integer [] array;
Integer a;
Integer b;
Integer c;
Integer x;
Integer y;
Integer tmp;
int i;
int j;
int ret;
boolean isSorted;
array = new Integer[3];
a = new Integer();
b = new Integer();
c = new Integer();
ret = a.create(0);
ret = b.create(20);
ret = c.create(10);
array[0] = a;
array[1] = b;
array[2] = c;
i = 0;
j = 0;
while (i < 3) {
j = 0;
while (j < 3-i-1) {
x = array[j];
y = array[j + 1];
if (x.getInt() > y.getInt()) {
// swap array[j] and array[j+1]
tmp = y;
array[j+1] = x;
array[j] = tmp;
} else {}
j = j + 1;
}
i = i+1;
}
// see if the array is already sorted.
i = 0;
isSorted = true;
while (i < 3) {
x = array[i];
isSorted = isSorted && (x.getInt() == i*10);
i = i + 1;
System.out.println(x.getInt());
}
return isSorted;
}
}
class Integer {
int i;
public int create(int x){
i = x;
return 0;
}
public int getInt(){
return i;
}
}