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.

Rung 1 — Scaffolded ⏱ ~50 min 🎯 LOs 3, 4 📖 Week 1 overview

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.

About typing the code in. Every code block in every lab disables copy-paste — except the very first one below. Why? Because once you’ve typed System.out.println( a dozen times, it stops being scary and starts being muscle memory. Copying robs you of that. The first Hello World is yours to copy because nobody should fight three things at once (syntax, IDE, and toolchain) just to see whether their setup works.

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 8881 on 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

Before you start this lab, install VS Code, Java and the SDPD profile — see the Setup page. It takes about 15 minutes and you only ever do it once. If you’ve already done it (or you’re on a lab PC that has it ready), keep reading.

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.

In File Explorer: 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.

Why a separate folder per week? Because every Java file has to be saved next to other files that belong with it. Mixing weeks means mixing programs the compiler thinks are related when they aren’t. Trust me on this one for now — there’s a real reason and we’ll get to it in Week 14.

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!");
    }
}

What every word means (read this once, then forget it for now).

  • public class Hello — declares a class called Hello. A class is a container for code. Every Java program lives inside at least one.
  • public static void main(String[] args) — the main method. When the program runs, this is where it starts. Every word in that line has a meaning we’ll unpack over the next few weeks — for now, just know that without it your program has no front door.
  • System.out.println("Hello World!"); — print the text inside the quotes, then move to a new line. The ; at the end is a full stop in Java’s grammar.
  • { opens a block, } closes it. Every { needs a matching }.

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.

Hello World!
Stuck? Show me what could be wrong
  • Filename mismatch. The file must be saved as Hello.java, with the capital H. hello.java won’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.
  • Hello typed 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.

Welcome to Java Programming!
If something went wrong

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.

The current month is September

Why two printlns instead of one? Because println (short for print line) prints whatever you give it, then moves to a new line. Two printlns = two lines of output. Next week we’ll see print (without the ln) and exactly what changes.

Stretch — try these if you finished early

  1. Change the text inside the quotes in Today.java to print today's actual month. Recompile and check.
  2. Add a third println line to Today.java that prints the day of the week. Three lines of output now.
  3. What happens if you delete the semicolon at the end of one of the println lines? Try it, look at the error message, then put the semicolon back.
  4. 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 01 folder: Hello.java, Welcome.java and Today.java
  • Each file compiles with no errors
  • Hello.java prints exactly Hello World! (case-sensitive, with the exclamation mark)
  • Welcome.java prints exactly Welcome to Java Programming!
  • Today.java prints exactly two lines: The current month followed by is 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's println (all lowercase letters in print, lowercase l, lowercase n). The compiler treats Println as 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.