Unit 1.1: Introduction to Algorithms, Programming, and Compilers¶
Scope: CS Awesome 2, Section 1.1
Learning Goals¶
By the end of this lesson, you should be able to:
- Describe an algorithm as an ordered process.
- Explain the role of an IDE and a compiler.
- Read the structure of a minimal Java program.
- Distinguish syntax, logic, and run-time errors.
- Use compiler messages as evidence while debugging.
Algorithms and Sequencing¶
An algorithm is a finite, step-by-step process for completing a task or solving a problem. It may be represented with plain language, pseudocode, a diagram, or program code.
Sequencing means that steps happen in a specific order, one at a time. Changing the order can change the result.
Example algorithm for displaying a boarding message:
1. Display the gate number.
2. Display the destination.
3. Display the departure time.
This is precise enough to identify the order, but it is not yet Java code.
From an Algorithm to a Program¶
A programming language gives us exact rules for expressing an algorithm. An integrated development environment (IDE) usually provides an editor, a compiler, a Run command, and tools for viewing errors and output.
Design algorithm → Write source code → Compile → Run → Test → Debug
Testing asks whether the program behaves as intended. Compiling only checks errors the compiler knows how to detect.
A First Java Program¶
public class Welcome
{
public static void main(String[] args)
{
System.out.println("Welcome aboard!");
}
}
| Part | Purpose |
|---|---|
public class Welcome |
Declares a class named Welcome. |
{ and } |
Mark the beginning and end of a code block. |
public static void main(String[] args) |
Declares the method where execution begins. |
System.out.println(...) |
Displays a value followed by a new line. |
; |
Ends the printing statement. |
If the class is public, the file must be named Welcome.java. Java is case-sensitive, so Welcome, welcome, and WELCOME are different names.
Compiling and Running¶
The compiler translates Java source code into code the Java runtime can execute. It also rejects source code that violates certain language rules.
Welcome.java → Java compiler → Welcome.class → Java runtime → output
The compiler does not prove that the algorithm is correct. A program can compile successfully and still produce the wrong answer.
Keywords and Syntax¶
Java keywords, including public, class, static, and void, have predefined meanings. Keywords must be written exactly as required and cannot be used as custom names.
Common syntax rules in a first program include:
- matching
{ },( ), and double quotes; - correct capitalization;
- a semicolon after a complete statement;
- a public class name that matches the file name.
Three Major Error Categories¶
Syntax Error¶
A syntax error breaks a language rule and is detected by the compiler.
System.out.println("Ready") // missing semicolon
Logic Error¶
A logic error lets the program run, but the result is incorrect because the algorithm or implementation is wrong.
System.out.println("Gate 8"); // intended gate was 18
Run-Time Error¶
A run-time error occurs while the program is executing and may terminate it abnormally. An exception is a run-time event that interrupts normal execution.
int result = 10 / 0; // ArithmeticException at run time
Reading Error Messages¶
Status.java:5: error: unclosed string literal
System.out.println("Ready);
^
Read this message as evidence:
Status.javaidentifies the file.5identifies where the compiler detected the problem.unclosed string literaldescribes the likely rule violation.^points near where parsing failed.
The actual cause may appear before the reported position. Fix the first error, compile again, and then inspect the new result.
Practice Missions¶
Mission 1: Algorithm Audit¶
Write a six-step algorithm for a kiosk that prints a numbered ticket. Then exchange steps 2 and 5 and explain whether the altered order still solves the same problem.
Mission 2: Build a Launch Message¶
Create Launch.java that prints exactly:
Sequence confirmed.
Launch ready.
Use a public class, a correct main method, and two output statements.
Mission 3: Error Classifier¶
For each situation, classify the error as syntax, logic, or run-time and justify your answer.
- A closing quote is missing.
- A program prints the arrival time where it should print the departure time.
- A program compiles but stops when it attempts integer division by zero.
Mission 4: Debug from Evidence¶
Assume the file is Badge.java. Correct every problem below and list the rule behind each correction.
Public class badge
{
public static void Main(String[] args)
{
system.out.println("Visitor Badge")
}
}
Key Summary¶
| Concept | Core idea |
|---|---|
| Algorithm | An ordered process for solving a problem. |
| Sequencing | Steps execute in a defined order. |
| IDE | Tools for writing, compiling, running, and debugging. |
| Compiler | Translates code and detects some errors. |
| Syntax error | A language-rule violation detected before execution. |
| Logic error | A runnable program produces unintended behavior. |
| Run-time error | A failure occurs while the program executes. |
Source scope: CS Awesome 2, Unit 1.1