Homework 3
This commit is contained in:
parent
bf60a078d7
commit
0afc86ceeb
95 changed files with 3163 additions and 4316 deletions
9
javali_tests/HW3/ErrCallBase.javali
Normal file
9
javali_tests/HW3/ErrCallBase.javali
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
// Test that you cannot invoke a method on an integer.
|
||||
|
||||
class Main {
|
||||
void main() {
|
||||
int x;
|
||||
x = 1;
|
||||
x.foo();
|
||||
}
|
||||
}
|
||||
9
javali_tests/HW3/ErrCircularInheritance.javali
Normal file
9
javali_tests/HW3/ErrCircularInheritance.javali
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
/* Test that the circular inheritance between Foo and Bar is detected */
|
||||
class Foo extends Bar { }
|
||||
class Bar extends Foo { }
|
||||
|
||||
class Main {
|
||||
void main() {
|
||||
writeln();
|
||||
}
|
||||
}
|
||||
11
javali_tests/HW3/ErrIfCondition.javali
Normal file
11
javali_tests/HW3/ErrIfCondition.javali
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
/* Test that if conditions must be boolean */
|
||||
class Main {
|
||||
void main() {
|
||||
if (84 / 2) {
|
||||
write(1);
|
||||
writeln();
|
||||
}
|
||||
write(2);
|
||||
writeln();
|
||||
}
|
||||
}
|
||||
14
javali_tests/HW3/ErrUnknownField1.javali
Normal file
14
javali_tests/HW3/ErrUnknownField1.javali
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
/* Test that an invalid field name is detected */
|
||||
|
||||
class X {
|
||||
int field;
|
||||
}
|
||||
|
||||
class Main {
|
||||
void main() {
|
||||
X x;
|
||||
x = new X();
|
||||
x.field = 0;
|
||||
x.notafield = 1; /* ILLEGAL: bad field name */
|
||||
}
|
||||
}
|
||||
30
javali_tests/HW3/OkInheritanceFields.javali
Normal file
30
javali_tests/HW3/OkInheritanceFields.javali
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
/* Test that fields are inherited */
|
||||
|
||||
class A {
|
||||
int foo;
|
||||
}
|
||||
|
||||
class B extends A {
|
||||
int bar;
|
||||
}
|
||||
|
||||
class Main {
|
||||
void main() {
|
||||
A a;
|
||||
B b;
|
||||
|
||||
a = new A();
|
||||
a.foo = 1;
|
||||
write(a.foo);
|
||||
|
||||
a = new B();
|
||||
a.foo = 2;
|
||||
write(a.foo);
|
||||
|
||||
b = new B();
|
||||
b.foo = 3;
|
||||
b.bar = 4;
|
||||
write(b.foo);
|
||||
write(b.bar);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,16 @@
|
|||
// Test that arrays are not covariant
|
||||
|
||||
class Main {
|
||||
void main() {
|
||||
boolean y;
|
||||
A[] Array1;
|
||||
B[] Array2;
|
||||
Array2 = new B[10];
|
||||
|
||||
Array1 = Array2;
|
||||
}
|
||||
}
|
||||
|
||||
class B extends A {}
|
||||
|
||||
class A {}
|
||||
|
|
@ -0,0 +1,11 @@
|
|||
// The left-hand side of an assignment cannot be a method call
|
||||
// Valid options are variable accesses, fields or an indexed array
|
||||
|
||||
class Main {
|
||||
void main() {
|
||||
action() = read();
|
||||
}
|
||||
int action() {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,15 @@
|
|||
// The left-hand side of an assignment cannot be a reference to this
|
||||
// Valid options are variable accesses, fields or an indexed array
|
||||
|
||||
class Main {
|
||||
void main () {
|
||||
int b;
|
||||
int[] c;
|
||||
c = new int[5];
|
||||
this.a = 10;
|
||||
b = 10;
|
||||
c[4] = 10;
|
||||
this = null;
|
||||
}
|
||||
int a;
|
||||
}
|
||||
14
javali_tests/HW3_nop90/Assignments/OkArraySubtypes.javali
Normal file
14
javali_tests/HW3_nop90/Assignments/OkArraySubtypes.javali
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
// any array is a subtype of Object
|
||||
// assignment and 'is equal' with an object of type 'Object' is fine
|
||||
|
||||
class Main {
|
||||
void main() {
|
||||
boolean y;
|
||||
Object x;
|
||||
int[] testArray;
|
||||
testArray = new int[10];
|
||||
x = testArray;
|
||||
testArray = null;
|
||||
x = null;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,8 @@
|
|||
// Multiple classes can't share the same name
|
||||
|
||||
class Aclass {}
|
||||
class Aclass {}
|
||||
|
||||
class Main {
|
||||
void main() {}
|
||||
}
|
||||
|
|
@ -0,0 +1,8 @@
|
|||
// Two fields in a class cannot share the same name
|
||||
|
||||
class Main {
|
||||
void main() {}
|
||||
int a;
|
||||
Object b;
|
||||
Main a;
|
||||
}
|
||||
|
|
@ -0,0 +1,8 @@
|
|||
// Two local variables in a method cannot share the same name
|
||||
|
||||
class Main {
|
||||
void main() {}
|
||||
void test() {
|
||||
int a, a;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,10 @@
|
|||
// test double declaration
|
||||
// test local variable and parameter have same name
|
||||
|
||||
class Main {
|
||||
void main() {}
|
||||
int action(int p1, boolean p2) {
|
||||
Object p1;
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
// No pair of parameters in a method declaration can have the same name
|
||||
|
||||
class Main {
|
||||
void main() {}
|
||||
int action(int p1, boolean p2, Object p, Object p) {}
|
||||
}
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
// No class can have the name "Object"
|
||||
|
||||
class Aclass {}
|
||||
class Object extends Aclass {}
|
||||
class Main extends Object {
|
||||
void main() {}
|
||||
}
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
// Error: the return type of the "main" method must be "void"
|
||||
|
||||
class Main {
|
||||
int main() {
|
||||
return 10;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
// Error: the main method must have no arguments
|
||||
|
||||
class Main {
|
||||
void main(Object[] args) {}
|
||||
}
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
// Error: there is no class called Main (case-sensitive)
|
||||
|
||||
class MAIN {}
|
||||
class main extends Object {}
|
||||
|
|
@ -0,0 +1,11 @@
|
|||
// Error: the "Main" class does not contain a method called "main"
|
||||
|
||||
class Main {
|
||||
void maini() {}
|
||||
void mein() {}
|
||||
void moon() {}
|
||||
}
|
||||
|
||||
class NotMain extends Main {
|
||||
void main() {}
|
||||
}
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
// The main method can be inherited from another class
|
||||
|
||||
class OtherMain {
|
||||
void main () {}
|
||||
}
|
||||
|
||||
class Main extends OtherMain {}
|
||||
Some files were not shown because too many files have changed in this diff Show more
Loading…
Reference in a new issue