Difference between revisions of "Chapter:Programming in Java - Exercises"

From Juneday education
Jump to: navigation, search
(Now, with exercises, solutions and links)
(No difference)

Revision as of 18:37, 7 May 2017

Exercises for the Programming in Java chapter

The layout of a Java program

  1. Write the Small class yourself using an editor (e.g Atom).
    1 public class Small {
    2 
    3   public static void main(String[]args) {
    4           
    5   }
    6 
    7 }
    

Expand using link to the right to see a hint.

Hopefully, you entered the text as shown in the question (without line numbers).

  1. Given that your class is called Small, what must be the name of the source code file?

Expand using link to the right to see a hint.

Since the class is called Small, we must name the file Small.java . This is a text file containing code in the Java programming language. Since the language is java we use the suffix java . A Java source code file with a so called "public class" must be named the same as the "public class" - in this case Small - and have the file suffix .java

  • Compile your Small class using javac
    • What is the name of the compiler program?
    • What is the argument given to the compiler?

Expand using link to the right to see a hint.

We compile the file like this:

$ javac Small.java

The name of the compiler is javac

The argument is the file we want the compiler to compile, namely Small.java

  • Execute your Small class using java
    • What is the name of the program executing your Java class?
    • What is the argument given to that program?

Expand using link to the right to see a hint.

We execute the file like this:

$ java Small

The name of the program is java

The argument is the class we want java to execute, namely Small .

Note that we do not give Small.class as argument. We give Small (the class name) as argument. Given that class name, Java knows how to find the file containing that class. Of course this means you have to be in the same directory because that's where Java is looking for the Small.class file.

  • Create a directory tmp. Enter the directory (tmp) and try to run the program again.
    • Note: This will not work!

Expand using link to the right to see a hint.

This will not work since, with the syntax used before, java can not find the class (see above). There are ways to make this work, but we will look into that another day. But if you enter the tmp/<code> directory, which has no files in it, and issue the same command as above, <code>$ java Small - as we explained above, Java will look for a file called Small.class and try to read it and execute the byte code instructions in it. But since there are no files in this directory, how could Java possibly find the Small.class file? You have given Java no hints on where to look!

Where does the execution start, in a Java program?

Expand using link to the right to see a hint.

Execution always start in the "main method" (which is currently empty).

The program "Small", what does it do?

Expand using link to the right to see a hint.

The program doesn't do anything, because the body (contents between { and } ) of the main method is empty. Where the execution starts, there are no instructions!

Inside the so called "main method" (from now on, simply "the main method") add exactly the following statement (a statement is another word for "instruction"): System.out.println("I am a println statement"); (using your editor, and don't forget to save the file after the change!)

  • Compile the Small.java file again
  • Run the Small class again
  • What does the program now do?

Expand using link to the right to see a hint.

The source code file Small.java should now look something like this:

1 public class Small {
2 
3   public static void main(String[]args) {
4     System.out.println("I am a println statement");
5   }
6 
7 }

The amount of white space (spaces) is not relevant, but it sure makes the program simpler to read, doesn't it?

We compile the file like this (as usual):

$ javac Small.java

We run the program like this (as usual):

$ java Small

The program, when run, will actually do something now! It will print I am a println statement in your terminal.

After the newly added statement to the main, on the next line, add the following statement:

System.out.println("This is an additional println statement!");

  • Compile the Small.java file again
  • Run the "Small" class again
  • What does the program now do?
  • In what order are the statements executed when you run the program?
  • Compare the order of the output lines, with the order of the statements in your code. Do they match?

Expand using link to the right to see a hint.

The program will now look something like this:

1 public class Small {
2 
3   public static void main(String[]args) {
4     System.out.println("I am a println statement");
5     System.out.println("This is an additional println statement!");
6   }
7 
8 }

Again, you compile the program like this:

$ javac Small.java

Again, you run the program like this:

$ java Small

The program now outputs the following:

I am a println statement
This is an additional println statement!

The order of the output is the same as the order of the corresponding println statements. From this, we learn that statements are executed sequentially (in the same order as they are written in the source code, one by one).

Links

Where to go next

Previous page (Programming in Java) | Next page (Our first Java program)