TD1 — VSCodium, Maven & First Java Classes
S7 Inf A3 — Java for Graphical and Mobile Programming
Stéphane Derrode — Centrale Lyon
Part 1 — Java Fundamentals
Duration: 2h · Pair or individual work
Objectives
By the end of this session you will be able to:
- Create and run a Maven project in VSCodium
- Write Java classes with private fields, constructors, getters and setters
- Instantiate objects, call methods, and use
toString() - Understand the difference between
==and.equals() - Work with arrays of objects
Part 0 — Environment Setup
This part should already be done before the session.
Follow the VSCodium & Maven Setup Tutorial available on the course website. It walks you through installing JDK 17, Maven, VSCodium, and the Java Extension Pack step by step.
If you have any problem with installation, come 10 minutes early and we will sort it out.
Quick check — open a terminal inside VSCodium and verify:
Part 1 — Create the Maven Project (10 min)
1.1 Create the project
In VSCodium, press Ctrl+Shift+P → type Maven: Create Maven Project → select:
- Archetype:
maven-archetype-quickstart, version1.5 - Group Id:
com.s7infa3 - Artifact Id:
td1
Then File ▸ Open Folder… and select the td1/ folder that was just created.
1.2 Configure Java 17
Replace the content of pom.xml with:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.s7infa3</groupId>
<artifactId>td1</artifactId>
<version>1.0</version>
<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
</project>
Save (Ctrl+S). Then open src/main/java/com/s7infa3/App.java, click ▶ Run above main() — you should see Hello World!.
Part 2 — Class Voiture (45 min)
2.1 UML specification
┌──────────────────────────────────────────┐
│ Voiture │
├──────────────────────────────────────────┤
│ - puissance : int │
│ - estDemarree : boolean │
│ - vitesse : double │
├──────────────────────────────────────────┤
│ + Voiture(int p) │
│ + Voiture() │
│ + Voiture(int p, boolean d, double v) │
│ + deQuellePuissance() : int │
│ + getVitesse() : double │
│ + isEstDemarree() : boolean │
│ + demarre() : void │
│ + arrete() : void │
│ + accelere(double v) : void │
│ + equals(Object obj) : boolean │
│ + toString() : String │
└──────────────────────────────────────────┘
2.2 Behaviour rules
| Method | Behaviour |
|---|---|
Voiture(int p) |
If p > 0, puissance = p; otherwise puissance = 5. Always: estDemarree = false, vitesse = 0.0 |
Voiture() |
Delegate to Voiture(5) using this(5) |
demarre() |
Sets estDemarree = true |
arrete() |
Sets estDemarree = false and vitesse = 0.0 |
accelere(double v) |
Only if estDemarree == true: vitesse += v + 25 |
equals(Object obj) |
Two Voiture are equal if they have the same puissance |
toString() |
Returns "Voiture[p=4, demarree=true, v=124.8]" |
2.3 Skeleton to complete
Create src/main/java/com/s7infa3/Voiture.java:
package com.s7infa3;
public class Voiture {
// TODO: declare the 3 private fields
public Voiture(int p) {
// TODO
}
public Voiture() {
this(5); // delegates to Voiture(int)
}
public Voiture(int p, boolean d, double v) {
// TODO
}
public int deQuellePuissance() { /* TODO */ return 0; }
public double getVitesse() { /* TODO */ return 0; }
public boolean isEstDemarree() { /* TODO */ return false; }
public void demarre() { /* TODO */ }
public void arrete() { /* TODO */ }
public void accelere(double v) { /* TODO */ }
@Override
public boolean equals(Object obj) {
// TODO: true if obj is a Voiture with same puissance
return false;
}
@Override
public String toString() {
// TODO: "Voiture[p=4, demarree=true, v=124.8]"
return "";
}
}
2.4 Test in App.java
Replace App.java with the following and verify every printed value:
package com.s7infa3;
public class App {
public static void main(String[] args) {
// Basic usage
Voiture maClio = new Voiture(4);
maClio.demarre();
maClio.accelere(99.8);
System.out.println(maClio);
// Voiture[p=4, demarree=true, v=124.8]
// Invalid power → defaults to 5
Voiture defaut = new Voiture(-10);
System.out.println(defaut.deQuellePuissance()); // 5
// accelere() has no effect if not started
Voiture immobile = new Voiture(6);
immobile.accelere(50.0);
System.out.println(immobile.getVitesse()); // 0.0
// arrete()
maClio.arrete();
System.out.println(maClio);
// Voiture[p=4, demarree=false, v=0.0]
// == vs equals()
Voiture saPolo = new Voiture(4);
System.out.println(maClio == saPolo); // false (different objects)
System.out.println(maClio.equals(saPolo)); // true (same puissance)
// Three-argument constructor
Voiture v2 = new Voiture(28, true, 0.0);
System.out.println(v2);
// Voiture[p=28, demarree=true, v=0.0]
}
}
Part 3 — Array of Objects: class Parc (25 min)
A Parc manages a fixed-size fleet of Voiture objects using a plain array (not ArrayList — that is CM2).
3.1 Specification
Create src/main/java/com/s7infa3/Parc.java:
| Member | Description |
|---|---|
Parc(int capacity) |
Creates an array of capacity slots (all null initially) |
add(Voiture v) |
Adds v to the next free slot; silently ignores if full |
size() |
Returns the number of vehicles currently stored |
getAt(int i) |
Returns the vehicle at index i; throws IllegalArgumentException if out of range |
demarrerTout() |
Calls demarre() on every stored vehicle |
toString() |
Returns one vehicle per line |
3.2 Skeleton
package com.s7infa3;
public class Parc {
private Voiture[] fleet;
private int count;
public Parc(int capacity) {
// TODO
}
public void add(Voiture v) {
// TODO: add only if count < fleet.length
}
public int size() { /* TODO */ return 0; }
public Voiture getAt(int i) {
// TODO: throw IllegalArgumentException if i < 0 or i >= count
return null;
}
public void demarrerTout() {
// TODO: loop over the count stored vehicles
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
// TODO: append each vehicle + newline
return sb.toString();
}
}
3.3 Test
Add to the end of main():
System.out.println("\n--- Parc ---");
Parc parc = new Parc(5);
parc.add(new Voiture(4));
parc.add(new Voiture(6));
parc.add(new Voiture(28));
System.out.println("Size: " + parc.size()); // 3
parc.demarrerTout();
System.out.println(parc.getAt(0));
// Voiture[p=4, demarree=true, v=0.0]
System.out.println(parc);
try {
parc.getAt(10);
} catch (IllegalArgumentException e) {
System.out.println("Caught: " + e.getMessage());
}
Part 4 — Exploration (remaining time)
4.1 Add a private static int nbCreees = 0 counter to Voiture, incremented in every constructor. Add public static int getNbCreees(). Test that after creating 5 vehicles the counter returns 5.
4.2 Add a setVitesse(double v) method: only accepts v >= 0 and only works if the car is started. If v < 0, throw IllegalArgumentException.
4.3 Override hashCode() in Voiture:
Why is it important to override both equals() and hashCode() together? (Hint: think of HashMap<Voiture, String>)
Deliverable
No formal submission for TD1. Verify all outputs match the expected values before leaving.
Keep this project — it will be extended in TD2.
Common Errors
| Error | Cause | Fix |
|---|---|---|
cannot find symbol |
Typo or missing import | Check spelling; Ctrl+. in VSCodium to auto-fix |
incompatible types |
Wrong type assigned | Check declared type matches the value |
NullPointerException |
Array slot is null |
Make sure count tracks filled slots correctly |
ArrayIndexOutOfBoundsException |
Index out of range | Guard with i >= 0 && i < count in getAt() |
accelere() has no visible effect |
Car not started | Call demarre() before accelere() |