Skip to content

Latest commit

 

History

History
56 lines (39 loc) · 5.04 KB

README.adoc

File metadata and controls

56 lines (39 loc) · 5.04 KB

Syntax

Here we learn the basic syntax of Java, and train using jshell.

Hint

In jshell, as with many shells, use ↑ (keyboard up arrow) to recall previous commands.

Short exercices

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!

  1. Assign your first name to a variable and use it to print Hello <your-first-name>.

  2. Assign your age to a integer variable and use it to print <your-age> is my age!

  3. Assign each digit of your age to different integer variables; use them to print <your-age> is my age!

  4. Create a boolean variable whose value is true, another one whose value is false, and use them to create an expression that evaluates to true and one that evaluates to false

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

  6. Assign the value "true" to a variable of type String. Create an expression that evaluates to true if, and only if, the value of that variable is equal to the String "true". Use the method s1.equals(s2) to test for equality of two strings s1 and s2.

Methods

  1. Assign 4659 and 23 to variables, and show the result of multiplying these variables

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

  3. Define a method that returns the greatest divisor of 4659 that is different than 4659

  4. Define a method that accepts an integer parameter and returns its greatest divisor except itself; use it to show the greatest divisor of 4659.

Classes

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.

More about classes

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.

More exercices

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.

Varargs

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

More syntax