Here we learn the basic syntax of Java, and train using jshell.
See: présentation.
In jshell, as with many shells, use ↑ (keyboard up arrow) to recall previous commands.
You can achieve all these tasks with only the syntax seen in this presentation, in particular, without using explicit conversions from String to integer or conversely. Try it!
-
Assign your first name to a variable and use it to print
Hello <your-first-name>.
-
Assign your age to a integer variable and use it to print
<your-age> is my age!
-
Assign each digit of your age to different integer variables; use them to print
<your-age> is my age!
-
Create a boolean variable whose value is
true
, another one whose value isfalse
, and use them to create an expression that evaluates totrue
and one that evaluates tofalse
-
Assign any number you like to an integer variable. Create an expression that evaluates to
true
if, and only if, that variable equals your age. (Test it by changing the value of the variable and running the expression again.) -
Assign the value
"true"
to a variable of typeString
. Create an expression that evaluates totrue
if, and only if, the value of that variable is equal to the String"true"
. Use the methods1.equals(s2)
to test for equality of two stringss1
ands2
.
-
Assign 4659 and 23 to variables, and show the result of multiplying these variables
-
Compute the greatest divisor of 4659 that is different than 4659 (use the
%
operator and a loop) (to check your answer, look at its factors) -
Define a method that returns the greatest divisor of 4659 that is different than 4659
-
Define a method that accepts an integer parameter and returns its greatest divisor except itself; use it to show the greatest divisor of 4659.
In a text editor, define a class MyMathClass
containing a method that accepts an integer parameter and returns its greatest divisor except itself, and a method that returns the smallest divisor of its parameter except one; and a class MyBooleanClass
containing a method xor
returning true
iff exactly one of its two boolean parameters is true
. Copy-paste this in jshell and call all these methods from jshell.
Define a class MovingThing
that has a static variable currentSpeed
and a static variable totalLength
; and a static method declared as static void move(double time)
that computes the distance an object moving at currentSpeed
moves in time
and adds it to the current total length. Copy-paste this in jshell and use your class to check that it works well. For example, after calling MovingThing.currentSpeed = 10
then MovingThing.move(50)
, the variable MovingThing.totalLength
should have a value of 500
.
Once this works, make all your variables private so that nobody but your own class can touch them. Modify your class to make it still useable by providing appropriate methods to get or set their values as required. Make sure to restrict access as much as reasonable, for example, an external user should not be able to modify the totalLength
directly.
Our goal here is to reach exercice 3.6 (here below), which requires to print out all numbers that have the maximum number of divisors. This requires to achieve exercices 3.1 and 3.2 first. Optionally, you may also want to attempt exercices 3.8 and 3.9 if you want some more advanced challenges.
-
EE3.1 to 3.2
-
3.6
-
EE3.8, 3.9 (optional)
The following two subsections are here for reference; we will come back to it later in the course.
See Oracle doc about the varargs syntax: Varargs
Exercice: call the static method String.format()
with no arguments, then with only one string as argument, then two strings, then three strings. Predict which calls will be accepted by the compiler. Explain in each case what parameters are effectively passed to the method, by considering the method declaration (hint: exactly two parameters are passed for each permissible call).
-
Classes as concept: instance variables and instance methods
-
Inheritance: extending interfaces and classes