:: Enseignements :: ESIPE :: IMAC2 :: IMAC2 2020-2021 :: Object oriented programming in Java ::
![[LOGO]](http://igm.univ-mlv.fr/ens/resources/mlv.png) |
#1.en - First steps in Java, strings, arrays and loops
|
Exercice 1 - Hello Groland
In Java, each public class must be defined in its own file.
The name of the file must be the name of the class it contains,
to which we add the suffix .java .
Class names must consist of adjoining words whose first
letter is a capital letter (CamelCase style).
First we will write small programs to familiarize yourself with the compiler,
the virtual machine and the methods.
-
Write the following program:
public class HelloGroland {
public static void main(String[] args) {
System.out.println("Hello Groland");
}
}
in your favorite text editor (notepad?) and save it under the name HelloGroland.java
-
Compile the program using the command javac
then check that the corresponding .class file
exists.
javac HelloGroland.java
-
Run the program with the command java
java HelloGroland
We don't write ".class" because the virtual machine adds it
on its own.
Exercice 2 - Hello Groland in Eclipse
Launch the IDE eclipse, and create a project "Java IMAC2": (File - New - Java Project)
Verify that the JRE execution environment is Java 15, and that separate folders are used for sources and
class files (by default, src for source files, and bin for compiled class files, i.e. bytecode files),
then click "Finish" and chose NOT to create "module".
Select the whole source code of the previous exercise, and past it on the "src" folder in Eclipse. Save this file,
and then run it (Run - Run as - Java Application).
Modify this source file (replacing "Hello Groland" by "Hello Groland in Eclipse"), save and run.
Now with a terminal (console), find the directory (
bin) in the workspace of your project to run
the same program with the command
java
java HelloGroland
Note: when you're typing a class Toto (Java source code) in eclipse, it is compiled incrementally.
Saving this file with eclipse (in src/Toto.java) ensures that this class is compiled
(if no error avoid this compilation): then, the bytecode (result of compilation) is stored in a file
bin/Toto.class.
This class could be "run"
- either from eclispe through the menu/button "Run"
- or through a terminal by the command java Toto
By now, you are intended to create your own classes with eclipse, and you can run it with the most convenient way (run menu or java command).
Exercice 3 - Print the arguments on the command line
Write a class
PrintArgs (on the "src" folder in Eclipse: left-click - New - Class) that prints
all the arguments, one by line.
This class will be more convenient to run through the java command
(even if you can configure arguments through the eclipse menu "Run - Run configuration..."):
$ java PrintArgs these are args
these
are
args
Command line arguments are stored in the array of strings passed as an argument to the method
public static void main(String[] args).
-
First, display the first argument
from the command line (in our example these ).
What happens it you don't pass any argument during the
execution of the program?
-
Write a loop that prints the content of an array, knowing that in Java,
arrays have an attribut (lenght) for that purpose.
-
Change your program in order to user the 'foreach' syntax
for(Type value: array)
Exercice 4 - Simple Calculator
Write a program that read a number on the standard input and display it on the console.
For that purpose, use
Scanner
and its method
nextInt().
To understand this program, it is useful to look at the documentation
avalaible,
and to register the links in your bookmark.
-
Copy the program above and add some code in order
to print the number typed by the user.
-
Indicate in the program where the variables are
and what is their type.
Modify the program to declare and initiate the
variables in one line.
Since Java 10, variable could be declared with the keyword var instead of the real type of the variable...
when possible, the compiler will "infer" the correct type from the context:
var value1 = scanner.nextInt();
-
Why nextInt() is not "really" a "function"?
So what is nextInt()?
-
Explain this line:
import java.util.Scanner;
-
Modify the program in order to ask for two integer and to display their sum.
-
Display also the difference, the product, the quotient and the rest.
-
How could you get 2.5 as result of 5/2?
Exercice 5 - Conversion from String to integer
We want to write a program that displays the sum of integers
read from the command line.
Here is an example:
$ java Sum 15 5 231
integers: 15 5 231
sum: 251
This program is divided in some functions :
-
Write a method stringArrayToIntArray() that takes as argument an array of String
and returns an array of integers taken from these strings.
The static method parseInt(String s) of the class java.lang.Integer allows to get the value
of an integer stored in a string
-
What means static for a method?
-
What happens if the word taken from the command line is not a number
-
Write a method sumElementsOfIntArray() that takes as argument an array of int and returns their sum.
-
Write the main method that uses the two methods above
to display the array of int and its sum.
There is a little trick to display the array. You should take a look at
java.util.Arrays.
Exercice 6 - From C to Java
The purpose of this example is to show the diferences in perfomance
between a program in C and the same written in Java.
-
Compile (gcc pascal.c) and run the file a.out while asking
the system to measure the processing time (time a.out).
-
Write the same program in Java (Pascal.java).
You can use copy/paste.
Compile this program, and run it while measuring time
(use time again).
How can you explain the difference in speed?
© Université de Marne-la-Vallée