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

From Juneday education
Jump to: navigation, search
(added a question)
(Where to go next: next page text)
 
(One intermediate revision by the same user not shown)
Line 261: Line 261:
 
=Links=
 
=Links=
 
==Where to go next==
 
==Where to go next==
 +
Next page is: [[Chapter:Our first Java program|Our first Java program]]
 +
 +
{{Nav links|prev=Chapter:Programming in Java|TOC=Programming_with_Java#Chapters|next=Chapter:Our first Java program}}
 +
 +
<!--
 
[[Chapter:Programming in Java | Previous page (Programming in Java)]] | [[Chapter:Our first Java program|Next page (Our first Java program)]]
 
[[Chapter:Programming in Java | Previous page (Programming in Java)]] | [[Chapter:Our first Java program|Next page (Our first Java program)]]
 +
-->

Latest revision as of 12:25, 21 September 2018

Exercises for the Programming in Java chapter

The layout of a Java program

Write your first "Java class"

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).

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 first Java class

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 first Java program

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.

Make Java not know what to do

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/ directory, which has no files in it, and issue the same command as above, $ 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!

About the execution of your program

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!

Make your program actually do something

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.

Make your program do even more!

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).

Making use of classes and objects

Create a new Java source code file called TestTalker.java .

Add a main method like the one your previously wrote and put the following instructions inside the main:

Talker myTalker = new Talker();
myTalker.talk();

Compile TestTalker.java . The compiler will complain about never having heard about the Talker "class". That's expected. Let's make the compiler shut up about this, by actually writing the Talker class.

Expand using link to the right to see a hint.

Your program source code should now look like this:

public class TestTalker {
  public static void main(String[] args) {
    Talker myTalker = new Talker();
    myTalker.talk();
  }
}

Enter the following in a Java source code file called Talker.java:

public class Talker {
  public void talk() {
    System.out.println("I can talk.");
    System.out.println("This is me, talking.");
  }
}

Compile the TestTalker.java file again. Did the compiler now complain again about not finding the Talker class? It shouldn't if you have entered the source code for the Talker.java file exactly as above, and saved both files in the same directory.

Use ls to list the files in the current directory. Did the compilation of TestTalker.java produce a TestTalker.class file? It should have. But did it also create a Talker.class file out of the Talker.java file? It actually should have done that too. It did find the Talker.java class this time, but inside the Java source code file Talker.java . For this reason, the compiler also compiled that file. Why? Because your program in TestTalker.java made use of the Talker class. For this to work, when the program is run, there must be a compiled version of this class available for Java to read and use.

Now run the TestTalker program like this:

$ java TestTalker

Can you see any output?

What you did in this small extra exercise, was the following:

  • You created a program which used another class, Talker
  • Your program actually created an object of the type Talker (the Talker class is used as the type of the myTalker variable)
  • Your program used the myTalker variable to send a message to the Talker object just created
  • Your program had the effect that the Talker object, you referred to as myTalker ran the talk() "method"
  • The talk() method executed the print statements

A method is a named place with code. You declared a method talk() in the Talker class. Now all objects of type Talker know how to "talk()". If you need a new Talker object, you write:

Talker someVariable = new Talker();

Now you can use the someVariable "reference" for sending messages to the Talker object you created. A message must have a corresponding "method" in the code for the class of the object, in your case, the Talker class.

What goes on behind the scenes could be described as this:

When the program was run, the Java Virtual Machine (JVM) saw the instruction in the main method to create a new Talker object and save the reference to it in a variable. In order to do this, the JVM read the Talker.class file into the memory, in order to know what Talker objects can do. Next, the main method had an instruction for sending the talk() message to the Talker object via the reference. The JVM did now send this message by calling the talk() method for the object. This meant investigating the instructions in the talk() method in the Talker class and they were simply two statements for outputting some text. So that is what the JVM did next. After that, there were no more instructions in the main method, so the program terminated.

  1. Run main() for the TestTalker class
  2. Create a Talker object and store reference in myTalker
    1. Read the Talker class from the file system and look what the Talker object can do
    2. Create the object and store the reference in myTalker
  3. Send the message talk() to the object
    1. Call the talk() method
      1. Print a message
      2. Print another message
  4. No more instructions in main
  5. Terminate program

Some history

  • Find out what year the first Java version was released
  • What other versions were released, and when?
  • What version are you running on your computer?

Expand using link to the right to see a hint.

Release Year
JDK Beta 1994
JDK 1.0 1996
JDK 1.1 1997
J2SE 1.2 1998
J2SE 1.3 2000
J2SE 1.4 2002
J2SE 5.0 2005
Java SE 6 2006
Java SE 7 2011
Java SE 8 2014
Source: Wikipedia

JDK 1.0 (January 23, 1996) was the first Java version.

To find out what Java version you are running, you can do the following:

$ java -version
openjdk version "1.8.0_131"
OpenJDK Runtime Environment (build 1.8.0_131-8u131-b11-0ubuntu1.16.04.2-b11)
OpenJDK 64-Bit Server VM (build 25.131-b11, mixed mode)
$
$
$ javac -version
javac 1.8.0_131

Links

Where to go next

Next page is: Our first Java program

« PreviousBook TOCNext »