You are on page 1of 2

Programming I - Lab1

Clara Benac Earle, ETSIINF, UPM


September 15, 2020

1 First program - “hello world”


Start DrJava (or similar) and write the following Java program.
public class Lab1 {

public static void main ( String [] args ) {

System . out . println ( ‘ ‘ Hola Mundo ’ ’);


}
}

Save the code and check that a file called Lab1.java has been created in your
computer. Compile and check that a file Lab.class has been created in your
compiter. Finally, execute the code and see the result on the screen.

2 Compiler and runtime errors


Try the following one by one, and observe what happens.

1. Remove one of the open curly brackets ({). Try to compile.


2. Remove one of the closing curly brackets (}). Try to compile.
3. Write mian instead of main. Try to compile. Try to execute.

4. Remove the word static. Try to compile. Try to execute.


5. Remove the word public. Try to compile.
6. Remove the word System. Try to compile.

7. Replace println with pintln. Try to compile.


8. Remove one of the parenthesis. Try to compile.
9. Add one parentesis extra. Try to compile.

1
3 Second program - print a triangle
Create a new class called Figures and, in the main, write the following code.

System.out.println(‘‘triangle’’);
System.out.println(‘‘*’’);
System.out.println(‘‘***’’);
System.out.println(‘‘*****’’);

Observe what happens when you execute the code. Write code for printing
a square, and a rectangle. Try to write code that prints other figures. Submit
Figures.java to Moodle.

You might also like