Full Transcript

CP1: STAGES OF ADT Modeling Data: Composite Data Types Data Static ADTs: Rarely change or remain constant. ❖ Arrays...

CP1: STAGES OF ADT Modeling Data: Composite Data Types Data Static ADTs: Rarely change or remain constant. ❖ Arrays Dynamic ADTs: Frequently change. Store data of the same type in contiguous memory Information collected to convey knowledge about cells. a world or environment. Examples: Allow direct access to each element by its index. Abstract Data Types (ADT) Stacks: Follow Last-In-First-Out (LIFO). ❖ Linked Lists Queues: Follow First-In-First-Out (FIFO). Ordered collection of data where each element, or A model or structure representing a type of data. Trees: Organize data hierarchically. node, links to the next. The data’s attributes (e.g., size, values). ❖ Strings Graphs: Represent connections between nodes. The functions that can be performed on the data Sequence of characters with a specific length. (e.g., add, remove, update). Data Structures Used to store text data. ADTs can be implemented in many programming ❖ Structs ❖ Purpose: languages, commonly as classes. Group multiple data types into a single structure. Define relationships between data and support Stages of Abstract Data Types (ADT) ADT operations. Allow the creation of instances that represent specific entities. 1. Specification Types of Data Structures: ❖ Classes: Define the components, properties, and ❖ Simple Data Structures (Primitive Data Types): Define both data (attributes) and operations relationships of the data. Integer (methods) as a unified type. Identify operations for the data (what the data can Float Instances of classes are called objects, which do). Boolean encapsulate both state and behavior. Character Algorithms 2. Representation Plan how to store instances of the ADT. (Data A sequence of finite instructions that define how Structure) to solve a specific problem. Often initially written in pseudocode. 3. Implementation Data structures, together with algorithms, form programs. Define how each operation will be performed with Functions return values after processing. the data. (Algorithm) Subroutines perform tasks with results as side effects. ❖ Components Classes: CREATING AN OBJECT Input Values Serve as blueprints for creating objects. An object is created from a class using the `new` keyword Output Values An instance of a class is formed by specific objects in Java. Problem Solved based on that class's blueprint. Steps to Create an Object: Properties: BASIC FEATURES OF OOP ❖ Declaration: Declare a variable with the object type. ❖ Finiteness: Completes after a limited number of steps. ❖ Abstraction ❖ Instantiation: Use the `new` keyword to create the ❖ Absence of Ambiguity: Steps are clear and process of hiding complexities and presenting only object. understandable. the essential details of a system to the user. ❖ Initialization: Call a constructor to initialize the new ❖ Sequence Definition: Steps are in a logical order. ❖ Encapsulation: object. ❖ Input/Output Definition: Both input data and desired binding of data and methods within a class, output are specified. restricting access to protect the object's state. ❖ Scope Definition: Focus is on the specified problem area. Abstraction solves the problem in the design side while, ❖ Effectiveness: Executes each step reliably within a finite time. Encapsulation is the implementation. ❖ Efficiency: Optimizes resources like time and memory. ❖ Inheritance: ------------------------------------------------------------------------------ One class (child or subclass) acquires the Constructors: properties (data members) and methods CP2: THE BASICS OF OOP (functionalities) of another class (parent or Initialize class instances, sharing the class name; a superclass). default one is provided if not defined. Object-Oriented Programming (OOP) Once a method is defined in the parent class, it is Methods: Models real-world characteristics and behaviors, automatically available to all child classes. allowing for the design of applications and ❖ Polymorphism: Collections of code designed to perform specific software through a more intuitive structure. The ability of an operation to exhibit different tasks, written once but reusable multiple times behaviors based on the data passed to it. during program execution. Objects: "many forms." Types of Methods basic entity of object-oriented programming Purpose: Allows the same method to be language. implemented in various ways depending on the Predefined Methods: Available in the standard Represent entities with distinct characteristics object type. library for immediate use (e.g., `sqrt()`, `print()`). (attributes) and behaviors (methods). User-Defined Methods: Created by developers to CP3: RECURSIVE ALGORITHM A recursive method breaks down a task into perform specific tasks. smaller subtasks to solve the problem more efficiently. Making Method Call Key Components of Recursion: Invocation: ▪ The program temporarily exits the current Base Case: method to execute the called method, ▪ The simplest version of the problem, returning afterward. which provides a straightforward solution. Passing Arguments: “Recursion is a way of solving a big problem using the Recursive Step: ▪ The calling method provides arguments (values solution in its subproblems leading to a simpler and ▪ uses smaller instances of the same from variables/constants) to the called elegant solution.” problem to build toward the solution. method. Iterative Algorithm: COMPUTING A PROBLEM Parameter Agreement: ▪ The number and type of arguments must Given a non-negative integer n, the factorial of n An algorithm that repeatedly executes a block of match the parameters in the called method. (denoted as n!) is the product of all positive code through loops until a specified condition is met. integers less than or equal to n. CLASSES IN JAVA Each cycle of the loop's execution is referred to as Local Variables: an iteration. Defined inside methods, constructors, or blocks. Recursive Algorithm: Declared and initialized within the method. An algorithm is considered recursive if it involves a Instance Variables: procedure that calls itself at least once. Declared within a class but outside any method. Recursive Call: Accessible from any method, constructor, or block o The invocation of the `recurse()` method within the class. within itself. Stopping Condition: Class Variables: o A condition that must be defined within Declared within a class using the `static` keyword. the method to terminate recursive calls Shared across all instances of the class. and prevent infinite loops. ------------------------------------------------------------------------------ Iterative Solution Solutions to the Given Problem Let n = 5 5! = 5 * 4 * 3 * 2 * 1 = 120 ARRAY INITIALIZATION ------------------------------------------------------------------------------ Recursive Solution CP4.1: LEARNING ARRAY DATA STRUCTURES PT. 1 Arrays widely used data structure. can hold multiple values of the same type under a single name. Fixed Length: Arrays have a fixed length, meaning Subscript: 0,1,2,3,4… (starts at 0) the size cannot be changed once defined. Syntax: Arrays are declared using square Element: 1,2,3,4… (starts at 1) brackets[]. Example: ARRAY DECLARATION num at = 75 subscript of 5th element = subscript of 63 = what is the 6th element = 80 DIMENSIONS OF ARRAYS IN JAVA 1D Array A single-dimensional array can be treated as either a row or a column. It uses only one subscript for indexing. ▪ Syntax: int x[] = new int; 2D ARRAY REPRESENTATION COLUMN MAJOR REPRESENTATION ▪ Example: String name[] = {"Sam", ROW MAJOR REPRESENTATION FORMULA: Start + k ([(i-1)] + [j-1]m) "Manuel", "Jaimie"}; FORMULA: Start + k ([(i-1)n] + [j-1]) 2D Array row column what is = 9 what is subscript of 15 = 1D ARRAY REPRESENTATION Given the 3x2 array named M and each element of the Given the 3x2 array named M and each element of the array is two words long. Compute for the starting address array is two words long. Compute for the starting address of element M. of element M. m=3 i=3 m=3 i=3 n=2 j=1 n=2 j=2 k=2 k=2 CP5: LEARNING LINKED LIST DATA STRUCTURE SINGLY-LINKED LIST DOUBLY-LINKED LIST LINKED LIST each node contains a pointer that identifies the each node has pointers to both its successor (next next element in the list. node) and predecessor (previous node). An ordered collection of data elements where each Structure: Structure: element contains a reference (link) to the next o Each node has two fields: o Each node contains three fields: element. ▪ Data: Stores the value. ▪ Data NODE ▪ Next Pointer: Points to the next ▪ Next Pointer node in the list. ▪ Previous Pointer Each element in a linked list. Characteristics: Characteristics: Node Structure: o Contains only one link to a single successor o Nodes can be navigated both forward and ▪ Data: Contains the value or information. (the next node). backward. ▪ Link: Points to the next node in the sequence. o Traversal is typically done in one direction o Allows more flexible traversal compared to HEAD (from head to tail). singly-linked lists. A pointer/reference to the first node in the linked list. LAST NODE The last node in the linked list points to NULL, indicating the end of the list. CIRCULARLY-LINKED LIST all nodes are connected in such a way that the last node points back to the first node, forming a circular chain. Types: o Singly Circular Linked List: Each node points to the next, with the last node linking back to the first. o Doubly Circular Linked List: Each node points to both its successor and predecessor, with the last linking to the first and the first linking to the last.

Use Quizgecko on...
Browser
Browser