Option D Study Guide
Option D Study Guide
Summary
[Click a link below to access the relevant section]
1. Vocabulary Bank
2. Data Types
3. Programming Languages, Code Quality, and Open-Source
4. OOP Overview
5. Instantiation
6. Inheritance
7. Aggregation
8. Polymorphism
9. Encapsulation
10. Static Methods & Attributes
11. UML and Relationship Diagrams
12. LinkedLists (HL)
13. 2D Arrays (HL)
14. Stacks (HL)
15. Recursion (HL)
16. Binary Search Trees (HL)
Sold 1 to
[email protected]
Vocabulary Bank
- Object - unit of data that contents variables and methods
- Class - Template (blueprint) for object including variables and methods
- Identifier - name that is used to identify a variable, method, class, or other programming
entity
- Object Reference - A variable that is used to access an object; the variable contains the
memory address of the object
- Object Instantiation - create an instance of an object from a class
- Attribute - variable or method
- Method - another word for a function - independent block of code that can be called on
to return an output given a specific range of inputs
- Method Signature - Included the name of the function and all its parameters - generally
the first line of the function
- Mutator Method - a method that is used to modify an attribute (the state) of an object
- Accessor Method - a method that is used to access and attribute (the state) of an object
- Method Overriding - feature that allows a subclass to provide a different implementation
of a method that is already defined in its superclass
- Method Overloading - a feature that allows for different implementations of a method in
the same class
- Polymorphism - a concept in object-oriented programming (OOP) that allows objects of
different types to be treated as if they were of the same type
- Encapsulation - refers to the idea of bundling data and methods that operate on that data
within a single unit, known as a class
- Aggregation - a type of relationship between objects in Java where one object contains
one or more instances of another class as part of its own internal state
- UML (Uniform Modeling Language) Diagram - Used to describe the structure of a system,
including its classes, attributes, and relationships
- Modularity - the practice of organizing code into smaller, independent units, known as
modules or packages.
- Access Modifier - control the level of access that other parts of the program have to a
particular class, method, or variable
- Inheritance - allows one class to inherit the properties and behaviors of another class
- Constructor - a special method that is called when an object of a class is created;
initializes the object's instance variables and prepares it for use.
- Dependency - where one class depends on another class to perform its functionality -
i.e. a Car object must have an Engine object
2
Data Types
3
- Benefits
- Easier for other programmers to understand your work
- Easier to debug consistent, commented code
- Easy for other programmers to maintain your code
- Open-Source Code - refers to software code that is made available to the public
and released under a license that grants users the right to view, modify, and
distribute the code.
Pros Cons
4
OOP (Object-Oriented Programming) Overview
- OOP (Object-Oriented Programming)
- OOP is a method of organizing information in a computer program that seeks to
model the real world.
- It organizes data by category.
- Objects represent a unit of data that can be passed around a program
- A class is a template from which an object is created and determines whether
information needs to be in the object.
- Each object has variables and functions associated with it called attributes and
behaviors, respectively.
- OOP Pros & Cons
5
Anatomy of a Class
6
Instantiation
- Instantiation
- The act of creating an object from a template
- The constructor is invoked in the process
- Constructor - a special method that is used to initialize an instance of a class
- When a new instance of a class is created, the constructor method is
automatically called to initialize the object and prepare it for use
- Has the same name as the class itself
- Does not have a return type
- May take parameters to allow the caller to provide initial values for the object's
properties
7
Instantiation (Example)
// Constructor method
public Employee(String name, int employeeId, double salary) {
this.name = name;
this.employeeId = employeeId;
this.salary = salary;
this.complaints = new String[10];
System.out.println("New object created");
}
...
}
// Instantiation
Employee employeeOne = new Employee("Walter", 356, 1000000);
// Example of object method usage
employeeOne.addComplaint("Scarily smart.");
8
Instantiation (Diagram)
Inheritance
- Inheritance
- Allows new classes to be created based on existing classes, thus inheriting their
properties and behavior
- Existing class is called the superclass or parent class, and the new class is called
the subclass or child class
- extends keyword is used to create a subclass of a superclass
- Inheritance Pros & Cons
9
Inheritance (Example)
...
}
10
Inheritance (Diagram)
11
Aggregation
- Aggregation
- A relationship between classes where one class contains an instance of another
class as a member variable
- When that class is instantiated, we end up with an object that contains other
objects
- If the “container object” is destroyed, the “contained object” will still exist
- Inheritance vs. Aggregation
12
Aggregation (Example)
13
Aggregation (Diagram)
14
Polymorphism
- Polymorphism
- A situation in which something can occur in different form, and we can therefore
perform the same action in many different ways
- In Java, refers to method overloading and method overriding.
- method overloading - situation in which there are different methods in the
same class with the same name, but which have different parameters
- method overriding - situation in which there are different methods in the
superclass and subclass with the same name, but which have different
parameters
- These can all be used, with parameters being the distinguishing factor.
15
Method Overriding (Example)
...
}
...
16
Encapsulation
- Encapsulation
1) the technique of binding data and functions together in a single unit (class) and
controlling access to that unit from outside
2) refers to the practice of hiding the internal details of an object from the outside
world
- Access modifiers
- Used to control access to attributes, methods, constructors, and sometimes,
classes
- private - can only be accessed and modified from within the same class in which
it was defined, and cannot be accessed from outside the class, including by
instances of that class
- protected - an be accessed and modified from within the same class in which it
was defined, as well as by any subclass of that class
- public - can be accessed and modified by any code that has a reference to an
instance of the class, as well as from within the class itself
17
Static Methods & Attributes
- Static Methods & Attributes
- Means that the method or attribute is the same for all instances of a class
- Defined the class level
- Contained in the classes rather an instance of the class (object)
- Less memory is taken up because memory is allocated for only one instance,
rather than every objects
18
UML (Uniform Modeling Language) Diagrams
- Classes - Contains 3 components
1) Class Name
2) Attributes
3) Behaviors (Methods)
19
- Relationships Diagram - Describes the relationship between classes/objects in a program
20
LinkedLists (HL)
- LinkedLists
- Java-specific data structure
- Doubly-linked list (although sometimes referred to as a singly-linked list on the
exam)
- Each piece of data is stored in a node, with pointers
- Located non-contiguously in memory - addresses of nodes are spread out across
RAM and nodes only linked by pointers
- Ideal for frequent insertion or deletion of elements, or when the size of the list is
unknown
- Ideal Use Cases
- Implementing a Queue - front of the queue is represented by the head of
the Linked List, and the rear of the queue is represented by the tail of the
Linked List.
- Implementing a Stack - top of the stack is represented by the head of the
Linked List, and the bottom of the stack is represented by the tail of the
Linked List
- Large Data Sets (due to dynamic memory allocation)
21
Example 2: Display Contents of Linked List
22
Example 5: Transfer Contents of a LinkedList to an Array (Using an Iterator)
23
Example 7: Remove an Object Based on Some Criteria
24
ArrayLists vs. LinkedLists
25
Recursion (HL)
- Tips for Using Recursion
1. Understand that recursion is used when we are repeating the same behavior with
consistently changing parameters.
2. Model the problem with an example input and output.
3. Turn the problem into a set of individual steps.
4. Keep in mind that at least one of the function parameters is going to have to
change on each recursive function call.
5. Understand that the base case is where the recursive calls will stop - it effectively
stops the function from calling itself
- Recursion Pros & Cons
26
2D Arrays (HL)
Example 1: Initializing and Creating a 2D Array
int[][] grades = {{99, 75, 68, 21, 98},
{100, 89, 73, 55, 77},
{100, 45, 99, 89, 71},
{99, 75, 89, 99, 100},
{89, 100, 54, 65, 99}};
27
Stacks (HL)
Example 1: Create and Initialize Stack
28
Example 3: Filter Out Contents of Stack By Some Criteria
29
- Ideal Use Cases
- Undo/Redo functionality - Any action that is done in an application is pushed on to
a stack and can be popped to undo and action or pushed again to redo an action
- Expression evaluation - Each time a new operator or operand is encountered, it is
pushed onto the stack. When an operator is encountered, the operands are
popped off the stack, the operation is performed, and the result is pushed back
onto the stack.
- Function call stack - Each time a function is called, its parameters and local
variables are pushed onto the stack. When the function returns, the values are
popped off the stack and control returns to the calling function.
30
Example 1: Creating and Initializing a Binary Tree
31
- Binary Search Trees Pros & Cons
32