Institute of Computer Science
  1. Courses
  2. 2018/19 spring
  3. Object-Oriented Programming (Narva College) (LTAT.NR.003)
ET
Log in

Object-Oriented Programming (Narva College) 2018/19 spring

  • Home
  • Materials
  • Java Glossary
  • Source Example
  • Cheat sheet (S1-S6)
  • Grading
  • Links

During session 1

slides_s1.pdf

In this session, we are going to use Notepad and Terminal for practising compiling.

1. Compile and run the program in the command line

When you start writing a piece of code in Java in any editor (e.g. Notepad, Gedit, etc.), the program looks like a simple text file. Unlike Python, the program cannot be executed at once. In Java, the program has to be written, complied and after that executed.

1. Create a folder for your Java files (e.g., "c:\myProject", or "d:\myProject").

Do NOT save your files on Desktop or Documents as they are hard to locate. The directory name should not contain spaces or special characters.

2. Launch a programming text editor (e.g. NotePad, or NotePad++, or TextPad - DO NOT use MS Word) and enter the source code:

 /*
  * First Java program to say Hello
  */
 public class HelloWorld {   // Save as "HelloWorld.java" into "d:\myProject"
    public static void main(String[] args) {
      System.out.println("Hello, world!");
    }
 }

3. Save the file as HelloWorld.java into your folder (e.g. d:\myProject).

4. Start a cmd: click the "Start" button ⇒ "run..." ⇒ Enter "cmd").

5. Set the Current Drive to the drive where you have saved your source file HelloWorld.java. (e.g. if the source file is saved in drive d, enter d: as follow:

 C:> d:
 D:>

6. Set the Current Working Directory to the directory with the source file via the cd (Change Directory) command. (e.g. if the source file is saved into d:\myProject:

 D:> cd \myProject
 D:\myProject>

7. Issue a dir (List Directory) command to confirm that the source file is present in the current directory:

 D:\myProject> dir
 ......
 xx-xxx-xx  06:25 PM               277 HelloWorld.java
 ......

8. Invoke the JDK compiler javac to compile the source code HelloWorld.java.

D:\myProject> javac HelloWorld.java

The compilation is successful if the command prompt returns. Otherwise, error messages would be shown.

NB! If you have never compiled any java file on your computer, most probably the computer will throw out an error message like The name specified is not recognized as an internal or external command ... or Bad command or file name. This means that java cannot find the compiler. In such case show the path to the compiler , e.g.:
D:\myProject>path C:\Program Files\Java\jdk-9.0.4\bin
* If the previous remark has not helped you resolve the error, check Common JDK Installation Errors.

9. The output of the compilation is a Java class called HelloWorld.class. Issue a dir command again to check for the output.

 D:\myProject> dir
 ......
 xx-xxx-xx  01:53 PM               416 HelloWorld.class
 xx-xxx-xx  06:25 PM               277 HelloWorld.java
......

10. To run the program, invoke the Java Runtime java:

 D:\myProject> java HelloWorld
 Hello, world!

Conclusion: Computers do not directly understand Java. We need a compiler between the source code and the computer. When we are programming using the command line interface, the command javac HelloWorld.java will compile the HelloWorld.java file into bytecode, which can be executed using the Java interpreter. To run the compiled program, you can use the command java HelloWorld where HelloWorld is the name of the original source code file.

2. Kitty

Write a program that prints a kitty speaking a greeting, similar to (but different from) the following:

 /\_/\     -----
( ‘ ’ )  / Hello, \
(  -  ) <  Junior  |
 | | |   \ Coder! / 
 (_|_)     -----

Be careful with back-slashes \.

3. A random number

Study the following example:

 double randomDouble = Math.random()*5+15;
 long roundedRandomLong = Math.round(Math.random()*5+15);
 int invalidInt = (int)Math.random()*5+15;

What is the output of the statements? What data type are the outputs?

Write a program that generates two random integers in the range of 21 and 90 (not inclusive), and prints out their fraction accurate to third decimal place.

After this session, we are going to use IntelliJ - a modern development environment which takes care of compiling the source code. When we choose to run the program, the development environment compiles and executes the program. Generally, all development environments compile source code while it is being written by the programmer, which means that simple errors will be noticed before executing the program.

  • Institute of Computer Science
  • Faculty of Science and Technology
  • University of Tartu
In case of technical problems or questions write to:

Contact the course organizers with the organizational and course content questions.
The proprietary copyrights of educational materials belong to the University of Tartu. The use of educational materials is permitted for the purposes and under the conditions provided for in the copyright law for the free use of a work. When using educational materials, the user is obligated to give credit to the author of the educational materials.
The use of educational materials for other purposes is allowed only with the prior written consent of the University of Tartu.
Terms of use for the Courses environment