oopr_ni_kenyonPogi_PRELIMS.pdf
Document Details
Uploaded by HighQualityCarnelian9252
OLFU
Tags
Full Transcript
(Module 1) Introduction to Programming Paradigms What are Computers Good at? Computers have three main advantages over humans: 1. Storage: They can store massive amounts of information. 2. Recall: They retrieve information quickly and accurately. 3. Calculation: They perform calculation...
(Module 1) Introduction to Programming Paradigms What are Computers Good at? Computers have three main advantages over humans: 1. Storage: They can store massive amounts of information. 2. Recall: They retrieve information quickly and accurately. 3. Calculation: They perform calculations at lightning speed and with perfect accuracy. However, computers can't do anything without step-by-step instructions telling them what to do. These instructions are known as a computer program. What is Programming? Programming is the process of giving a set of written instructions to the computer, telling it what to do. There are multiple approaches to writing programs, known as programming paradigms. Programming Paradigms Definition: A programming paradigm refers to an approach or method used to solve problems or perform tasks using a programming language. Two Popular Approaches in Writing Computer Programs: 1. Procedural Programming ○ Definition: Also known as imperative programming, this approach uses a list of instructions to tell the computer what to do step by step. ○ Key Characteristics: Relies on procedures or subroutines to perform computations. Many early programming languages are procedural. ○ Examples: C: Developed by Dennis Ritchie and Ken Thompson. FORTRAN: One of the earliest procedural programming languages. COBOL: Commonly used in business and financial systems. 2. Object-Oriented Programming (OOP) ○ Definition: OOP is an extension of procedural programming that approaches problems by modeling them as objects. It involves creating classes, objects, and applications. ○ Key Characteristics: Classes serve as blueprints for creating objects. Objects are created from these classes. Applications are built using these objects. ○ First Object-Oriented Programming Language: Simula (Simula I, Simula 67), created by Ole-Johan Dahl and Kristen Nygaard in the 1960s. ○ Examples: Simula: The first object-oriented programming language. Java: Developed by James Gosling. C++: Developed by Bjarne Stroustrup. Python: Developed by Guido van Rossum. Ruby: Developed by Yukihiro Matsumoto. Basic Concepts in Object-Oriented Programming Objects and Classes 1. Object: A component of a program that knows how to perform certain actions and how to interact with other elements in a program. ○ Objects are the basic units of OOP. ○ An object can be anything, such as a person or a pen. ○ Objects have attributes (state) and behaviors (methods). Example: A pen is an object. Attributes (State): Name = Reynolds, Color = White. Behavior: Writing. 2. Class: ○ A class is a blueprint or template from which objects are created. ○ It defines a group of objects with common properties. ○ Syntax for Declaring a Class: Core Concepts of Object-Oriented Programming 1. Inheritance: ○ Inheritance allows one object to acquire the properties and behaviors of a parent object. ○ It provides code reusability and supports runtime polymorphism. ○ Parent Class (Super Class): The class whose properties are inherited. ○ Child Class (Sub Class): The class that inherits from the parent class. 2. Polymorphism: ○ Definition: The ability of a variable, function, or object to take on multiple forms. ○ Polymorphism allows you to define one interface or method with multiple implementations. ○ Example: A single function name can have different implementations in different contexts. 3. Encapsulation: ○ Definition: The bundling of data and methods into a single unit (class) and restricting access to them. ○ Encapsulation ensures that the data is protected from outside access and modification. ○ Analogy: Like a medical capsule that protects the drug inside, encapsulation hides and protects the data of a class. 4. Abstraction: ○ Definition: The process of hiding unnecessary details from the user and showing only essential features. ○ Abstraction focuses on exposing only the relevant information and concealing internal complexities. Summary: Class: Blueprint for creating objects. Object: Instance of a class with attributes and methods. Encapsulation: Protecting the internal state and methods of an object. Inheritance: A mechanism that allows a class to inherit properties from another class. Polymorphism: Ability to use the same method name in different contexts. Abstraction: Hiding complexity and exposing only what is necessary. (Module 2) History of Java The Green Project (1991) Java began as part of the Green Project, which was initiated to develop software for consumer electronics like televisions and computers. James Gosling was the chief engineer of the project. Oak Programming Language The language created by James Gosling for the Green Project was initially named Oak, after an oak tree outside his office. In 1992, Oak was the predecessor of Java. Java’s Name Origin The name Java was derived from a local coffee shop where the team frequented. This is why the icon for Java is a coffee cup. Java’s Official Launch (1995) In 1995, Java was officially released with the promise of WORA – “Write Once, Run Anywhere.” This meant that Java programs could run on any device or platform without modification. Java Today In 2010, Java became one of the most widely used programming languages in the world. What is Java? Java is a general-purpose, concurrent, object-oriented, class-based programming language. It is known for its runtime environment (JRE), which includes the Java Virtual Machine (JVM), a key component of the Java platform. Java is free to access and runs on all platforms, making it suitable for various applications. Uses of Java: 1. Banking: Used for transaction management. 2. Retail: Java powers billing applications in stores and restaurants. 3. Information Technology: Solves implementation dependencies. 4. Android: Android applications are written in Java or use Java APIs. 5. Financial Services: Server-side applications in finance often use Java. 6. Stock Market: Used to write algorithms for stock market investments. 7. Big Data: The Hadoop MapReduce framework is written in Java. 8. Scientific Research: Java is used for handling large amounts of data. Features of Java 1. Simple: Java eliminates complex features like pointers and operator overloading, found in other languages like C++. 2. Portable: Java is platform-independent, meaning applications written in Java can run on different platforms without modification. 3. Object-Oriented: Everything in Java is considered an object, and all operations are performed using objects. 4. Secure: Java converts code into bytecode after compilation, which is not readable by humans. It also runs programs in a sandbox to prevent security breaches. 5. Dynamic: Java supports dynamic memory allocation, reducing memory wastage and improving performance. 6. Distributed: Java provides features for creating distributed applications. Through Remote Method Invocation (RMI), one program can invoke methods in another program across a network. 7. Robust: Java’s strong memory management and compile-time and run-time checks reduce the likelihood of errors. 8. High Performance: Java achieves high performance through bytecode and Just-In-Time (JIT) compilers, which convert bytecode into native machine code. 9. Interpreted: Java’s bytecodes are interpreted by the Java Runtime Environment (JRE). 10. Multithreaded: Java supports multiple threads of execution, which makes it easier to write programs that perform multiple tasks simultaneously. Structure of a Java Program Class Definition: Class: A class is a container that holds all the pieces of a program, including attributes and methods. Class Body: Contains the attributes (variables) and methods (functions) necessary for the program to work. Key Elements: 1. Access Modifier: ○ Public: An access modifier that allows the code to be accessed by all objects. 2. Keyword - Class: ○ Class is a keyword that informs the compiler that a class is being defined. Method Definition: Method Header and Body: The header includes the method's signature, and the body contains the logic to perform a task. Method Modifier: Specifies the properties of the method (e.g., static, final). Void: Means the method does not return a value. Arguments: Values passed to the method. In this example: HelloWorld is the class. public static void main(String[] args) is the main method, where execution begins. System.out.println prints the message to the console. Escape Sequences \n: Newline \t: Horizontal tab \: Backslash ": Double quote Comments in Java Single-line comment: // This is a single-line comment Multi-line comment: Programming Errors in Java 1. Syntax Errors: ○ Occur when the code structure or grammar is incorrect (e.g., missing semicolons). 2. Semantic Errors: ○ Occur when the code is unrecognizable by the Java compiler due to incorrect logic. 3. Runtime Errors: ○ Occur while the program is executing and typically indicate issues like division by zero or missing files. Common Errors and Solutions: Missing Semicolon: ○ Error: ; expected ○ Solution: Add a semicolon at the end of the line. Incorrect Punctuation: ○ Error: ) expected ○ Solution: Ensure all parentheses and curly braces are properly matched. Invalid Argument: ○ Error: Invalid argument ○ Solution: Make sure arguments are correctly formatted, with commas separating them. (Module 3) Java Data Types What is a Data Type? A data type is a classification that specifies the type of data a variable can hold. In Java, data types are always included when declaring a variable. Java Supports Two Types of Data Types: 1. Primitive Data Types: Basic data types that are predefined by the language. ○ These include: Type Name Kind of Value Memory used byte Integer 1 byte short Integer 2 bytes int Integer 4 bytes long Integer 8 bytes float Floating point numbers 4 bytes double Floating point numbers 8 bytes char Single Character 2 bytes Boolean True or False 1 bit 2. Class Data Types: Also known as non-primitive or reference data types, these are created by users or provided by Java, such as String and objects. ○ Example: String name; Declaring and Initializing Variables What is a Variable? A variable is a storage location in the computer's memory where a value can be stored and later used by a program. A variable can only hold one value at a time, and this value can change during the program’s execution. Syntax for Variable Declaration: Single Variable Declaration: [DataType] [variable]; ○ Example: int number1; Multiple Variable Declaration: [DataType] [variable1], [variable2]; ○ Example: int number1, number2, sum; Constants What is a Constant? A constant is similar to a variable, but its value cannot change once it is assigned. Constants are also called read-only variables. Key Features of Constants: 1. Cannot be changed while the program is running. 2. Declared using the final keyword. 3. By convention, UPPER CASE LETTERS are used for defining constants. Different Types of Operators in Java What are Operators? Operators are symbols used to perform computations, define conditions, or establish relationships between variables. Types of Operators in Java: 1. Arithmetic Operators: Used to perform basic mathematical operations. ○ Symbols: +, -, *, / 2. Comparison (Relational) Operators: Used to compare two values. ○ Symbols: >, =,