0% found this document useful (0 votes)
14 views9 pages

Cs DJ

The document covers various programming concepts including methods, encapsulation, inheritance, polymorphism, and object-oriented programming principles. It provides code examples and explanations for each concept, illustrating how they are applied in Java. Additionally, it includes practice questions for further understanding and application of the discussed topics.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views9 pages

Cs DJ

The document covers various programming concepts including methods, encapsulation, inheritance, polymorphism, and object-oriented programming principles. It provides code examples and explanations for each concept, illustrating how they are applied in Java. Additionally, it includes practice questions for further understanding and application of the discussed topics.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

1.

public int howManyE(){


int count = 0;
for (int i = 0, i < text.lenght(); i++){
if (text.chart(i) == 'e'){
count++;
}
}
2. The loop iterates over each character in the string and the if statemnt check if the current
character matches 'e' and increments the counter accordingly
3. Parameter variable is a variable used to pass information into a method, it is declared in
the method signature and holds the value of the argument passed when the method is called

1. Vehicle V = new Vehicle( 'X1234567' , )


2. This.broken refers to the instance variable, while 'broken' refers only to the parameter
3. V.setColor(Vehicle.BLACK);
4. - aggregation
- 'ParkingArea' can contain 'Vehicle''s
- One 'ParkingArea' can contain between 0 and 300 instances of 'Vehicle'
5. - static variables are associated with the class itself rather than any specific instance of
the class
- all instances of the class share the same static variable

1. - it can be active( true) or not active(false)


- to save memory
2. public POSline split(POSLine oldLine){
POSLine newLine = new POSLine();
int i = 0;
boolean flag = false;
while((i<20)&&(oldLine.getLine(i)!=null)){
if(flag){
newLine.joinLine(oldLine.leaveLine(i));
}
else i++;
flag = !flag;
}
return newLine;
}
PAPER 2024
10A
an object is an abstract entity;
consists of data/attributes/properties;
has methods/behaviour/actions on;
An object occupies memory;
An object is an instance of a class;
10B
setFuelType(boolean fuelType);
10C
public String getBrandModel(){
return this.brandModel;
}
10D
A default constructor instantiates an object of a class;
with null or default values;
without using any parameter;
10e
fuelType can no longer be boolean but could be other type of data like int or char to
represent distinct values;
for 4 different types of fuel;
10f
inheritance
10g
public class Car extends Rental{
private int numberOfDoors;
public Car(){
this.numberOfDoors = 4;
}
public int getNumberOfDoors(){
return this.numberOfDoors;
}
public void setNumberOfDoors (int i){
this.numberOfDoors = i;
}
}
11a
Inheritance
11b
easier to maintain;
as the changes in the parent class are automatically reflected in the child class;
11c
Encapsulation
Abstraction
Polymorphism
11d
Improves code organisation; smaller manageable modules leading to better logical
organization.
12a
the value is passed when the method is called;
passed as a value or as a reference;
is found in the parameter list of the method definition;
12b
public Car[ ] findBrandModels(Car[ ] allCars){
int count = 0;
Car[ ] result = new Car [100];
for (int i =0; i< allCars.length&&allCars[i] != null; i++){
boolean found = false;
for (int j = 0; j < i; j++){
if(allCars[i].getBrandModel().equals(allCars[j].getBrandModel())){
found = true;
}}
if (!found){
result[count++] = allCars[i];
}
return result;
}
12c
create desiredCars array;
iterate through the result of all findBrandModels();
if an object fulfils the three conditions;
copy the car object in the array desiredCars;
return the ‘desiredCars’ array;
13a
Customer diamencik linia Car
13b
The problem becomes that Car ‘has a’ Customer and Customer ‘has a’ car it may cause
inconsistencies
13b
obligation - to respect privacy;
amplification - only relevant data should be stored that helps the customer;
reason- to limit the impact on privacy.
2019November
10a
Aggregation
10b
POSsystem contains POSline contains Cart
10c
The name of a program element(like method or class);
10d
The class is the template of an object that defines the actions and properties of an object.
Example: Class ‘POSline’ stores instantiated objects of the ‘Cart’ class
10e
private Cart[ ] line = new Cart[20];
10f
public void joinLine(Cart newCart){
int i = 0 ;
while (this.line[i] != null){
i++;
}
this.line[i] = newCart;
}
10g
public Cart leaveLine(int n){
Cart result = this.line[n];
for (int i = n ; i < 19; i++){
this.line[i] = this.line[i+1];
}
this.line[19] = null;
return result;
}
11a
New class is derived from an existing class;
New class inherits all properties and behaviours of other class;
The derived class is called a subclass and the original is called superclass;
11b
It reduces maintenance overhead because you only have to make updates to superclass in
this program to POSline
11c
Saves development time because classes and their methods do not need to be rewritten;
Promotes organization as code will be shorter it will be easier to read;
11d

1. Class and Object (Klasa i Obiekt)


A class is a template for creating objects. It defines the properties (variables) and behaviors
(methods) of the objects.

(Klasa to szablon do tworzenia obiektów. Określa właściwości (zmienne) i zachowania


(metody) obiektów.)

An object is an instance of a class.

(Obiekt to instancja klasy.)

Example (Przykład)

public class Car {


private String brand; // Instance variable (zmienna instancyjna)
private int speed;

// Constructor (Konstruktor)
public Car(String brand, int speed) {
this.brand = brand;
this.speed = speed;
}

// Getter method (Metoda pobierająca)


public String getBrand() {
return brand;
}

// Setter method (Metoda ustawiająca)


public void setSpeed(int speed) {
this.speed = speed;
}

// Method to accelerate the car (Metoda do przyspieszania)


public void accelerate() {
speed += 10;
System.out.println("Speed: " + speed);
}
}

• this refers to the current object. (Odwołuje się do bieżącego obiektu.)

• Getter and Setter methods provide controlled access to private fields.


(Metody getter i setter zapewniają kontrolowany dostęp do prywatnych pól.)

2. Encapsulation (Enkapsulacja)

Encapsulation means restricting access to certain components of an object to protect its


data. This is done using access modifiers like private, public, and protected.
(Enkapsulacja oznacza ograniczenie dostępu do określonych komponentów obiektu w celu
ochrony jego danych.)

Example (Przykład)

public class BankAccount {


private String accountHolder; // Private field (Prywatne pole)
private double balance;

// Constructor (Konstruktor)
public BankAccount(String accountHolder, double balance) {
this.accountHolder = accountHolder;
this.balance = balance;
}

// Public getter (Publiczny getter)


public double getBalance() {
return balance;
}

// Method to deposit money (Metoda wpłaty)


public void deposit(double amount) {
if (amount > 0) {
balance += amount;
System.out.println("Deposited: " + amount);
}
}
}

• Private fields can only be accessed or modified using public methods.

(Prywatne pola mogą być dostępne lub modyfikowane tylko za pomocą metod publicznych.)

3. Inheritance (Dziedziczenie)

Inheritance allows a class to acquire properties and methods from another class using the
extends keyword.

(Dziedziczenie pozwala klasie na przejęcie właściwości i metod innej klasy za pomocą słowa
kluczowego extends.)

Example (Przykład)

// Parent class (Klasa nadrzędna)


public class Animal {
public void sound() {
System.out.println("Animals make sounds.");
}
}
// Child class (Klasa podrzędna)
public class Dog extends Animal {
@Override
public void sound() {
System.out.println("Dog barks.");
}
}

• @Override allows a subclass to provide a specific implementation of a


method from its superclass.

(@Override pozwala klasie podrzędnej dostarczyć własną implementację metody z klasy


nadrzędnej.)

4. Polymorphism (Polimorfizm)

Polymorphism allows one interface or class to represent different types of objects.

(Polimorfizm pozwala jednej klasie lub interfejsowi reprezentować różne typy obiektów.)

It is commonly implemented using method overriding.

(Najczęściej jest implementowany za pomocą nadpisywania metod.)

Example (Przykład)

public class Shape {


public void draw() {
System.out.println("Drawing a shape.");
}
}

public class Circle extends Shape {


@Override
public void draw() {
System.out.println("Drawing a circle.");
}
}

public class Square extends Shape {


@Override
public void draw() {
System.out.println("Drawing a square.");
}
}

// Main method to demonstrate polymorphism


public class Main {
public static void main(String[] args) {
Shape shape1 = new Circle(); // Polymorphism
Shape shape2 = new Square();

shape1.draw(); // Output: Drawing a circle


shape2.draw(); // Output: Drawing a square
}
}

• Polymorphism allows the same method to behave differently based on the


object it is called on.

(Polimorfizm pozwala tej samej metodzie zachowywać się inaczej w zależności od obiektu.)

5. Abstract Classes and Interfaces (Klasy Abstrakcyjne i Interfejsy)

• Abstract Class: A class that cannot be instantiated and may contain abstract
methods.

(Klasa abstrakcyjna: Klasa, której nie można zainicjalizować i może zawierać metody
abstrakcyjne.)

• Interface: A collection of abstract methods that must be implemented by a


class using the implements keyword.

(Interfejs: Zbiór metod abstrakcyjnych, które muszą być zaimplementowane przez klasę
używającą słowa implements.)

Example (Przykład)

// Abstract Class (Klasa Abstrakcyjna)


abstract class Vehicle {
abstract void move();
}

// Interface (Interfejs)
interface Flyable {
void fly();
}

// Concrete Class (Klasa Konkretna)


public class Airplane extends Vehicle implements Flyable {
@Override
public void move() {
System.out.println("Airplane is moving on the runway.");
}

@Override
public void fly() {
System.out.println("Airplane is flying.");
}
}

• Abstract methods do not have a body and must be implemented by the


subclass.

(Metody abstrakcyjne nie mają ciała i muszą być zaimplementowane przez klasę
podrzędną.)

• Interfaces support multiple inheritance, unlike abstract classes.

(Interfejsy obsługują wielodziedziczenie, w przeciwieństwie do klas abstrakcyjnych.)

Practice Questions (Pytania Praktyczne)

1. Design a class representing a Library Book with attributes like title, author,
and status (borrowed/available). Implement methods to borrow and return the book.

(Zaprojektuj klasę reprezentującą książkę biblioteczną z atrybutami jak tytuł, autor i status.
Zaimplementuj metody do wypożyczania i zwracania książki.)

2. Create a parent class called Employee and derive subclasses for Manager
and Developer. Implement methods to calculate salary using different logic for each role.

(Stwórz klasę nadrzędną o nazwie Employee i utwórz podklasy dla Manager i Developer.
Zaimplementuj metody do obliczania pensji z różną logiką dla każdej roli.)

3. Implement a program that uses an interface to simulate different types of


payments like CreditCard and PayPal.

(Zaimplementuj program używający interfejsu do symulacji różnych rodzajów płatności jak


Karta Kredytowa i PayPal.)

Let me know if you’d like to see detailed solutions for any of these!

You might also like