[go: up one dir, main page]

0% found this document useful (0 votes)
9 views12 pages

Fiza Oop 9

This document outlines the lab assignments for the Object Oriented Programming course at Bahria University, Karachi Campus, for Spring 2024. It includes tasks related to Java programming concepts such as composition, association, and aggregation, along with sample code implementations and UML class diagrams. The document is submitted by Fiza Khan and includes a detailed index of lab experiments and their objectives.

Uploaded by

fizajamshed4
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views12 pages

Fiza Oop 9

This document outlines the lab assignments for the Object Oriented Programming course at Bahria University, Karachi Campus, for Spring 2024. It includes tasks related to Java programming concepts such as composition, association, and aggregation, along with sample code implementations and UML class diagrams. The document is submitted by Fiza Khan and includes a detailed index of lab experiments and their objectives.

Uploaded by

fizajamshed4
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

Bahria University,

Karachi Campus

Course: CSC-210 - Object Oriented Programming


Term: Spring 2024, Class: BSE- 2(C)

Submitted By:

Fiza Khan 89219


(Name) (Reg. No.)

Submitted To:

Engr. Mahawish/Engr. Saniya Sarim

Signed Remarks: Score:


INDEX
SNO DATE LAB LAB OBJECTIVE SIGN
NO
1 16/2/2024 1 Introduction To Java Programming Language

2 23/2/2024 2 Implementation Of Class and Object In OOP

3 1/3/2024 3 Access Modifiers in Java

4 8/3/2024 4 Constructors in Java


5 15/3/2024 5 Introduction to Static Variables and Methods
6 22/3/2024 7 Inheritance
7 26/4/2024 8 Polymorphism
8 3/5/2024 9 Association, Aggregation and Composition
Bahria University,
Karachi Campus

LAB EXPERIMENT NO.


9
LIST OF TASKS
TASK NO OBJECTIVE
1 Write a program and create the objects of classes in class car to explain the concept
of composition. Create several classes as engine, doors, capacity and wheel having
their individual methods attributes. The object of these classes are created in a car
class and they are set as public. The object of this car class is created in Main method
and this with the help of this object we can call other classes as well and can use
their functionalities and design UML class diagram.

3 A company manages many stores. Each Store contains many Products. Implement
Product, Store and Company classes using association and aggregation concepts
and design UML class diagram.

Submitted On:
9/5/2024
(Date: DD/MM/YY)
3/5/2024 Object Oriented Programming
[Association , Aggregation and Composition]

LAB # 09
Task # 01:
Write a program and create the objects of classes in class car to explain the concept of composition. Create
several classes as engine, doors, capacity and wheel having their individual methods attributes. The object
of these classes are created in a car class and they are set as public. The object of this car class is created
in Main method and this with the help of this object we can call other classes as well and can use their
functionalities and design UML class diagram

Solution:
Main class:
package fizalab9_task1;

public class FizaLab9_Task1 {

public static void main(String[] args) {


Car c1=new Car(70);
c1.brake();
c1.display();
System.out.println("-------------------------------------");
c1.engine.start();
System.out.println("-------------------------------------");
c1.doors.open();
System.out.println("-------------------------------------");
c1.capacity.addPassengers(3);
System.out.println("-------------------------------------");
c1.wheels.spin();
System.out.println("-------------------------------------");
}

Car class:
package fizalab9_task1;

public class Car {


public final Engine engine;
public final Doors doors;
public final Capacity capacity;
public final Wheels wheels;
public double speed;

public Car(double speed) {


engine = new Engine("E8");
doors = new Doors(false);
capacity = new Capacity(5);
wheels = new Wheels("Suzuki");

FIZA KHAN 1
3/5/2024 Object Oriented Programming
[Association , Aggregation and Composition]

this.speed=speed;
}

public double getSpeed() {


return speed;
}

public void brake() {


this.speed -= 5;
}

public void display() {


System.out.println("Engine: Type - " + engine.getType());
System.out.println("Doors: Is Open - " + doors.isIsOpen());
System.out.println("Capacity: Value - " + capacity.getValue());
System.out.println("Wheels: Brand - " + wheels.getBrand());
System.out.println("Speed: " + speed);
}

Engine class:
package fizalab9_task1;

public class Engine {

private String type;

public Engine(String type) {


this.type = type;
}

public String getType() {


return type;
}

public void start() {


System.out.println("Engine class method:");
System.out.println("Engine starting====>");
}
}

Doors class:
package fizalab9_task1;

public class Doors {


private boolean isOpen;

public Doors(boolean isOpen) {


this.isOpen = isOpen;
}

public boolean isIsOpen() {


return isOpen;

FIZA KHAN 2
3/5/2024 Object Oriented Programming
[Association , Aggregation and Composition]

public void open() {


System.out.println("Doors class method:");
if (isOpen==false) {
System.out.println("Opening the door====>");
isOpen = true;
} else {
System.out.println("The door is already open.");
}
}
}

Capacity class:
package fizalab9_task1;

public class Capacity {

private int value;

public Capacity(int value) {


this.value = value;
}

public int getValue() {


return value;
}

public void addPassengers(int passengers) {


System.out.println("Capacity class method:");
if (passengers <= value) {
System.out.println(passengers + " passengers added.");
} else {
System.out.println("Not enough space for " + passengers + " passengers.");
}
}
}

Wheels class:
package fizalab9_task1;

public class Wheels {


private String brand;

public Wheels(String brand) {


this.brand = brand;
}

public String getBrand() {


return brand;
}

public void spin() {


System.out.println("Wheels class method:");
System.out.println("Wheels spinning==>");
}
}

FIZA KHAN 3
3/5/2024 Object Oriented Programming
[Association , Aggregation and Composition]

Output:

UML:

Task # 03:
A company manages many stores. Each Store contains many Products. Implement Product, Store and
Company classes using association and aggrigation concepts and design UML class diagram.

FIZA KHAN 4
3/5/2024 Object Oriented Programming
[Association , Aggregation and Composition]

Task # 03:
A company manages many stores. Each Store contains many Products. Implement Product, Store and
Company classes using association and aggrigation concepts and design UML class diagram.

Solution:
Main class:
package fizalab9_task3;
import java.util.*;

public class Main {

public static void main(String[] args) {


Product p1=new Product("Juice",200,"24/4/2024");
Product p2=new Product("Water",100,"1/9/2024");
Product p3=new Product("Bread",150,"15/12/2024");
Product p4=new Product("Friuts",500,"30/1/2024");

List<Product> drinks = new ArrayList<Product>();


drinks.add(p1);
drinks.add(p2);

List<Product> food = new ArrayList<Product>();


food.add(p3);
food.add(p4);

Store s1=new Store("Walmart", "London", "Jim",drinks);


Store s2=new Store("Imtaiz", "Karachi", "Ali",food);

List<Store> Stores = new ArrayList<Store>();


Stores.add(s1);
Stores.add(s2);

Company company=new Company("LVMH",5000000, Stores);


for (Store store : Stores) {
System.out.println("Store Name: " + store.getName());
System.out.println("Location: " + store.getLoc());
System.out.println("Owner: " + store.getOwner());
for (Product product : store.getProducts()) {
System.out.println("Products:");
System.out.println("===================================");
System.out.println("Product Name: " + product.getName());
System.out.println("Price: " + product.getPrice());
System.out.println("Expiry: " + product.getExpiry());
System.out.println("------------------------------------");
}
}

FIZA KHAN 5
3/5/2024 Object Oriented Programming
[Association , Aggregation and Composition]

System.out.println("Total products in company========>");


System.out.println(company.getTotalProducts());
}

Product class:
package fizalab9_task3;

public class Product {

private String name;


private double price;
private String expiry;

public Product(String name, double price, String expiry) {


this.name = name;
this.price = price;
this.expiry = expiry;
}

public String getName() {


return name;
}

public double getPrice() {


return price;
}

public String getExpiry() {


return expiry;
}

public double total(int quantity) {


return price * quantity;
}
}

Store class:
package fizalab9_task3;
import java.util.List;

public class Store {


private String name;
private String loc;
private String owner;
private List<Product> products;

public Store(String name, String loc, String owner, List<Product>


products) {

FIZA KHAN 6
3/5/2024 Object Oriented Programming
[Association , Aggregation and Composition]

this.name = name;
this.loc = loc;
this.owner = owner;
this.products = products;
}

public String getName() {


return name;
}

public String getLoc() {


return loc;
}

public String getOwner() {


return owner;
}

public List<Product> getProducts() {


return products;
}

Company class:
package fizalab9_task3;

import java.util.*;
public class Company {
private String name;
private double revenue;
private List<Store> stores;

public Company(String name, double revenue, List<Store> stores) {


this.name = name;
this.revenue = revenue;
this.stores = stores;
}

public String getName() {


return name;
}

public double getRevenue() {


return revenue;
}

public List<Store> getStores() {


return stores;
}

FIZA KHAN 7
3/5/2024 Object Oriented Programming
[Association , Aggregation and Composition]

public int getTotalProducts()


{
int noofproducts=0;
List <Product>products;
for(Store st: stores)
{
products=st.getProducts();
for(Product p : products)
{
noofproducts++;
}
}
return noofproducts;
}
}

Output:

FIZA KHAN 8
3/5/2024 Object Oriented Programming
[Association , Aggregation and Composition]

UML:

FIZA KHAN 9

You might also like