CS203 Ultimate Study Hub

Object-Oriented Programming • Professor Mahmut Unan • UAB CS

📗

Week 1: Java Fundamentals

Introduction to Java, OOP Concepts, Syntax Basics

🎯

Why OOP?

  • • Security: Bind data + functions together
  • • Complexity: Easier to manage large systems
  • • Reusability: Objects reused across applications
  • • Maintenance: Modular design for updates
🧩

Core OOP Concepts

  • • Abstraction: Extract common features
  • • Class: Blueprint/category of objects
  • • Encapsulation: Combine elements into entity
  • • Inheritance: "IS-A" relationship
  • • Polymorphism: Process objects differently
⚖️

Java vs Python

Python: Whitespace matters
Java: Braces { } matter
Python: Newline ends statement
Java: Semicolon ; ends
Python: Dynamic typing
Java: Static typing
📦

Primitive Types

int, long, short, byte
double, float
char
boolean

Types must be declared: int x = 10;

🔄

Control Flow

  • • if / else if / else
  • • switch statements
  • • while loop
  • • do-while loop (runs at least once!)
  • • for loop
  • • for-each loop
⚙️

Compiler & JVM

Python: Interpreted (line by line at runtime)

Java: Compiled → bytecode → JVM executes

javac MyFile.java → java MyFile
Hello World Comparison
// Python:
// print("Hello World")

// Java:
public class Main {
    public static void main(String[] args) {
        System.out.println("Hello World");
    }
}

💻 Week 1 Lab Code

Lab 1: Fibonacci & Numbers (Completed)

Fibonacci.java
import java.util.Scanner;

public class Fibonacci {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter the value of n: ");
        int n = scanner.nextInt();

        int first = 0;
        int second = 1;

        for (int i = 1; i <= n; i++) {
            System.out.print(first + " ");
            int next = first + second;
            first = second;
            second = next;
        }
    }
}
Numbers.java
public class Numbers {
    public static void main(String[] args) {
        int x = 100;
        int sum = 0;
        for (int i = 0; i <= x; i++) {
            sum = sum + i;
        }
        System.out.println(sum);  // Output: 5050
    }
}

📋 Week 1 SOP

Java Environment Setup & First Programs

1

Install JDK & IDE

2

Create Your First Program

3

Compile & Run

4

Lab 1: Fibonacci & Numbers

🎯 Week 1 Quiz

Test Your Java Fundamentals

1. In Java, what character ends a statement?

2. Which is NOT a primitive type in Java?

3. What does the JVM do?

4. Which loop is guaranteed to run at least once?

🎓

Week 1: Lecture Walkthrough

Step-by-step learning path through Java fundamentals

📖 Follow this guided walkthrough to master Week 1 concepts in the optimal learning order
1

Why OOP & Why Java?

⏱️ 15 min • Start Here

🎯

Learning Goal

Understand the motivation behind Object-Oriented Programming and why Java is a great language to learn it.

📚 Key OOP Benefits

  • ▸ Security: Bind data and functions together — no unauthorized access
  • ▸ Reusability: Objects can be reused across applications
  • ▸ Easier Maintenance: Modular design allows updating specific parts
  • ▸ Manageability: Handle complexity of large software systems
Abstraction
Encapsulation
Inheritance
Polymorphism

✅ Checkpoint

Can you name one advantage of OOP over procedural programming? (Answer: Modularity, reusability, security through encapsulation, etc.)

2

Java vs Python: Key Syntax Differences

⏱️ 20 min • Critical Foundation

Feature Python Java
Statement End New line Semicolon ;
Code Blocks Indentation Braces { }
Variable Types Dynamic (inferred) Static (declared)
Comments # comment // comment

⚠️ Common Mistake

Forgetting the semicolon at the end of statements is the #1 error for Python programmers learning Java. Your program will NOT compile!

✅ Checkpoint

What happens if you forget a semicolon in Java? (Answer: Compile error! The code won't even run.)

3

Compilation: Source → Bytecode → JVM

⏱️ 15 min • How Java Runs

Java Execution Pipeline
┌─────────────────┐    javac     ┌─────────────────┐    java     ┌─────────────────┐
│  Hello.java    │───────────▸│  Hello.class   │───────────▸│      JVM       │
│  (Source Code) │  Compiler  │  (Bytecode)    │  Runtime   │  (Execution)   │
└─────────────────┘            └─────────────────┘            └─────────────────┘

🔨 Interpreted (Python)

Code is read and executed line by line at runtime. Errors found during execution.

⚙️ Compiled (Java)

Code is validated and converted to bytecode BEFORE running. Many errors caught early!

✅ Checkpoint

What is the JVM? (Answer: Java Virtual Machine — it executes bytecode. This allows "Write Once, Run Anywhere"!)

4

Primitive Types & Type Declarations

⏱️ 20 min • Core Concept

🔑 The Big Rule

In Java, you MUST declare the type of every variable. The type cannot change!

int x = 10; // x is an int FOREVER

int

Integer

double

Decimal

boolean

true/false

char

Character

💡 String is NOT a primitive!

String is a class (reference type). Primitives are: int, long, short, byte, float, double, char, boolean.

✅ Checkpoint

Is String a primitive type? (Answer: NO! String is a class/reference type.)

5

Control Flow: if, switch, loops

⏱️ 25 min • Essential Syntax

If Statement Syntax
if (condition) {      // Parentheses required!
    // code block       // Braces for multiple statements
} else if (other) {
    // other code
} else {
    // default
}

while

Check BEFORE loop

May run 0 times

do-while

Check AFTER loop

Runs at least once!

for

Init; condition; update

Counter-based

⚠️ Logical Operators

&& = AND || = OR ! = NOT

✅ Checkpoint

Which loop runs at least once even if the condition is false? (Answer: do-while)

6

Hello World: The Java Program Structure

⏱️ 15 min • Final Topic

Hello.java
public class Hello {                // Class name = filename!
    public static void main(String[] args) {  // Entry point
        System.out.println("Hello World");
    }
}

🔑 Key Points

  • • Class name MUST match filename (Hello.java)
  • • public static void main(String[] args) is the entry point
  • • Everything lives inside a class in Java

🎉 Week 1 Complete!

You've learned Java basics: OOP concepts, syntax differences from Python, compilation, types, and control flow. Take the quiz to test your knowledge!