Unit 1.1
Introduction to Algorithms, Programming, and Compilers
Learning Objectives:
- 1.1.A: Represent patterns and algorithms using written language or diagrams
- 1.1.B: Explain the code compilation and execution process
- 1.1.C: Identify types of programming errors
What are Algorithms?
Definition
An algorithm defines step-by-step processes to follow when completing a task or solving a problem.
Key Characteristics
- Sequential: Steps are completed one at a time
- Precise: Each step must be clearly defined
- Finite: Must eventually complete
- Effective: Must solve the intended problem
Real-World Example: Making a Sandwich
- Get two slices of bread
- Open the peanut butter jar
- Spread peanut butter on one slice
- Open the jelly jar
- Spread jelly on the other slice
- Put the slices together
Representing Algorithms
Written Language
Algorithms can be described using natural language (like English) in a clear, step-by-step format.
Diagrams and Flowcharts
Visual representations help us understand the flow and decision points in algorithms.
Algorithm: Find the Largest Number in a List
- Start with the first number as the current largest
- Look at the next number in the list
- If this number is larger than the current largest, make it the new largest
- Repeat steps 2-3 until you've checked all numbers
- The current largest is your answer
Sequencing: The order in which steps are completed matters!
From Algorithms to Programs
What is Programming?
Programming is the process of converting algorithms into instructions that a computer can understand and execute.
Programming Languages
- High-level languages: Java, Python, C++ (easier for humans)
- Low-level languages: Assembly, Machine Code (closer to hardware)
Why Java?
- Object-oriented programming language
- Platform independent ("Write once, run anywhere")
- Widely used in industry
- Strong typing helps catch errors early
Development Environment
Writing Code
Code can be written in any text editor, but we typically use an Integrated Development Environment (IDE).
What is an IDE?
An IDE provides tools for programmers to:
- Write code with syntax highlighting
- Compile code to check for errors
- Run programs to test functionality
- Debug code to find and fix problems
Popular Java IDEs
- BlueJ (beginner-friendly)
- Eclipse
- IntelliJ IDEA
- Visual Studio Code
Code Compilation and Execution
The Java Compilation Process
Steps:
- Write Java source code (.java file)
- Compile using javac compiler
- Generate bytecode (.class file)
- Execute bytecode on Java Virtual Machine (JVM)
Role of the Compiler
The compiler checks your code for syntax errors before creating executable bytecode. Errors detected by the compiler must be fixed before the program can run.
Types of Programming Errors
Syntax Errors
Mistakes in the program where the rules of the programming language are not followed.
When detected: At compile time
Example: Missing semicolon, misspelled keywords
Logic Errors
Mistakes in the algorithm that cause incorrect or unexpected behavior.
When detected: During testing
Example: Using wrong formula, incorrect conditions
Run-time Errors
Mistakes that occur during program execution, typically causing abnormal termination.
When detected: While program is running
Example: Division by zero, accessing invalid array index
Exceptions
A type of run-time error that interrupts the normal flow of program execution.
When detected: During execution
Example: NullPointerException, ArrayIndexOutOfBoundsException
Programming Error Examples
Syntax Error Example
Logic Error Example
Run-time Error Example
Problem-Solving Process
Steps to Solve Programming Problems
- Understand the Problem - What exactly needs to be solved?
- Plan the Algorithm - Break down into step-by-step solution
- Write the Code - Implement the algorithm in Java
- Test the Program - Run with different inputs
- Debug and Refine - Fix any errors found during testing
Best Practices
- Start with simple examples
- Write algorithms in plain English first
- Test your logic before coding
- Use meaningful variable names
- Comment your code to explain complex logic
Let's Practice!
Activity: Algorithm Design
Work with a partner to write an algorithm for the following problem:
Problem: Netflix Password Game
Your friend wants to guess your 4-digit Netflix PIN. Write an algorithm that your friend could follow to systematically guess your PIN in the most efficient way possible.
Constraints:
- Each digit is 0-9
- Digits can repeat (like 1111 or 2002)
- After 3 wrong guesses, the system locks for 1 minute
- You want to minimize total time to crack the code
Extension: What if you know the PIN contains at least one repeated digit?
Algorithm Analysis
- How many total possible combinations exist?
- What's the worst-case scenario for your algorithm?
- How does the lockout feature change your strategy?
- What assumptions is your algorithm making?
- How would you test if your algorithm works correctly?
Unit 1.1 Summary
Key Concepts
- Algorithms: Step-by-step processes for solving problems
- Sequencing: Order of steps matters
- Programming: Converting algorithms into computer instructions
- IDE: Tools for writing, compiling, and running code
- Compilation: Process of converting source code to executable bytecode
Error Types
- Syntax Errors: Violate language rules (compile time)
- Logic Errors: Incorrect algorithm (testing reveals)
- Run-time Errors: Occur during execution
- Exceptions: Special type of run-time error
Next Up: Unit 1.2 - Variables and Data Types
We'll learn how to store and manipulate data in Java programs!