-
Notifications
You must be signed in to change notification settings - Fork 2
Syntax
int, boolean, and user defined classes are the permitted types in this language. A generic type can be specified for generic classes which will be substituted for the appropriate type when a class is created.
An expression can be an integer, a string, true, false, a variable, a binary expression, a new class, or a method call.
Binary expressions can use +, -, *, /, ==, and <.
They cannot use <=, >, >=, &&, or ||.
A class is created with the new keyword followed by the appropriate constructor for the class being created. Between the class name and the parameters is a list of types surrounded by < and >. These are used to specify the types if the class has generic types defined. The < and > are required even if the class does not specify any generic types.
new Foo<>();
new Foo<int, string>(var1, var2);
A statement can be an expression, return with or without a value, break, print, variable declaration, variable assignment, if, or while. All statements must end with a semicolon. To group multiple statements curly braces are used. No semicolon is needed after the curly braces when grouping statements.
A break statement terminates the loop it is found in. A B=break statement can only be used inside the code of a while statement.
The print statement takes in an expression and outputs it into the console.
A variable declaration has a type followed by a name and then assigned a value or an expression. Variables have to be assigned a value when they are created. They can be reassigned later by stating the variable name and assigning it to a new value. A variable cannot be void type nor can it be assigned a void type.
int num = 5;
num = 6;An If statement starts with the if keyword followed by an expression enclosed in parentheses. Afterwards is the code of the if statement. If the code contains multiple statements it is enclosed in curly braces. After the code an else is required. This is followed by the else statements code. Again, if the code contains multiple statements it is enclosed in curly braces.
if (true == true)
...
else
...OR
if (true == true) {
...
} else {
...
}An While statement starts with the while keyword followed by an expression enclosed in parentheses. Afterwards is the code of the while statement. If the code contains multiple statements it is enclosed in curly braces.
while (true == false)
...OR
while (true == false) {
....
}Classes start with the class keyword followed by the class' name. Afterwards if a class will be a child class the keyword extends followed by the name of the inherited class. Following this, the class' code is enclosed in curly braces.
Non-inherited class
class Foo<> {
...
}Inherited class
class Bar<> extends Foo<> {
...
}Classes can have generic types which represent placeholder variables for the class. To specify that a class is generic a generic type variable is place between the <> in the class declaration. Generic type variables are typically one capital letter. These generic variables are used in the body of the class definition as placeholders for another type.
Non-inherited generic class
class List<A> {
...
}Inherited generic class
class Bar<> extends Foo<A> {
...
}Inherited generic class
class Bar<> extends Foo<string> {
...
}Inherited generic class
class Bar<A> extends Foo<A> {
...
}Inherited generic class
class Bar<A> extends Foo<string> {
...
}Classes can contain a combination of variable declarations, method declarations, and are required to have only one constructor.
A class' constructor starts with the constructor keyword followed by variable declarations which are enclosed in parentheses. Following this the constructor's code is enclosed in curly braces.
constructor(int num, boolean val) {
...
}If the class is extending another class super must be called as the first statement in the constructor. The parameters of super must match with the parameters of the class' parent constructor.
constructor(int num, boolean val) {
super(num, val);
...
}A class' instance variables are variable declarations defined inside the class. Instance variables are automatically private and require methods to modify or retrieve values.
A class' method declarations start with an access modifier followed by the return type of the method. The return type of the method can be any permitted type as well as void. Next comes the method name followed by variable declarations enclosed in parentheses. Finally the method's code is enclosed in curly braces.
public int Foo(int Bar) {
...
return 5;
}A method must have a return. For a method with a non-void return type the return must return a value which is the same as the return type. For a method which returns void a blank return is used.
public void increment() {
sum = sum + 1;
return;
}