Object Oriented

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 17

Assignment 1

Student’s Name: pius kyalo

Adm number: BIT/2022/51475

Institutional Affiliation: mku

Course: information technology

Instructor’s Name: Daniel nganga

Date: 20th June 2024


1.Explain the three major types of relationships which can be identified among objects in

object oriented system development. Give a suitable example for each.

1.1 Association

Association is a relationship where objects are related to each other without implying ownership.

It represents a bi-directional relationship where both objects can interact with each other.

Example:

Library and Book A library contains books, and books are found in a library.

Diagram

+------------+ +-------+

| Library |<>---------<| Book |

+------------+ +-------+

1.2 Inheritance

Inheritance is a relationship where one class (child) inherits attributes and methods from another

class (parent). It represents an "is-a" relationship.

Example

Animal and Dog: A dog is an animal, so the Dog class inherits from the Animal class.
Diagram

+---------+

| Animal |

+---------+

+---------+

| Dog |

+---------+

1.3 Aggregation/Composition

Aggregation is a relationship where one class is a part of another class but can exist

independently. Composition is a stronger form where the part cannot exist without the whole.

Example

Car and Engine (Composition): An engine is a part of a car, and it cannot exist

independently without the car.


Classroom and Students (Aggregation): A classroom has students, but students can exist

without the classroom.

Diagram

Aggregation:

+-----------+ +--------+

| Classroom |<>-------->| Student|

+-----------+ +--------+

Composition:

+------+<>--------+-------+

| Car |<>--------| Engine|

+------+ +-------+

2.Object oriented systems development uses both an activity diagrams and an interaction

diagrams. Explain the purpose of each.


Interaction diagrams model how objects interact with each other to achieve a specific

functionality. The most common types are sequence diagrams and collaboration diagrams.

Purpose:

 Depict the sequence of messages exchanged between objects.

 Illustrate the interaction and collaboration among objects.

 Show the time ordering of messages and how operations are carried out.

Example (Sequence Diagram)

User System Database

| | |

|----Login------>| |

| |---Query------->|

| |<--Result-------|

|<---Success---- | |

| | |
3.Using a suitable example explain how objects communicate in object orientation.

Objects communicate by sending messages to each other, invoking methods or functions that are

defined in their class.

Example:

Consider a Bank Account class with a withdraw method. A Customer object may call the

withdraw method of the Bank Account object to withdraw money.

Classes:

class BankAccount {

double balance;

public void withdraw (double amount) {

if (balance >= amount) {

balance -= amount;

System.out.println("Withdrawal successful. New balance: " + balance);


} else {

System.out.println("Insufficient funds.");

class Customer {

String name;

BankAccount account;

public void make Withdrawal(double amount) {

account.withdraw(amount);

Diagram:

+-----------+ +-------------+
| Customer | | BankAccount |

+-----------+ +-------------+

| - name | | - balance |

| - account |<----->| |

+-----------+ +-------------+

| +make Withdrawal () | +withdraw () |

+-----------+ +-------------+

4.Explain what the following perspectives used in drawing class diagrams capture

Conceptual Perspective

The conceptual perspective captures the high-level concepts and relationships in the problem

domain without focusing on implementation details. It models real-world entities and their

associations.

Example

Class diagram representing a university with classes like Student, Professor, and Course.

Specification Perspective
The specification perspective details the interfaces, methods, and interactions between classes. It

focuses on how the system will be designed rather than how it will be implemented.

Example

Class diagram showing methods and properties of Student, Professor, and Course classes.

Implementation Perspective

The implementation perspective is concerned with the actual implementation details, including

data types, method signatures, and class relationships.

Example

Class diagram with specific data types and access specifiers for Student, Professor, and

Course classes.

4.Define an abstract class and explain its importance to inheritance.


An abstract class is a class that cannot be instantiated and often contains abstract methods

(methods without implementation) that must be implemented by subclasses. It provides a base

for other classes to build upon and ensures a common interface.

Importance

 Encapsulation of common functionality: Allows sharing common code among related

classes.

 Enforces a contract: Subclasses must implement abstract methods, ensuring consistent

behavior.

 Promotes code reuse: Avoids duplication by allowing subclasses to inherit common

properties and methods

5. A motor dealer makes an order for various items. An order comprises a list of required

parts each identified by part-number, name-of-item, and item-price. It is possible to

perform operations like adding an item in the order list, deleting an item from the list and

printing the total value of the order.

Required:

a) Identify the class (es) in the above description and construct a class diagram showing

any relationship, the operations and data members.

class Order {

List<Item> items;
void addItem(Item item) {

items.add(item);

void deleteItem (Item item) {

items.remove(item);

double getTotalValue() {

double total = 0;

for (Item item: items) {

total += item.getPrice();

return total;

}
class Item {

String part Number;

String nameOfItem;

double itemPrice;

double getPrice() {

return itemPrice;

6. Use the following case to identify Classes and Operations. Associate the operations with

the appropriate classes.

Computerized Telephone Book for a university

• The telephone book should contain entries for each person in the university

community--student, professor, and staff member.

• Users of the directory can look up entries. In addition, the administrator of the

telephone book can, after supplying a password, insert new entries, delete existing entries,
modify existing entries, print the telephone book, and print a listing of all students or of all

faculty.

identified Classes

 Person (base class for Student, Professor, Staff)

 Telephone Book

Class Diagram

class Person {

String name;

String phoneNumber;

class Student extends Person {

class Professor extends Person {


}

class Staff extends Person {

class TelephoneBook {

List<Person> entries;

void addEntry(Person person) {

entries.add(person);

void deleteEntry(Person person) {

entries.remove(person);

}
void modifyEntry(Person person) {

// Logic to modify entry

void printBook() {

// Logic to print all entries

void printStudents() {

// Logic to print only students

void printFaculty() {

// Logic to print only faculty

}
Diagram

+-----------------+

| Person |

+-----------------+

| - name |

| - phoneNumber |

+-----------------+

+-----------------+

| Student |

+-----------------+

+-----------------+

| Professor |

+-----------------+
^

+-----------------+

| Staff |

+-----------------+

+-----------------------+

| TelephoneBook |

+-----------------------+

| - entries |

| +addEntry() |

| +deleteEntry() |

| +modifyEntry() |

| +printBook() |

| +printStudents() |

| +printFaculty() |

+-----------------------+

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy