Deck Flashcards
(41 cards)
What does it mean if it says “Extend” in the code?
It means it’s inheritance.
What is the general nature of an object? [2]
An object is an abstract identity;
And it’s components are data;
What are two disadvantages of OOP? [4]
OOP are larger than other programs;
Thus slower;
Unsuitable for minor/small projects;
As OOP increases complexity for little gain;
Modularity (concept)
When you break down a system into modular components, you can work on specific parts separately. This approach is known as modular development or modular programming.
Outline one advantage of modularity [2]
Faster development;
Since different programming teams can work together on different modules;
Easier to debug;
Because the smaller modules will have fewer mistakes than one big program;
Relationships between classes (definition concept)
In Object-Oriented Programming (OOP), there are several types of relationships between classes. Let’s break them down in easy terms:
Association:
Easy Explanation: Think of association as a connection between two classes. It’s like saying one class knows about another.
Example: A Person class might be associated with an Address class, meaning a person has an address.
Aggregation:
Easy Explanation: Aggregation is a special type of association where one class is part of another class.
Example: A University class may have many Student classes. The students are part of the university, but they can exist independently.
Composition:
Easy Explanation: Composition is a strong form of aggregation where the child class is so dependent on the parent class that it cannot exist independently.
Example: An Engine class is composed within a Car class. If the car is destroyed, the engine is gone too.
Inheritance:
Easy Explanation: Inheritance is like saying one class is a specialized version of another class. It allows a class to inherit attributes and behaviours from another class.
Example: A Dog class could inherit from an Animal class. The dog is a type of animal and shares some common characteristics.
Dependency:
Easy Explanation: Dependency occurs when one class relies on another class, but changes in the dependent class don’t necessarily affect the class that depends on it.
Example: A Car class might depend on a Fuel Tank class. If you change how the fuel tank works, it doesn’t necessarily change the car’s basic functionality.
Relationships in OOP (in code)
Association:
public class Person {
String name;
public Person(String name) { this.name = name; } }
public class Address {
String street;
String city;
public Address(String street, String city) { this.street = street; this.city = city; } }
// Association
public class AssociationExample {
public static void main(String[] args) {
Person person = new Person(“John”);
Address address = new Address(“123 Main St”, “Cityville”);
person.address = address; // Person knows about Address } } Aggregation:
import java.util.ArrayList;
import java.util.List;
public class University {
String name;
List<Student> students = new ArrayList<>(); // Aggregation</Student>
public University(String name) { this.name = name; } }
public class Student {
int studentId;
String name;
public Student(int studentId, String name) { this.studentId = studentId; this.name = name; } }
// Aggregation
public class AggregationExample {
public static void main(String[] args) {
University university = new University(“ABC University”);
Student student1 = new Student(1, “Alice”);
Student student2 = new Student(2, “Bob”);
university.students.add(student1); university.students.add(student2); } } Composition:
public class Engine {
public void start() {
System.out.println(“Engine started”);
}
}
public class Car {
Engine engine = new Engine(); // Composition
public void start() { engine.start(); } }
// Composition
public class CompositionExample {
public static void main(String[] args) {
Car car = new Car();
car.start(); // Starting the car also starts its engine
}
}
Inheritance:
public class Animal {
String species;
public Animal(String species) { this.species = species; } public void makeSound() { // Abstract method } }
public class Dog extends Animal {
public Dog(String species) {
super(species);
}
@Override public void makeSound() { System.out.println("Woof!"); } }
// Inheritance
public class InheritanceExample {
public static void main(String[] args) {
Dog dog = new Dog(“Canine”);
System.out.println(dog.species); // Accessing inherited attribute
dog.makeSound(); // Accessing overridden method
}
}
Dependency:
public class FuelTank {
public void refill() {
System.out.println(“Fuel tank refilled”);
}
}
public class Car {
FuelTank fuelTank; // Dependency
public Car(FuelTank fuelTank) { this.fuelTank = fuelTank; } public void drive() { System.out.println("Car is driving"); } }
// Dependency
public class DependencyExample {
public static void main(String[] args) {
FuelTank fuelTank = new FuelTank();
Car car = new Car(fuelTank);
car.drive(); // Car depends on FuelTank for driving
fuelTank.refill(); // Changes to FuelTank don’t affect Car
}
}
Outline one advantage of binary search [1]
Binary search is much faster than sequential search;
Because it halves the search range for every comparison;
Outline one disadvantage of binary search [1]
Not applicable to unsorted data;
Outline the use of data hiding as a use of a security feature in OOP [2]
Encapsulation allows making instance variables of a class private;
So that other classes can’t access accidentally;
Outline why having more than one method with the same name does not cause a conflict? [2]
Because they both have different parameters;
Allowing the compiler to choose the right one;
What is the name for this type of method: why having more than one method with the same name does not cause a conflict? [1]
Polymorphism;
Define the term method signature [2]
The method name and the type of all of its parameters;
Is the unique identifiers (concept)
Outline an advantage of using a library [2]
Convenience;
Because implementations of common tasks are available;
Reliability;
Because these implementations are fully developed, functional and robust;
Advantages and disadvantages of dynamic linked lists and static arrays.
check one note
Code that instantiates array “line” with 20 cart objects [2]
private Cart[] line = new Cart([20];
Concept:
Imagine a Shelf:
Think of your array like a shelf where you want to store some items. The type of items you want to store is like the type of things you want to put on the shelf.
Specify the Shelf:
Say out loud what type of items your shelf will hold. For example, “I want a shelf for Cart items.” In Java, this sounds like Cart[].
Name the Shelf:
Give your shelf a name. For example, “I’ll call it ‘line’.” In Java, this becomes Cart[] line.
Get a New Shelf:
Use the word “new” to ask for a brand new shelf. In Java, it’s new.
Tell the Size of the Shelf:
Specify how many items your shelf can hold. For example, “I want it to hold 20 Cart items.” In Java, this is done with square brackets, like new Cart[20].
Put the Shelf in its Place:
Finally, tell Java where to put your shelf. Connect it to the name you chose earlier. For example, “Put the ‘line’ shelf where I can access it.” In Java, this is Cart[] line = new Cart[20];.
Distinguish between a class and an instantiation [2]
A class is the template of an object; OR the blue print of an object;
Is the creation of the actual object;
What is “this.” used in java for?
this is a keyword in Java used to refer to the current object. In this context, this.line[i] means the line array of the current object
Define the term inheritance [2]
A new class is derived from an existing class;
The new class inherits all data of the other class;
How to use flags? (concept)
Use flags as true or false, and make sure to type them as booleans;
Also make sure to put them at the end of each loop as not true or if(flag) #checks if true
To skip an element
Define the term object reference [2]
A pointer to a memory location;
Where the object is stored;
How do you find the first object in linked list?
Use the head; eg. head.getNex()
As the head always points towards the first object.
How do you traverse through linked list?
// Loop until reaching the nth node (no need to check for null)
while (i < n) {
prev = curr; // Save the current node as the previous node
curr = curr.getNext(); // Move to the next node
i++;
}
Explain the importance of using coding style and naming conventions when programming [4]
Indentation;
Annotations;
Helps understanding;
For easier maintenance;