Chapter:Our first Java program - Exercises

From Juneday education
Jump to: navigation, search

Exercises - Our first Java program chapter

The file Simple.java:

 1 import lib.Greeter;
 2 
 3 public class Simple {
 4 
 5     public static void main(String[]args) {
 6 
 7         Greeter hello = new Greeter();
 8         Greeter hi    = new Greeter("Hi, I am simple");
 9 
10         hello.greet();
11         hi.greet();
12     }
13 
14 }

This program defines a new Java "class" called Simple. What characters do you think symbolizes the start of the class, and the end of the class respectively? (You do not need to understand what a Java class is yet! This is just a reading exercise.)

Expand using link to the right to see a hint.

The symbol signifying "begin" is:

{

It's called "left curly brace". It is said to start (begin) a block, in this case a block of code for the class definition.

The symbol signifying "end" is:

}

It's called "right curly brace". It is said to end a block, in this case a block of code for the class definition.

The class Simple declares a "method" called main. What characters do you think symbolizes the start of the method, and the end of the method respectively? (You do not need to understand what a method is yet! This is just a reading exercise.)

Expand using link to the right to see a hint.

The same symbols for start and end are used: { and } respectively. This time they are said to start and end a block for the method main. The block is everything between the two curly braces.

What names used in this small program starts with a capital letter?

Expand using link to the right to see a hint.

These are the names beginning with a capital letter:

  • Greeter
  • Simple
  • String

They are all names of Java Classes. String is a Java class that comes with the Java development kit (our Java environment). The rest are classes that we have written for you. Simple is the class in which the small program above is defined. It, in turn, uses Greeter which is a class we have written for you and is actually located in the lib directory. String is used in the definition of the main method. It's significance and use will be discussed later on. It is not used in this simple program, but needs to be part of the main method declaration.

Instructions for setting up the program

Start by creating a directory for this chapter (you may call it what you want but "our-first-java-program" could be a decent name).

Enter that directory and create a directory called "lib" (for the lib package where the Greeter.java should be).

Download Simple.java to the "our-first-java-program" directory (or whatever you called the directory).

Download Greeter.java to the "lib" directory. You should now have the following structure:

.
|-- lib
|   `--Greeter.java
`--Simple.java

Still in the directory where you have downloaded Simple.java (and also put the lib directory!), do the following to compile and run:

$ javac Simple.java
$ java Simple

Now run it!

Follow the instructions above to set up the directories, download the source code, and, compile and run the Simple program.

Expand using link to the right to see a hint.

See Videos for compiling and running (eng) (swe)

Links

Further reading

Where to go next

Next page is: Variables and types

« PreviousBook TOCNext »