Homework 4

This commit is contained in:
Carlos Galindo 2020-01-15 22:34:57 +01:00
parent 0afc86ceeb
commit 72cc3206c4
Signed by: kauron
GPG key ID: 83E68706DEE119A3
125 changed files with 4200 additions and 1636 deletions

View file

@ -0,0 +1,15 @@
/* Test illegal downcasts */
class A { }
class B extends A { }
class Main {
void main() {
A a;
B b;
a = new A();
b = (B) a; /* Should fail at runtime */
write(0);
writeln();
}
}

View file

@ -0,0 +1,23 @@
class Main {
void main() {
int a;
a = fib(20);
write(a);
writeln();
}
int fib(int n) {
int fib;
int fib2;
if (n <= 1) {
fib = n;
} else {
fib = fib(n-1);
fib2 = fib(n-2);
fib = fib + fib2;
}
return fib;
}
}

View file

@ -0,0 +1,14 @@
/* Test expressions using array elements as operands */
class Main {
int[] x;
void main() {
int i;
x = new int[3];
x[0] = 3;
x[1] = 4;
x[2] = 5;
i = x[0] + x[1] + x[2];
write(i);
writeln();
}
}

View file

@ -0,0 +1,20 @@
/* Test access to parameters and fields */
class A {
int i;
void foo(int p) {
write(p);
write(i);
writeln();
}
}
class Main {
void main() {
A a;
a = new A();
a.i = 10;
a.foo(1);
a.foo(2);
a.foo(3);
}
}

View file

@ -0,0 +1,28 @@
/* Test read() with different kinds of LHS values */
class Main {
int x;
void main() {
int y;
int[] arr;
write(1);
writeln();
y = read();
write(y + 1);
writeln();
x = read();
write(x + 1);
writeln();
arr = new int[64];
arr[x] = read();
write(arr[x] + 1);
writeln();
}
}

View file

@ -0,0 +1,4 @@
9
51
12

View file

@ -0,0 +1,14 @@
/* Test legal downcasts */
class A { }
class B extends A { }
class Main {
void main() {
A a;
B b;
a = new B();
b = (B) a; /* OK at runtime */
write(0);
}
}

View file

@ -0,0 +1,29 @@
/* Test access to fields from elements of arrays */
class A {
int field;
void foo() {
write(1);
write(field);
writeln();
}
}
class Main {
A[] x;
void main() {
int i;
x = new A[2];
i = 1;
write(i);
writeln();
x[i] = new A();
x[i].field = i + 1;
i = x[1].field;
write(i);
writeln();
x[1].foo();
}
}

View file

@ -0,0 +1,50 @@
/* Test virtual method calls */
class A {
void override() {
write(0);
writeln();
}
void base() {
write(1);
writeln();
}
}
class B extends A {
void override() {
write(2);
writeln();
}
void sub() {
write(3);
writeln();
}
}
class Main {
void main() {
A a;
B b;
a = null;
b = null;
a = new A();
a.base();
a.override();
b = new B();
b.base();
b.override();
b.sub();
a = b;
a.base();
a.override();
b.base();
b.override();
b.sub();
}
}

View file

@ -0,0 +1,81 @@
/* Overall test of arrays, loops, etc. that does a simple quicksort */
class Record {
int a;
void print() {
write(a);
writeln();
}
}
class Main {
Record [] a;
int i;
void swap(Record r1, Record r2) {
int temp;
temp = r1.a;
r1.a = r2.a;
r2.a = temp;
}
void sort(int left, int right) {
int i,j;
int m;
m = (a[left].a + a[right].a) / 2;
i = left;
j = right;
while (i <= j) {
while (a[i].a < m) { i = i+1; }
while (a[j].a > m) { j = j-1; }
if (i <= j) {
swap(a[i], a[j]);
i = i + 1;
j = j - 1;
}
}
if (left < j) { sort(left, j); }
if (i < right) { sort(i, right); }
}
void main() {
int SIZE;
int j;
SIZE = 5;
a = new Record[SIZE];
j = 0;
while (j < SIZE) {
a[j] = new Record();
j = j + 1;
}
a[0].a = 5;
a[1].a = 3;
a[2].a = 1;
a[3].a = 4;
a[4].a = 2;
/* Numbers before sorting */
j = 0;
while (j < SIZE) {
a[j].print();
j = j + 1;
}
writeln();
sort(0, 4);
/* Numbers after sorting */
j = 0;
while (j < SIZE) {
a[j].print();
j = j + 1;
}
writeln();
}
}