Introduction to Java syntax

Java - TP1
Author

Ludovic Deneuville

Objectives

In this first tutorial, we’re simply going to familiarize ourselves with Java syntax, without using object-oriented programming.

This is why methods are defined as static. They are class methods. You don’t need to instantiate an object to call them.

We’re going to do a few exercises on the subject of passwords.

1 Git setup

In all tutorials, we are going to code on SSPCloud datalab.

On the datalab, your services have a limited lifespan.

To save your programs, the best practice is to use a git repository. We will therefore create and use a token to communicate with GitHub.

Using a token means you do not have to authenticate yourself every time you exchange data between your datalab service and GitHub.

TipHow it works
  • Create a classic token on GitHub
    • ghp_...
  • Declare it on SSPCloud
    • This creates an environment variable containing the token value.
    • export GIT_PERSONAL_ACCESS_TOKEN=ghp_...
  • Use it when creating the clone
    • git clone https://$GIT_PERSONAL_ACCESS_TOKEN@github.com/....git
  • You can now push and pull without authentication

1.1 Is your token still active?

    • Fill in:
      • token name: Datalab GENES
      • expiration date ➡️ Custom ➡️ 31 December
    • Tick repo and workflow boxes
    • Click on Generate token

This token will only be visible once. If you lose it or if it expires, you must generate a new one.

1.2 Is your token registered on SSPCloud?

On the Datalab:

If you do not have a declared token:

Warning

If you are uncertain or believe that the token declared on the datalab is no longer active, create a new one.

1.3 Create your remote repository

2 Launch Vscode Java

A service is available with Visual Studio Code and Java.

Important

You won’t find this service in the basic catalog. You have to use the link above to launch it.

It is a service built from vscode-python, on which Java is then installed via this script.

    • Click on Code
    • Copy https url to clipboard
    • Don’t forget to insert the token
    • In the menu (3 parallel bars top left) ➡️ File
    • Enter : /home/onyxia/work/ENSAI-2A-Java-TP/tp1 ➡️ OK
    • Or with command: code-server /home/onyxia/work/ENSAI-2A-Java-TP/tp1
TipShortcuts
  • SHIFT + ALT + A: Comment / Uncomment selected lines
  • SHIFT + ALT + O: Auto import

3 Compile and Run

How to compile and run Java code?

3.1 Compile

  • a single Java file: javac MyClass.java
  • all Java files: javac *.java
  • specify output directory for .class files (recommended): javac -d bin MyClass.java

3.2 Run

Important

To run a class, it must have a main() method.

  • java MyClass
  • when .class files are in the bin/ directory: java -cp bin MyClass
NoteStatic Methods

As mentioned at the beginning, the aim of this tutorial is not OOP, but simply basic Java syntax.

So all the methods defined here are static ➡️ They are class methods ➡️ They can be called without instantiating an object.

4 Exercices

    • Terminal > New Terminal (or CTRL + ù)

4.1 Brute force

You will write a Java method that attempts to brute-force a hashed 6-digit numeric password.

  • The method will iterate through all numbers from 000000 to 999999
  • For each number, generate and compare hash with the given target hash
  • If a match is found, return the original number else return null

Tip
  • Use method hashPassword()
  • String.format("%06d", 123)➡️ “000123”

4.2 Strong password

You will write a Java method to check if a password is strong. A password is strong if:

  • ✅ It has at least 12 characters
  • ✅ It contains at least one uppercase letter
  • ✅ It contains at least one lowercase letter
  • ✅ It contains at least one digit
  • ❌ It does not contain whitespace
Tip

Usefull methods:

  • Character.isUpperCase(<char>)
  • Character.isDigit(<char>)
  • Character.isWhitespace(<char>)

4.3 Check password list

You will write a method that takes a list of passwords (ArrayList) and checks their strength.

The method should return a HashMap where:

  • The keys are the passwords
  • The values are true if the password is strong, false otherwise

4.4 Generate a Random Password

Objective: Implement a method to generate a secure random password that includes at least:

  • ✅ 1 uppercase letter
  • ✅ 1 lowercase letter
  • ✅ 1 digit
  • ✅ 1 special character
  1. Ensure length is at least 4
  2. Create a list of characters
  3. Add at least one character from each group
  4. Fill the remaining characters randomly from all groups
  5. Shuffle the password to mix character types Collections.shuffle(<myList>)
  6. Convert the list into a String and return it
TipGenerate random int between 0 and 20
import java.security.SecureRandom;

SecureRandom random = new SecureRandom();
System.out.println(random.nextInt(20));

4.5 Evaluate a password

Objective: Reads a password, compute a score and evaluates its strength.

For the length, add two points each time the password exceeds a threshold: 8, 12, 16, 18 and 20 characters.

Then check whether the password contains lowercase letters, uppercase letters, digits, and special characters. Count how many of these categories are present and add (number of categories) to the score.

Score Strength
< 6 Weak
6 Okay
7–8 Good
> 8 Strong

4.6 Login System

Complete file Login.java:

Bonus:

  • After entering username, give 3 chances to input the correct password
  • If all 3 attempts fail, restart username input
TipUse a Scanner
Scanner scanner = new Scanner(System.in);

System.out.print("Enter username: ");
String username = scanner.nextLine();

scanner.close();

End of the Lab

Important
    • if your VSCode service dies, your code will not be lost
    • on Datalabs, there is no guarantee of the lifespan of your services
    • to free up reserved resources