Never confuse education with intelligence, you can have a PhD and still be an idiot.
- Richard Feynman -



Difference between revisions of "Chapter:Our first Java program - Exercises"

From Juneday education
Jump to: navigation, search
(Links added)
 
(Exercises)
Line 1: Line 1:
 +
= Exercises - Our first Java program chapter=
 +
The file Simple.java:
 +
<syntaxhighlight lang="Java" line="1">
 +
import lib.Greeter;
  
 +
public class Simple {
  
 +
    public static void main(String[]args) {
 +
 +
        Greeter hello = new Greeter();
 +
        Greeter hi    = new Greeter("Hi, I am simple");
 +
 +
        hello.greet();
 +
        hi.greet();
 +
    }
 +
 +
}
 +
</syntaxhighlight>
 +
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.)
 +
<div class="mw-collapsible mw-collapsed">
 +
Expand using link to the right to see a hint.
 +
<div class="mw-collapsible-content">
 +
The symbol signifying "begin" is:
 +
<syntaxhighlight lang="Java">{</syntaxhighlight>
 +
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:
 +
<syntaxhighlight lang="Java">}</syntaxhighlight>
 +
It's called "right curly brace". It is said to end a block, in this case a block of code for the class definition.
 +
</div></div>
 +
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.)
 +
<div class="mw-collapsible mw-collapsed">
 +
Expand using link to the right to see a hint.
 +
<div class="mw-collapsible-content">
 +
The same symbols for start and end are used: <code>{</code> and <code>}</code> 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.
 +
</div></div>
 +
What names used in this small program starts with a capital letter?
 +
<div class="mw-collapsible mw-collapsed">
 +
Expand using link to the right to see a hint.
 +
<div class="mw-collapsible-content">
 +
These are the names beginning with a capital letter:
 +
* <syntaxhighlight lang="Java" inline="true">Greeter</syntaxhighlight>
 +
* <syntaxhighlight lang="Java" inline="true">Simple</syntaxhighlight>
 +
* <syntaxhighlight lang="Java" inline="true">String</syntaxhighlight>
 +
They are all names of [[Class|Java Classes]]. <code>String</code> is a Java class that comes with the Java development kit (our Java environment). The rest are classes that we have written for you. <code>Simple</code> is the class in which the small program above is defined. It, in turn, uses <code>Greeter</code> which is a class we have written for you and is actually located in the lib directory. <code>String</code> 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.
 +
</div></div>
 +
==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:
 +
<pre>
 +
.
 +
|-- lib
 +
|  `--Greeter.java
 +
`--Simple.java
 +
</pre>
 +
 +
Still in the directory where you have downloaded Simple.java (and also put the lib directory!), do the following to compile and run:
 +
<syntaxhighlight lang="bash">
 +
$ javac Simple.java
 +
$ java Simple
 +
</syntaxhighlight>
 +
==Now run it!==
 +
Follow the instructions above to set up the directories, download the source code, and, compile and run the Simple program.
 +
<div class="mw-collapsible mw-collapsed">
 +
Expand using link to the right to see a hint.
 +
<div class="mw-collapsible-content">
 +
See Videos for compiling and running [https://vimeo.com/173330126 (eng)] [https://vimeo.com/173329944 (swe)]
 +
</div></div>
 
=Links=
 
=Links=
 
==Where to go next==
 
==Where to go next==
 
[[Chapter:Our first Java program|Previous chapter (Our first Java program)]] | [[Chapter:Variables and types|Next chapter (Variables and types)]]
 
[[Chapter:Our first Java program|Previous chapter (Our first Java program)]] | [[Chapter:Variables and types|Next chapter (Variables and types)]]

Revision as of 20:28, 7 May 2017

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

Where to go next

Previous chapter (Our first Java program) | Next chapter (Variables and types)