Skip to content

VOL 01 / CH 01 / LESSON 05

1.5 Lab: Fix Errors, Change Output, Test Entry Points ​

Prerequisites: Lessons 1 through 4 of this chapter Estimated time: 30 minutes Practice environment: JDK 17+ and Bash; all files written to a temporary directory You are ready to continue when: You have completed three required experiments and save the commands and results

All exercises are performed in a new directory:

bash
chapter1_lab_root=$(mktemp -d)
readonly chapter1_lab_root
mkdir -p "$chapter1_lab_root/chapter01-lab/out"
cd "$chapter1_lab_root/chapter01-lab"

When deleting or rebuilding artifacts, operate only within the out.

Required Exercise 1: Fix Three Types of Compilation Errors ​

Save as Bug.java:

java
public class Bug {
    public static void main(String[] args) {
        system.out.println("Hello, Java!")
        System.Out.println("Hello Coder!");
    }
}

Compile the code exactly as written:

bash
javac -d out Bug.java

Record the first diagnostic message, then fix the issues in order:

  1. Add a semicolon at the end of the first method call;
  2. Fix the capitalization of system;
  3. Fix the capitalization of Out.

Recompile after each change. Finally, run:

bash
java -cp out Bug

Expected output:

text
Hello, Java!
Hello Coder!

Note: A single syntax error can trigger multiple cascading diagnostics, so the number of initial error messages may not exactly match the number of independent root causes in the source code.

Required Exercise 2: Observe Recompilation ​

Save as Greeting.java:

java
public class Greeting {
    public static void main(String[] args) {
        System.out.println("version one");
    }
}
bash
javac -d out Greeting.java
java -cp out Greeting

Change the source code to version two, but do not compile yet, run it directly. You should still see version one. Only after recompiling and running again will the output change to version two.

In your lab notes, write a sentence explaining: Which file is being executed by java?

Required Exercise 3: Using Command-Line Arguments ​

Save as Greeter.java:

java
public class Greeter {
    public static void main(String[] args) {
        if (args.length == 0) {
            System.out.println("Hello, user!");
            return;
        }
        System.out.println("Hello, " + args[0] + "!");
    }
}

Compile and run twice:

bash
javac -d out Greeter.java
java -cp out Greeter
java -cp out Greeter Lin

Expected output:

text
Hello, user!
Hello, Lin!

You haven't yet systematically studied if and arrays, but for now, just confirm that the words following the class name in the startup command are passed into args.

Optional Exercise 1: print, println, and Escape Sequences ​

java
public class Lines {
    public static void main(String[] args) {
        System.out.print("north ");
        System.out.println("gate");
        System.out.println("line 1\nline 2");
        System.out.println("A\tB");
    }
}

Before running the code, write down your prediction of the four output lines. Then compile and verify. The terminal's display width for tab characters may vary, so focus only on whether there is a tab spacing between A and B.

Optional Exercise 2: Can Entry Methods Be Overloaded? ​

Save as EntryPoints.java:

java
public class EntryPoints {
    public static void main(String[] args) {
        System.out.println("launcher entry");
        main(3);
    }

    public static void main(int count) {
        System.out.println("overload: " + count);
    }
}

It compiles because the two methods have different parameter types, which qualifies as method overloading. The Java launcher selects the traditional entry point with a string array parameter, while main(int) is a regular overloaded method called internally by the program.

Now copy main(String[] args) and keep the signature exactly the same. The compiler will reject duplicate methods within the same class. This demonstrates the distinction between "same name" and "same signature."

Optional Exercise 3: Clean Artifacts Before Verification ​

bash
pwd
test ! -e out || rm -r -- out
mkdir out
javac -d out Greeter.java
java -cp out Greeter Ada

This demonstrates that the results are not dependent on stale artifacts. pwd must show the current experiment directory; do not run recursive deletion in an uncertain or ambiguous location.

Experiment Log Template ​

ExperimentOriginal CommandFirst Key Diagnosis or OutputModificationRevalidation Result
Bug
Rebuild
Parameters

Acceptance and Cleanup ​

Confirm that Bug.java produces two lines of output, observe firsthand that the old bytecode still runs, and verify that both the zero-argument and parameterized paths of Greeter pass successfully. After saving your lab notes somewhere you want to keep them, delete the temporary directory:

bash
cd
ls -ld -- "$chapter1_lab_root"
rm -r -- "$chapter1_lab_root"

Errors don't teach themselves; you must document the commands, stages, the first diagnostic message, and the location of the output artifact, then re-run the test to narrow down the issue.

→ Next: Variables and Types

Built with VitePress | Software Systems Atlas