Homework 3

This commit is contained in:
Carlos Galindo 2020-01-15 22:32:25 +01:00
commit 0afc86ceeb
Signed by: kauron
GPG key ID: 83E68706DEE119A3
95 changed files with 3163 additions and 4316 deletions

View 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();
}
}

View 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();
}
}

View 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();
}
}

View 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 */
}
}

View 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);
}
}

View file

@ -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 {}

View file

@ -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;
}
}

View file

@ -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;
}

View 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;
}
}

View file

@ -0,0 +1,8 @@
// Multiple classes can't share the same name
class Aclass {}
class Aclass {}
class Main {
void main() {}
}

View file

@ -0,0 +1,8 @@
// Two fields in a class cannot share the same name
class Main {
void main() {}
int a;
Object b;
Main a;
}

View file

@ -0,0 +1,8 @@
// Two local variables in a method cannot share the same name
class Main {
void main() {}
void test() {
int a, a;
}
}

View file

@ -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;
}
}

View file

@ -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) {}
}

View file

@ -0,0 +1,7 @@
// No class can have the name "Object"
class Aclass {}
class Object extends Aclass {}
class Main extends Object {
void main() {}
}

View file

@ -0,0 +1,7 @@
// Error: the return type of the "main" method must be "void"
class Main {
int main() {
return 10;
}
}

View file

@ -0,0 +1,5 @@
// Error: the main method must have no arguments
class Main {
void main(Object[] args) {}
}

View file

@ -0,0 +1,4 @@
// Error: there is no class called Main (case-sensitive)
class MAIN {}
class main extends Object {}

View file

@ -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() {}
}

View file

@ -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