Homework 4
This commit is contained in:
parent
0afc86ceeb
commit
72cc3206c4
125 changed files with 4200 additions and 1636 deletions
21
javali_tests/HW4_nop90/Casts/ErrCastUnrelatedType.javali
Executable file
21
javali_tests/HW4_nop90/Casts/ErrCastUnrelatedType.javali
Executable file
|
@ -0,0 +1,21 @@
|
|||
// Test types in a Cast (cannot cast to unrelated type)
|
||||
|
||||
class Main {
|
||||
void main() {
|
||||
C1 a, b;
|
||||
C2 c, d;
|
||||
Object o;
|
||||
c = new C2();
|
||||
a = new C1();
|
||||
|
||||
b = (C1) c;
|
||||
d = (C2) a;
|
||||
o = (Object) a;
|
||||
o = (Object) c;
|
||||
o = (Main) c;
|
||||
}
|
||||
}
|
||||
|
||||
class C1 {}
|
||||
|
||||
class C2 extends C1{}
|
14
javali_tests/HW4_nop90/Casts/ErrObjectToArrayCast.javali
Executable file
14
javali_tests/HW4_nop90/Casts/ErrObjectToArrayCast.javali
Executable file
|
@ -0,0 +1,14 @@
|
|||
// Test downcast from Object to array
|
||||
|
||||
class Main {
|
||||
void main() {
|
||||
A[] a;
|
||||
Object o;
|
||||
|
||||
o = new Object();
|
||||
|
||||
a = (A[]) o;
|
||||
}
|
||||
}
|
||||
|
||||
class A{}
|
12
javali_tests/HW4_nop90/Casts/ErrPrimitiveCast.javali
Executable file
12
javali_tests/HW4_nop90/Casts/ErrPrimitiveCast.javali
Executable file
|
@ -0,0 +1,12 @@
|
|||
// Test types in a Cast
|
||||
// cannot cast an int to an boolean
|
||||
|
||||
class Main {
|
||||
void main() {
|
||||
int a;
|
||||
boolean b;
|
||||
a = 1;
|
||||
|
||||
b = (boolean) a;
|
||||
}
|
||||
}
|
11
javali_tests/HW4_nop90/Casts/ErrPrimitiveCast2.javali
Executable file
11
javali_tests/HW4_nop90/Casts/ErrPrimitiveCast2.javali
Executable file
|
@ -0,0 +1,11 @@
|
|||
// Test types in a Cast (cannot cast to unrelated type)
|
||||
|
||||
class Main {
|
||||
void main() {
|
||||
int a;
|
||||
boolean b;
|
||||
//b = false;
|
||||
|
||||
a = (int) b;
|
||||
}
|
||||
}
|
12
javali_tests/HW4_nop90/Casts/OkArrayToObjectCast.javali
Executable file
12
javali_tests/HW4_nop90/Casts/OkArrayToObjectCast.javali
Executable file
|
@ -0,0 +1,12 @@
|
|||
// Test downcast from array to Object
|
||||
|
||||
class Main {
|
||||
void main() {
|
||||
int[] x;
|
||||
Object o;
|
||||
|
||||
x = new int[5];
|
||||
|
||||
o = (Object) x;
|
||||
}
|
||||
}
|
14
javali_tests/HW4_nop90/Casts/OkArrayToObjectCast2.javali
Executable file
14
javali_tests/HW4_nop90/Casts/OkArrayToObjectCast2.javali
Executable file
|
@ -0,0 +1,14 @@
|
|||
// Test downcast from array to Object
|
||||
|
||||
class Main {
|
||||
void main() {
|
||||
A[] a;
|
||||
Object o;
|
||||
|
||||
a = new A[5];
|
||||
|
||||
o = (Object) a;
|
||||
}
|
||||
}
|
||||
|
||||
class A{}
|
15
javali_tests/HW4_nop90/Casts/OkSubtype.javali
Executable file
15
javali_tests/HW4_nop90/Casts/OkSubtype.javali
Executable file
|
@ -0,0 +1,15 @@
|
|||
// Test cast from a type to the same type
|
||||
|
||||
class Main {
|
||||
void main() {
|
||||
C c,d;
|
||||
Object o,g;
|
||||
c = new C();
|
||||
g = new Object();
|
||||
|
||||
d = (C) c;
|
||||
o = (Object) g;
|
||||
}
|
||||
}
|
||||
|
||||
class C {}
|
26
javali_tests/HW4_nop90/Casts/OkTypeCast.javali
Executable file
26
javali_tests/HW4_nop90/Casts/OkTypeCast.javali
Executable file
|
@ -0,0 +1,26 @@
|
|||
// Test valid casts
|
||||
|
||||
class Main {
|
||||
void main() {
|
||||
C1 a, x;
|
||||
C2 c,d;
|
||||
C4 b,f;
|
||||
Object o;
|
||||
c = new C2();
|
||||
a = new C1();
|
||||
b = new C4();
|
||||
f = new C4();
|
||||
|
||||
x = (C1) b;
|
||||
o = (Object) f;
|
||||
}
|
||||
}
|
||||
|
||||
class C1 {}
|
||||
|
||||
class C2 extends C1{}
|
||||
|
||||
class C3 extends C2{}
|
||||
|
||||
class C4 extends C3{}
|
||||
|
23
javali_tests/HW4_nop90/Casts/OkTypeToObjectCast.javali
Executable file
23
javali_tests/HW4_nop90/Casts/OkTypeToObjectCast.javali
Executable file
|
@ -0,0 +1,23 @@
|
|||
// Test casts from type to object
|
||||
|
||||
class Main {
|
||||
void main() {
|
||||
C1 a;
|
||||
Object o,f,g;
|
||||
int x;
|
||||
boolean y;
|
||||
a = new C1();
|
||||
x = 2;
|
||||
y = true;
|
||||
|
||||
|
||||
o = (Object) a;
|
||||
//f = (Object) x;
|
||||
//g = (Object) y;
|
||||
}
|
||||
}
|
||||
|
||||
class C1 {}
|
||||
|
||||
|
||||
|
Loading…
Add table
Add a link
Reference in a new issue