Week 1 · Lab 1 — Your first Java program
Write, compile and run Hello World in Java. By the end you’ll know exactly what just happened.
First contact with Java. You'll go from an empty file to a program that prints to the screen, and you'll see what the compiler does when you get it wrong.
What you’ll do
You’ll write three short Java programs called Hello.java, Welcome.java and Today.java. They each print one or two lines of text to the console. Along the way you’ll learn what a Java class looks like, what public static void main(String[] args) actually means, and what the compiler tells you when you miss a semicolon.
Before you start
Preflight checklist
- VS Code is installed and open
- The sdpd-grader extension is active (green dot in the bottom-right status bar)
- You're enrolled on the Moodle page for SDPD 1 (course code
8881on vlegalwaymayo.atu.ie) - You have somewhere to save your files — see the Setup section below if not
Setup — do this once at the start of the year
You also need a place to save your files. The first time you do a lab, do the next two steps. Skip ahead to Exercise 1 if you’ve already done them.
Step 1 — OneDrive
In Windows, search for “OneDrive” and sign in with your ATU email address (the one ending in @atu.ie). Once it’s running you should see OneDrive — Atlantic TU in your File Explorer sidebar. This is where all your lab work lives — it survives lab-PC reboots and follows you between machines.
Step 2 — A folder for your Java work
Inside your OneDrive, make a folder called Java Labs. Inside that, make a folder for this week’s work called Week 01. You’ll add a new Week 02, Week 03 and so on as we go.
OneDrive — Atlantic TU \ Java Labs \ Week 01.This is the folder you'll save every Week 1 lab file into.
Step 3 — Open the folder in VS Code
In VS Code: File → Open Folder… and pick the Week 01 folder you just made. Confirm the bottom-left status bar says SDPD (your profile is active). You’re now ready to start writing Java.
Exercises
Hello.java — your first Java program
Write, compile and run a program that prints Hello World! to the console.
Step 1 — Create the file. In VS Code, hit Ctrl+N (or Cmd+N on Mac) to make a new file. Save it immediately (Ctrl+S) into your Week 01 folder with the exact name Hello.java — capital H, the rest lowercase, ending in .java. The filename has to match the class name inside the file. Get this wrong and the compiler will say so.
Step 2 — Type the code. This first one is yours to copy — see the badge in the top-right of the code block. The class name in the code (Hello) must match the filename (Hello.java) exactly:
Hello.java
public class Hello {
public static void main(String[] args) {
System.out.println("Hello World!");
}
}Step 3 — Compile and run it. Press F5 (or Ctrl+Shift+B). VS Code’s terminal opens at the bottom of the window and runs the javac compile then the java run. You should see the output below within a couple of seconds.
Later, when the sdpd-grader extension is live, you’ll also see a ▶ Submit lab button in the bottom status bar that does this plus uploads to Moodle for grading. For now, F5 is enough.
- Filename mismatch. The file must be saved as
Hello.java, with the capital H.hello.javawon’t work. - Missing semicolon. Look at the end of line 3 — it ends in
;. Java treats a missing semicolon as a hard error. - Mismatched braces. Count the
{and the}. There should be exactly two of each. Hellotyped differently in the class line and the filename. The compiler will say so explicitly: “class Hello is public, should be declared in a file named Hello.java”.
Welcome.java — type it yourself this time
Write a second Java program — same shape as Hello.java, but different class name and different output.
Step 1 — New file. Ctrl+N, save as Welcome.java in Week 01. The class name inside must be Welcome to match.
Step 2 — Type this in. No copying. Notice how the class name on line 1 and the filename match:
Welcome.java
public class Welcome {
public static void main(String[] args) {
System.out.println("Welcome to Java Programming!");
}
}Step 3 — Compile and run. Same as before — F5.
The most common mistake at this stage is typing welcome (lowercase) somewhere it should be Welcome (capital W) — or vice versa. Java is case-sensitive: Welcome, welcome and WELCOME are three different names as far as the language is concerned. Check the class declaration, the filename, and any references against each other.
Today.java — printing two lines
Write a program that prints two lines to the console using two println statements.
Step 1 — New file. Ctrl+N, save as Today.java in Week 01.
Step 2 — Type the code. Type two separate System.out.println lines, one after the other:
Today.java
public class Today {
public static void main(String[] args) {
System.out.println("The current month");
System.out.println("is September");
}
}Step 3 — Compile and run. F5.
Stretch — try these if you finished early
- Change the text inside the quotes in
Today.javato print today's actual month. Recompile and check. - Add a third
printlnline toToday.javathat prints the day of the week. Three lines of output now. - What happens if you delete the semicolon at the end of one of the
printlnlines? Try it, look at the error message, then put the semicolon back. - What happens if you change
"Hello World!"to'Hello World!'(single quotes instead of double)? Try it.
Submit
Submitting this lab
When the sdpd-grader extension is live, you'll see a ▶ Submit lab button in the bottom status bar. Clicking it compiles all three of your .java files and uploads the result to Moodle. You get a pass/fail panel within ~5 seconds and can submit as many times as you like before the deadline — only your last submission counts.
Until the grader is live, zip your Week 01 folder and upload it to the Lab 1 assignment on Moodle.
How the grader scores this
What the grader checks
- Three files exist in your
Week 01folder:Hello.java,Welcome.javaandToday.java - Each file compiles with no errors
Hello.javaprints exactlyHello World!(case-sensitive, with the exclamation mark)Welcome.javaprints exactlyWelcome to Java Programming!Today.javaprints exactly two lines:The current monthfollowed byis September
Common pitfalls
What usually goes wrong on lab 1
- Class name doesn't match the filename. The compiler error reads: "class X is public, should be declared in a file named X.java". Rename one or the other.
- Single quotes around the string. In Java,
"hello"is a string but'hello'is a syntax error. Single quotes are for individual characters only — we'll meet them in Week 2. - Capital P in
Println. It'sprintln(all lowercase letters inprint, lowercasel, lowercasen). The compiler treatsPrintlnas a completely different name it has never heard of. - Forgetting the semicolon. Every statement ends in
;. The compiler points at the line after the missing one, which is confusing the first few times. - Mismatched braces. If you delete a
{or}by accident, the compiler complains about a different line than the one that's wrong. Count braces top to bottom — there should be the same number of opens and closes.
Where to ask
If you're stuck and the hints didn't help
In the lab: wave at your lab supervisor — that's what they're there for. Don't fight a missing semicolon alone for 20 minutes.
Outside the lab: post in the Lab 1 questions forum on Moodle so the answer reaches everyone in the cohort.
For anything else: michael.duignan@atu.ie — replies within 48 hours during teaching weeks.