💻 What is a Program?
Program – a set of instructions written in a programming language that tells a computer what to do and how to do it.
Key Points:
- Solves specific problems or automates tasks.
- Programs take input, process it, and produce output.
- Executed by the computer's CPU.
- Programs are written using programming languages.
Program: مجموعة تعليمات تخبر الحاسوب ماذا يفعل وكيف. يحل مشاكل، يقوم بمهام، يأخذ مدخلات ويعطي مخرجات، ينفذه المعالج CPU.
📝 Programming Language
Programming language – a formal way of writing instructions that a computer can understand and execute. It is the tool used to create the program. Defines words, symbols, and rules to write valid program statements.
Programming language: طريقة رسمية لكتابة التعليمات يفهمها الحاسوب. تحدد الكلمات والرموز والقواعد لكتابة أوامر صحيحة.
🛠️ Program Development Phases
- Writing the program in a specific programming language (e.g., Java).
- Translating the program into a form the computer can execute (machine language).
- Debugging: Finding and fixing different types of errors.
١- كتابة البرنامج بلغة برمجة. ٢- ترجمته إلى لغة الآلة. ٣- تصحيح الأخطاء.
📄 Source Code
Source code – a program written in a programming language. It follows the syntax and rules of the language.
- A program that is syntactically correct is not necessarily logically (semantically) correct.
- A program will always do what we tell it to do, not what we meant to tell it.
- Aim for clear, maintainable, and well-structured code.
Source code: الكود المكتوب بلغة برمجة. يجب أن يتبع القواعد النحوية (syntax). الصحة النحوية لا تضمن الصحة المنطقية. البرنامج ينفذ ما نكتبه حرفياً.
⚙️ Compiler vs Interpreter
Computer cannot understand source code directly. It must be translated into machine language.
| Compiler | Interpreter |
|---|---|
| Translates entire source code into machine code (executable file) before the program runs. | Directly executes source code line by line without generating a separate machine code file. |
| The machine code can be saved and executed many times without recompiling. | Continues translating until the first error is met. |
| Examples: C++, C#, Java (compiles to bytecode, then interpreted/JIT). | Examples: PHP, Python. |
Compiler: يترجم كل الكود إلى لغة آلة دفعة واحدة قبل التنفيذ. ينتج ملفاً تنفيذياً. أمثلة: C++, Java.
Interpreter: ينفذ الكود سطراً سطراً دون إنشاء ملف منفصل. يتوقف عند أول خطأ. أمثلة: Python, PHP.
🐞 Types of Errors – Detailed with Examples
Cause: Violation of language grammar rules. Detected by compiler.
🔹 Most common syntax errors in Java:
- Missing semicolon at end of statement.
- Mismatched braces/parentheses
{ }or( ). - Using undeclared variables or misspelling variable names.
- Incorrect case (Java is case‑sensitive:
mainvsMain). - Missing quotes around strings.
📌 Examples:
Cause: Program compiles but crashes during execution.
🔹 Most common runtime errors in Java:
- ArrayIndexOutOfBoundsException – accessing index outside array length.
- NullPointerException – calling method on null reference.
- ArithmeticException – division by zero.
- NumberFormatException – converting invalid string to number.
- ClassCastException – invalid casting.
📌 Examples:
Cause: Program runs without crashing but produces wrong output. Hardest to detect.
🔹 Most common logic errors:
- Wrong formula (e.g., perimeter instead of area).
- Off-by-one errors in loops (starting from wrong index).
- Incorrect condition in if statements.
- Variable initialization mistakes (forgetting to initialize).
📌 Examples:
📊 Error Type Comparison
| Error Type | When detected | Program behavior | Common examples |
|---|---|---|---|
| Syntax | By compiler (before running) | Won't compile | Missing semicolon, mismatched braces, undeclared variable |
| Runtime | During execution | Crashes/throws exception | ArrayIndexOutOfBounds, NullPointer, division by zero |
| Logic | After execution (wrong output) | Runs but gives wrong result | Wrong formula, off-by-one, incorrect condition |
أنواع الأخطاء:
Syntax errors (أخطاء نحوية): يكتشفها المترجم. أمثلة: نسيان الفاصلة المنقوطة ;، عدم تطابق الأقواس {}، استخدام متغير غير مصرح به.
Runtime errors (أخطاء تنفيذية): تحدث أثناء التشغيل. أمثلة: الوصول لمؤشر خارج حدود المصفوفة ArrayIndexOutOfBoundsException، استدعاء دالة على كائن فارغ NullPointerException، القسمة على صفر.
Logic errors (أخطاء منطقية): البرنامج يعمل لكن النتيجة خاطئة. أمثلة: استخدام معادلة خاطئة (محيط بدل المساحة)، بدء حلقة من 1 بدلاً من 0، شرط if خاطئ.
🧩 What is OOP?
Object-Oriented Programming (OOP) is a programming paradigm that models a system as a collection of objects, each representing a real-world entity with data (attributes) and behavior (methods).
Classes and objects are the two main aspects of OOP.
OOP: نموذج برمجي يعتمد على الكائنات التي تمثل كيانات حقيقية ببيانات وسلوكيات. الكلاسات والكائنات هما الأساس.
📦 Class vs Object
- Class: A template / blueprint / prototype that describes what an object will be. It defines attributes (data/properties) and methods (behaviors).
- Object: An instance of a class. Represents real-world entities (car, dog, student) with their own attributes and behaviors.
Activity – Student class:
- Objects (instances): a specific student named "Ahmed", "Sara".
- Attributes: name, ID, GPA, major.
- Methods: study(), attendClass(), submitAssignment().
Activity – Bank class:
- Objects: a specific account (account number 12345).
- Attributes: accountNumber, balance, ownerName.
- Methods: deposit(), withdraw(), checkBalance().
Class: مخطط أو نموذج. Object: نسخة من الكلاس. مثال طالب: الكائنات (أحمد، سارة)، الصفات (الاسم، الرقم)، السلوكيات (يدرس، يحضر).
✅ Benefits of OOP
- Modularity – code organized into self-contained classes/objects.
- Reusability (DRY – Don't Repeat Yourself) – code can be reused by creating objects from classes and using inheritance (less code, shorter development).
- Maintainability – clear structure; easier to update, debug, and test.
- Scalability/Extensibility – add new features/objects with minimal impact.
- Abstraction – hide complexity; expose essential features.
- Natural Modeling – maps well to real-world entities and relationships.
Modularity: تقسيم الكود إلى وحدات. Reusability: إعادة استخدام الكود. Maintainability: سهولة الصيانة. Scalability: قابلية التوسع. Abstraction: إخفاء التعقيد. Natural Modeling: محاكاة العالم الحقيقي.
🌍 Popular OOP Languages
Java, C#, Python, C++ (supports both POP and OOP).
☕ Why Java?
- Created by James Gosling at Sun Microsystems, officially introduced May 20, 1995.
- Now the world's most widely used OOP language.
- Java is everywhere: mobile apps, games, web backends, big tech (Google, Amazon, LinkedIn, Netflix, Uber, NASA), trading platforms.
Main features:
- Simple
- Pure Object-Oriented (almost)
- Built-in security
- Portability & Versatility: “Write once, run anywhere” – code written on one platform can run on any other OS (Windows, Linux, Mac) without recompiling.
- Networked & Distributed – allows multiple computers to communicate on the same network or over the internet.
جافا لغة كائنية منتشرة. تتميز بالبساطة، الأمان، "اكتب مرة شغل في أي مكان" بفضل JVM. تستخدم في تطبيقات الهاتف، الويب، أنظمة كبرى (جوجل، أمازون، ناسا).
⚙️ Java Development – 5 Phases
- Edit: write source code (.java file) using an editor or IDE (Eclipse, IntelliJ, NetBeans, VS Code).
- Compile: Java compiler (javac) translates source code into bytecode (.class file).
- Load: Class loader loads .class files into memory.
- Verify: Bytecode verifier checks bytecodes for security and validity.
- Execute: JVM executes bytecodes using interpretation and JIT (Just-In-Time compilation).
١- التحرير (كتابة الكود). ٢- الترجمة إلى bytecode. ٣- تحميل الكلاسات. ٤- التحقق من bytecode. ٥- التنفيذ بواسطة JVM.
🔷 Bytecode & Java Virtual Machine (JVM)
- Bytecode: platform-independent intermediate code generated by Java compiler. Saved in .class files.
- JVM (Java Virtual Machine): part of JDK, executes bytecodes. It is a software application that simulates a computer, hiding the underlying OS and hardware.
- JIT (Just-In-Time) compiler: translates bytecodes into native machine code at runtime for better performance.
Command examples:
java Welcome → runs the program
Bytecode: كود وسيط مستقل عن المنصة. JVM: آلة افتراضية تنفذ bytecode. JIT: يحول bytecode إلى لغة الآلة أثناء التنفيذ لتحسين الأداء.
📦 JDK vs JRE vs JVM
- JVM (Java Virtual Machine): interprets or compiles bytecode into native machine code for execution.
- JRE (Java Runtime Environment): allows Java programs to run on any device. JRE = JVM + libraries + classes.
- JDK (Java Development Kit): used by developers to create and compile Java applications. JDK = JRE + development tools (compiler, debugger, etc.).
JVM: ينفذ bytecode. JRE: بيئة تشغيل = JVM + مكتبات. JDK: أدوات التطوير = JRE + مترجم وأدوات.
🌍 Write Once, Run Anywhere
While most languages achieve portability by compiling a program for each CPU, Java provides a JVM for each platform so programmers do not have to recompile for different platforms.
جافا توفر JVM لكل منصة، لذا الكود المصدري يُترجم إلى bytecode مستقل، ثم JVM المخصصة للمنصة تنفذه. لا حاجة لإعادة الترجمة.
📌 Unit 1 – All key points (complete recap with errors)
- Program: set of instructions. Programming language: formal rules.
- Development phases: Write → Translate → Debug.
- Compiler: whole program at once (Java, C++). Interpreter: line by line (Python).
- Error types with examples:
- Syntax – missing semicolon, mismatched braces, undeclared var.
- Runtime – array index out of bounds, null pointer, division by zero.
- Logic – wrong formula, off-by-one, incorrect condition.
- OOP: class (blueprint) vs object (instance). Benefits: modularity, reusability, maintainability, scalability, abstraction, natural modeling.
- Java: portable via JVM, bytecode, JDK/JRE/JVM, 5-phase execution.
- Portability: JVM per platform, same bytecode runs everywhere.
ملخص الوحدة الأولى:
- البرنامج: مجموعة تعليمات. لغة البرمجة: قواعد رسمية.
- مراحل التطوير: كتابة، ترجمة (compiler/meterجم), تصحيح.
- الأخطاء النحوية (Syntax): نسيان ;، أقواس غير متطابقة، متغير غير معلن.
- الأخطاء التنفيذية (Runtime): الوصول لمؤشر خارج المصفوفة، استدعاء دالة على null، قسمة على صفر.
- الأخطاء المنطقية (Logic): معادلة خاطئة (محيط بدلاً من مساحة)، بدء حلقة من 1 بدلاً من 0، شرط if خاطئ.
- OOP: كلاس (مخطط) وكائن (نسخة). فوائد: نمطية، إعادة استخدام، سهولة صيانة، تجريد.
- جافا: محمولة (bytecode + JVM)، JDK (أدوات تطوير)، JRE (بيئة تشغيل)، JVM (آلة افتراضية).