2025 Java Workshop¶
Introduction¶
Welcome to the 2025 Java Workshop! This workshop is designed to help you learn the basics of Java programming. Whether you're new to programming or just looking to brush up on your skills, this workshop is for you.
In this workshop you'll cover the following topics:
- Java syntax
- Variables and data types
- Control flow (if statements, loops)
- Basic Functions
Prerequisites¶
Before you get started, make sure you have the following installed on your computer:
- Docker Desktop https://docs.docker.com/desktop/
- Git https://git-scm.com/downloads
- VS Code https://code.visualstudio.com/download
- Dev Container Extension https://marketplace.visualstudio.com/items?itemName=ms-vscode-remote.remote-containers
Workshop Structure¶
- Setup
- Variables and Data Types
- Operators
- Input and Output
- Conditional Statements
- For Loops
- Arrays
- Functions
- Practical Project Demo
Setup¶
To get started, download the workshop materials by cloning the repository: (this can also be done from inside vscode)
Once you've cloned the repository, and it's open in VS Code you should get a popup asking if you want to open the development container. Click Reopen in Container
to get started.
This will start the development container and install all the necessary dependencies for the workshop including Java.
This may take a few minutes so have a chat with your fellow attendees while you wait.
When it's done you should see a terminal window with the Java version displayed.
Hello World¶
Let's start with the classic "Hello World" program. This program simply prints "Hello World" to the console.
package org.progsoc.java2025.basics;
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello World!");
}
}
Variables and Data Types¶
There are several different data types in Java, including int
, double
, boolean
, and String
. Each data type is used to store different types of values.
Typically you declare a variable by specifying the data type followed by the variable name. For example, to declare an integer variable called age
, you would write int age;
.
package org.progsoc.java2025.basics;
public class VariablesAndDataTypes {
public static void main(String[] args) {
int number = 5;
double decimal = 4.3;
String text = "These are variables";
boolean logic = true;
System.out.println("Number: " + number);
System.out.println("Decimal: " + decimal);
System.out.println("Text: " + text);
System.out.println("Logic: " + logic);
}
}
Operators¶
Operators are used to perform operations on variables and values. Java has several different types of operators, including arithmetic, comparison, and logical operators. (e.g. +
, -
, *
, /
, ==
, !=
, &&
, ||
)
package org.progsoc.java2025.basics;
public class Operators {
public static void main(String[] args) {
// Arithmetic operators: +, -, *, /
System.out.println(5 + 3); // 8
System.out.println(9 / 3); // 3
System.out.println(9 % 2); // 1
// Comparison operators: <, >, <=, >=, ==, !=
System.out.println(5 == 3); // false
System.out.println(5 != 3); // true
System.out.println(5 < 7); // true
System.out.println(5 >= 7); // false
// Logic Operators: || (or), && (and)
System.out.println(false || true); // true
System.out.println(5 > 3 && 7 > 5); // true
}
}
Input and Output¶
When building a simple Java program, you may want to take input from the user and display output to the user. This can be done using the Scanner
class for input and the System.out.println()
method for output. (similar to console.log
in JavaScript)
package org.progsoc.java2025.basics;
import java.util.Scanner;
public class UserInput {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter some text: ");
String enteredText = scanner.nextLine();
scanner.close();
System.out.println("You have entered: " + enteredText);
}
}
Conditional Statements¶
Conditional statements are used to perform different actions based on different conditions. In Java, you can use if
, else if
, and else
statements to control the flow of your program.
package org.progsoc.java2025.basics;
import java.util.Scanner;
public class Conditionals {
public static void main(String[] args) {
System.out.print("What is your age? ");
Scanner scanner = new Scanner(System.in);
int age = scanner.nextInt();
scanner.nextLine(); // to consume the newline character
if (age >= 18) {
System.out.println("You can legally drink!");
} else if (age >= 14) {
System.out.println("You can't legally drink, but you can legally work!");
} else {
System.out.println("You have no freedom");
}
scanner.close();
}
}
Arrays¶
An array is a collection of variables of the same type. You can access the elements of an array using an index. The index starts at 0, so the first element of an array is at index 0, the second element is at index 1, and so on.
package org.progsoc.java2025.basics;
public class Arrays {
public static void main(String[] args) {
String[] cars = { "Volvo", "BMW", "Mazda" };
// Accessing an item
System.out.println(cars[0]); // Volvo
// Changing an element of an array
cars[0] = "Ford";
System.out.println(cars[0]); // Ford
// Getting the length of the array
int carsLength = cars.length;
System.out.println(carsLength); // 3
}
}
Loops¶
If you want your program to repeat a block of code multiple times, you can use loops. Java has several different types of loops, including while
loops and for
loops which we'll cover below.
While Loop¶
This is a basic example of a while
loop. The loop will continue to run as long as the condition inside the parentheses is true
. (which is always in this case)
package org.progsoc.java2025.basics;
public class WhileLoop {
public static void main(String[] args) {
while (true) {
System.out.println("repeat forever");
}
}
}
While Condition¶
Instead of using a constant condition, you can use a variable to control the loop. In this example, the loop will continue to run as long as the value of i
is less than 5.
package org.progsoc.java2025.basics;
public class WhileCondition {
public static void main(String[] args) {
int i = 0;
while (i < 5) {
System.out.println("i is " + i);
i++;
}
}
}
For Loop¶
A for
loop is used when you know how many times you want to repeat a block of code. The loop has three parts: the initialization, the condition, and the increment.
Each of these parts is separated by a semicolon. The initialization is executed once before the loop starts, the condition is checked before each iteration of the loop, and the increment is executed after each iteration of the loop.
package org.progsoc.java2025.basics;
public class ForLoop {
public static void main(String[] args) {
for (int i = 0; i < 10; i++) {
System.out.println("Counted: " + i);
}
}
}
Over an Array¶
By combining a for
loop with an array, you can iterate over each element in the array and perform an action on it.
package org.progsoc.java2025.basics;
public class ArrayLoop {
public static void main(String[] args) {
double[] numbers = { 1, 2, 3, 4 };
// Start with zero
double sum = 0;
for (int i = 0; i < numbers.length; i++) {
// Add up each number in the array onto sum
sum = sum + numbers[i];
}
// Divide by the number of items to get the average
double average = sum / numbers.length;
System.out.println("Average: " + average); // 2.5
}
}
Functions (Methods)¶
A function is a block of code that performs a specific task. You can define a function to take input parameters and return a value. In Java, you define a function using the public static
keywords followed by the return type, the function name, and the parameters.
package org.progsoc.java2025.basics;
public class Functions {
public static boolean isEven(int val) {
return (val % 2) == 0;
}
public static void main(String[] args) {
System.out.println(isEven(4)); // true
System.out.println(isEven(7)); // false
System.out.println(isEven(8)); // true
}
}
Practical Project Demo¶
Guess Number¶
Now that you've learned the basics of Java programming, it's time to put your skills to the test with a practical project. In this demo, we'll build a simple number guessing game.
If you're ready, open the GuessNumber.java
file and follow the instructions in the comments to complete the project.
package org.progsoc.java2025.demos;
public class GuessNumber {
public static int getUserInput() {
java.util.Scanner scanner = new java.util.Scanner(System.in);
int enteredNumber = scanner.nextInt();
scanner.close();
if (enteredNumber < 1 || enteredNumber > 10) {
System.out.println("Invalid input. Please enter a number between 1 and 10.");
System.exit(1);
}
return enteredNumber;
}
public static int getComputerNumber() {
return (int) (Math.random() * 10) + 1;
}
public static String determineWinner(int userNumber, int computerNumber) {
if (userNumber == computerNumber) {
return "It's a tie!";
} else if (userNumber > computerNumber) {
return "You win!";
} else {
return "Computer wins!";
}
}
public static void main(String[] args) {
System.out.println("Welcome to the Number Guessing Game!");
System.out.println("Please enter a number between 1 and 10: ");
int userNumber = getUserInput();
int computerNumber = getComputerNumber();
System.out.println("Computer chose: " + computerNumber);
System.out.println("You chose: " + userNumber);
System.out.println("The winner is: " + determineWinner(userNumber, computerNumber));
}
}
Rock Paper Scissors¶
Another fun project you can try is building a simple rock-paper-scissors game. Open the RockPaperScissors.java
file and follow the instructions in the comments to complete the project.
package org.progsoc.java2025.demos;
/**
* A simple Rock, Paper, Scissors game that uses functions and takes user input
* using scanner
*/
public class RockPaperScissors {
public static void main(String[] args) {
System.out.println("Welcome to Rock, Paper, Scissors!");
System.out.println("Please enter your choice (rock, paper, or scissors): ");
String userChoice = getUserInput();
String computerChoice = getComputerChoice();
System.out.println("Computer chose: " + computerChoice);
System.out.println("You chose: " + userChoice);
System.out.println("The winner is: " + determineWinner(userChoice, computerChoice));
}
/**
* Get user input using scanner
*
* @return the user input (rock, paper, or scissors)
*/
public static String getUserInput() {
java.util.Scanner scanner = new java.util.Scanner(System.in);
String enteredText = scanner.nextLine();
scanner.close();
if (!enteredText.equals("rock") && !enteredText.equals("paper") && !enteredText.equals("scissors")) {
System.out.println("Invalid input. Please enter rock, paper, or scissors.");
System.exit(1);
}
return enteredText;
}
/**
* Get the computer's choice randomly
*
* @return the computer's choice (rock, paper, or scissors)
*/
public static String getComputerChoice() {
int random = (int) (Math.random() * 3);
if (random == 0) {
return "rock";
} else if (random == 1) {
return "paper";
} else {
return "scissors";
}
}
public static String determineWinner(String userChoice, String computerChoice) {
if (userChoice.equals(computerChoice)) {
return "It's a tie!";
} else if (userChoice.equals("rock") && computerChoice.equals("scissors")) {
return "You win!";
} else if (userChoice.equals("paper") && computerChoice.equals("rock")) {
return "You win!";
} else if (userChoice.equals("scissors") && computerChoice.equals("paper")) {
return "You win!";
} else {
return "Computer wins!";
}
}
}
Conclusion¶
Congratulations on completing the 2025 Java Workshop! We hope you've learned a lot and had fun along the way. If you have any questions or feedback, feel free to reach out to us on Discord or GitHub.