0% found this document useful (0 votes)
19 views24 pages

Fiza Oop 7

This document outlines the lab exercises for the Object Oriented Programming course at Bahria University, Karachi Campus, for Spring 2024. It includes tasks focused on Java programming concepts such as inheritance, class design, and method implementation. The document provides detailed instructions for various programming assignments involving class hierarchies and object-oriented principles.

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)
19 views24 pages

Fiza Oop 7

This document outlines the lab exercises for the Object Oriented Programming course at Bahria University, Karachi Campus, for Spring 2024. It includes tasks focused on Java programming concepts such as inheritance, class design, and method implementation. The document provides detailed instructions for various programming assignments involving class hierarchies and object-oriented principles.

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/ 24

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
Bahria University,
Karachi Campus

LAB EXPERIMENT NO.


7
LIST OF TASKS
TASK NO OBJECTIVE
1 Write the classes below containing the given instance variables and methods,
following the inherited hierarchy.
2 Write a program that inherits a class named Alien and Pirates from a parent class
Human. The human class has its own features like, Human can sleep, walk, talk etc.
the Alien and Pirates class inheriting these functionalities as well as they have their
characteristics, thus explaining the concepts of inheritance.
3 Write a program that inherits a class named Produce, Cosmetics, Pharmacy,
electronic Item and Cloth from a parent class Item. The Item class has its own
features like, name and price etc. the Child classes inheriting these functionalities
as well as they have their characteristics, thus explaining the concepts of
inheritance. Chile classes like Produce, can have their own child classes i.e., Frozen
and Fresh.
4 Write a program that inherits a class named Pakistani, BBQ, Chines, Fast Food and
Beverages etc. from a parent class Cuisines. The Cuisines class has its own features
like, name, quantity and price etc. the Child classes inheriting these functionalities
as well as they have their characteristics, thus explaining the concepts of
inheritance. Child classes can have their own child classes.
5 Write code according to given guide.You must draw a class diagram first to start
writing your code. Consider a superclass Items which models customer’s purchases.

Submitted On:
28/3/2024
(Date: DD/MM/YY)
22/3/2024 Object Oriented Programming
[Inheritance]

LAB # 07
Task # 01:
Write the classes below containing the given instance variables and methods,
following the inherited hierarchy:

Solution:
Main class:
package lab7_task1;

public class Shape {


private double area;

public Shape(){
this.area=0.0;
}

public double getArea() {


return area;
}

public void setArea(double area) {


this.area = area;

YOUR NAME 1
22/3/2024 Object Oriented Programming
[Inheritance]

}
public void onAreaChange(){

}
}

Shape class:
package lab7_task1;

public class Shape {


private double area;

public Shape(){
this.area=0.0;
}

public double getArea() {


return area;
}

public void setArea(double area) {


this.area = area;
}
public void onAreaChange(){

}
}

Circle class:
package lab7_task1;

public class Circle extends Shape{


private double radius;
private double PI=3.142;

public Circle(){
super();
}

public void setRadius(double radius) {


this.radius = radius;
}
@Override
public void onAreaChange(){
setArea(PI*Math.pow(radius, 2));
System.out.println("\nArea of circle: "+getArea());
}
}

Triangle class:
package lab7_task1;

public class Triangle extends Shape {


private double base;

YOUR NAME 2
22/3/2024 Object Oriented Programming
[Inheritance]

private double height;

public Triangle() {
super();
}

public void setBase(double base) {


this.base = base;
}

public void setHeight(double height) {


this.height = height;
}
@Override
public void onAreaChange(){
setArea(0.5*base*height);
System.out.println("\nArea of circle: "+getArea());
}

Triangle class:
package lab7_task1;

public class Rectangle extends Shape{


private double height;
private double width;

public Rectangle() {
super();
}

public void setHeight(double height) {


this.height = height;
}

public void setWidth(double width) {


this.width = width;
}

@Override
public void onAreaChange() {
setArea(height*width);
System.out.println("\nArea of Rectangle: "+getArea());

YOUR NAME 3
22/3/2024 Object Oriented Programming
[Inheritance]

Output:

YOUR NAME 4
22/3/2024 Object Oriented Programming
[Inheritance]

Task # 02:
Write a program that inherits a class named Alien and Pirates from a parent class
Human. The human class has its own features like, Human can sleep, walk, talk
etc. the Alien and Pirates class inheriting these functionalities as well as they
have their characteristics, thus explaining the concepts of inheritance.

Solution:
Main class:
package lab7_task2;

public class Lab7_Task2 {

public static void main(String[] args) {


Alien a1=new Alien();
System.out.println("_______ALIEN________");
a1.setName("Vexor");
a1.setWeight(32.5);
a1.setColor("Green");
System.out.println(a1);
a1.ShapeShift("Human");
System.out.println("\nHUMAN INHERITED BY ALIEN:");
a1.Sleep();
a1.Walk();
a1.Talk();
System.out.println("\n_______PIRATES_______");
Pirates p1=new Pirates();
p1.setName("Captain Jack");
p1.setWeight(59);
p1.setShip("Delta");
System.out.println(p1);
p1.fight();
System.out.println("\nHUMAN INHERITED BY PIRATES:");
p1.Sleep();
p1.Walk();
p1.Talk();
}

Human class:
package lab7_task2;

public class Human {


protected String name;
protected double weight;

protected String getName() {


return name;

YOUR NAME 5
22/3/2024 Object Oriented Programming
[Inheritance]

protected void setName(String name) {


this.name = name;
}

protected double getWeight() {


return weight;
}

protected void setWeight(double weight) {


this.weight = weight;
}

protected void Sleep(){


System.out.println("Human can sleep");
}
protected void Walk(){
System.out.println("Human can walk");
}
protected void Talk(){
System.out.println("Human can talk");
}
@Override
public String toString() {
return "Name: " + this.name + "\nWeight: " + this.weight;
}

Alien class:
package lab7_task2;

public class Alien extends Human{


protected String color;

protected String getColor() {


return color;
}

protected void setColor(String color) {


this.color = color;
}

protected void ShapeShift(String form){


System.out.println("Tranforming into "+form+"!");
}

@Override
public String toString() {
return super.toString()+"\nColor: " + this.color;
}

YOUR NAME 6
22/3/2024 Object Oriented Programming
[Inheritance]

Pirates class:
package lab7_task2;

public class Pirates extends Human{


protected String ship;

protected String getShip() {


return ship;
}

protected void setShip(String ship) {


this.ship = ship;
}

protected void fight() {


System.out.println("Engaging in a fierce battle!");
}

@Override
public String toString() {
return super.toString()+"\nShip: " + ship;
}
}

Output:

YOUR NAME 7
22/3/2024 Object Oriented Programming
[Inheritance]

Task # 03:
Write a program that inherits a class named Produce, Cosmetics, Pharmacy,
electronic Item and Cloth from a parent class Item. The Item class has its own
features like, name and price etc. the Child classes inheriting these
functionalities as well as they have their characteristics, thus explaining the
concepts of inheritance. Chile classes like Produce, can have their own child
classes i.e., Frozen and Fresh.

Solution:
Main class:
package lab7_task3;

public class Lab7_Task3 {

public static void main(String[] args) {


System.out.println("____________FROZEN_____________");
Frozen f1=new Frozen("Cheese",900,"12/5/2024","31/3/2024","Ziplock bag");
f1.Display();
System.out.println("-------------------------------");
System.out.println("\n__________COSMETICS___________");
Cosmetics c1=new Cosmetics("Maybelline",6000,"21/8/2024","Foundation");
c1.Display();
System.out.println("-------------------------------");
System.out.println("\n_______ELECTRONIC ITEM________");
ElectronicItem e1=new ElectronicItem("Juicer",12000,"1/2/2024",12);
e1.Display();
System.out.println("-------------------------------");
}

Item class:
package lab7_task3;

public class Item {


protected String name;
protected double price;
protected String expiry;

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


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

YOUR NAME 8
22/3/2024 Object Oriented Programming
[Inheritance]

public void Display(){


System.out.println("Name: "+name);
System.out.println("Price: "+price);
System.out.println("Expiry date: "+expiry);
}
}

Produce class:
package lab7_task3;

public class Produce extends Item {

protected String manDate;

public Produce(String name, double price, String expiry, String manDate) {


super(name, price, expiry);
this.manDate=manDate;
}

@Override
public void Display(){
super.Display();
System.out.println("Manufacturing Date: "+manDate);
}

Frozen class:
package lab7_task3;

public class Frozen extends Produce {

protected String packType;

public Frozen(String name, double price, String expiry, String manDate, String
packType) {
super(name, price, expiry, manDate);
this.packType=packType;
}

@Override
public void Display(){
super.Display();
System.out.println("Packaging Type: "+packType);
}

Fresh class:
package lab7_task3;

public class Fresh extends Produce{


protected String cond;

YOUR NAME 9
22/3/2024 Object Oriented Programming
[Inheritance]

public Fresh(String name, double price, String expiry, String manDate, String
cond) {
super(name, price, expiry, manDate);
this.cond = cond;
}

@Override
public void Display() {
super.Display();
System.out.println("Condition: "+cond);
}

Cosmetics class:
package lab7_task3;

public class Cosmetics extends Item{


protected String type;

public Cosmetics(String name, double price, String expiry, String type) {


super(name, price, expiry);
this.type = type;
}

@Override
public void Display() {
super.Display();
System.out.println("Type: "+type);
}

Pharmacy class:
package lab7_task3;

public class Pharmacy extends Item{

protected String brand;

public Pharmacy(String name, double price, String expiry, String brand) {


super(name, price, expiry);
this.brand = brand;
}

@Override
public void Display() {
super.Display();
System.out.println("Brand name: "+brand);
}

ElectronicItem class:

YOUR NAME 10
22/3/2024 Object Oriented Programming
[Inheritance]

package lab7_task3;

public class ElectronicItem extends Item{

protected int warranty;

public ElectronicItem(String name, double price, String expiry, int warranty) {


super(name, price, expiry);
this.warranty = warranty;
}

@Override
public void Display() {
super.Display();
System.out.println("Warranty: "+warranty);
}

Cloth class:
package lab7_task3;

public class Cloth extends Item{

protected String material;

public Cloth(String name, double price, String expiry, String material) {


super(name, price, expiry);
this.material = material;
}

@Override
public void Display() {
super.Display();
System.out.println("Material: "+material);
}

YOUR NAME 11
22/3/2024 Object Oriented Programming
[Inheritance]

Output:

Task # 04:
Write a program that inherits a class named Pakistani, BBQ, Chines, Fast Food
and Beverages etc. from a parent class Cuisines. The Cuisines class has its own
features like, name, quantity and price etc. the Child classes inheriting these
functionalities as well as they have their characteristics, thus explaining the
concepts of inheritance. Child classes can have their own child classes.

Solution:
Main class:
package lab7_task4;

public class Lab7_Task4 {

public static void main(String[] args) {


Pakistani p1=new Pakistani("Katakat",3,700,"Asian");
System.out.println(p1);
System.out.println("-----------------------------------");
Burger b1=new Burger("Crispy Zinger",2,500,"US",1);
System.out.println(b1);
System.out.println("-----------------------------------");
BBQ b2=new BBQ("Bihari boti",4,800,"Spicy");

YOUR NAME 12
22/3/2024 Object Oriented Programming
[Inheritance]

System.out.println(b2);
System.out.println("-----------------------------------");
Beverages b3=new Beverages("Cola Next",2,300,"Cold");
System.out.println(b3);
System.out.println("-----------------------------------");
}

Cuisines class:
package lab7_task4;

public class Cuisines {


protected String name;
protected int quantity;
protected double price;

public Cuisines(String name,int quantity,double price){


this.name=name;
this.quantity=quantity;
this.price=price;
}

@Override
public String toString() {
return "Name: " + name + "\nQuantity: " + quantity + "\nPrice: " + price;
}

Pakistani class:
package lab7_task4;

public class Pakistani extends Cuisines{


protected String type;

public Pakistani(String name, int quantity, double price, String type) {


super(name, quantity, price);
this.type = type;
}

@Override
public String toString() {
return super.toString() + "\nType: "+type;
}

BBQ class:
package lab7_task4;

public class BBQ extends Cuisines{


protected String taste;

YOUR NAME 13
22/3/2024 Object Oriented Programming
[Inheritance]

public BBQ(String name, int quantity, double price, String taste) {


super(name, quantity, price);
this.taste = taste;
}

@Override
public String toString() {
return super.toString() + "\nTaste: "+taste;
}

Chinese class:
package lab7_task4;

public class Chinese extends Cuisines{

protected String sideline;

public Chinese(String name, int quantity, double price, String sideline) {


super(name, quantity, price);
this.sideline = sideline;
}

@Override
public String toString() {
return super.toString() + "\nSide Line: "+sideline;
}

FastFood class:
package lab7_task4;

public class FastFood extends Cuisines{


protected String origin;

public FastFood(String name, int quantity, double price, String origin) {


super(name, quantity, price);
this.origin = origin;
}

@Override
public String toString() {
return super.toString() + "\nOrigin: "+origin;
}

Burger class:
package lab7_task4;

public class Burger extends FastFood{


protected int noofpersons;

YOUR NAME 14
22/3/2024 Object Oriented Programming
[Inheritance]

public Burger(String name, int quantity, double price, String origin, int
noofpersons) {
super(name, quantity, price, origin);
this.noofpersons = noofpersons;
}

@Override
public String toString() {
return super.toString() + "\nNo of Persons: "+noofpersons;
}
}

Beverages class:
package lab7_task4;

public class Beverages extends Cuisines{


protected String temp;

public Beverages(String name, int quantity, double price, String temp) {


super(name, quantity, price);
this.temp = temp;
}

@Override
public String toString() {
return super.toString() + "\nTemperature: "+temp;
}

Output:

YOUR NAME 15
22/3/2024 Object Oriented Programming
[Inheritance]

Task # 05:
Write code according to given guide. You must draw a class diagram first to
start writing your code.

Consider a superclass Items which models customer’s purchases. This class has:

 Two private instance variables name (String) and unitPrice (double).

 One constructor to initialize the instance variables.

 A default constructor to initialize name to “no item”, and unitPrice to 0.

use this()

 A method getPrice that returns the unitPrice.

 Accessor and mutator methods.

 A toString method to return the name of the item followed by @

symbol, then the unitPrice.

Consider two subclasses WeighedItem and CountedItem. WeighedItem has an

additional instance variable weight (double) in Kg while CountedItem has an

additional variable quantity (int) both private.

 Write an appropriate constructor for each of the classes making use

of the constructor of the superclass in defining those of the

subclasses.

 Override getPrice method that returns the price of the purchasedItem

based on its unit price and weight (WeighedItem), or quantity

(CountedItem). Make use of getPrice of the superclass

 Override also toString method for each class making use of the

toString method of the superclass in defining those of the subclasses.

toString should return something that can be printed on the receipt.

For example

YOUR NAME 16
22/3/2024 Object Oriented Programming
[Inheritance]

Banana @ 3.00 1.37 Kg 4.11 PKR (in case of WeighedItem class)

Pens @ 4.5 10 units 45 PKR (in case of CountedItem class)

Class diagram:

Item

- name: String
- unitPrice: double

+ Item(name: String,
unitPrice: double)
+ Item()
+ getName(): String
+ setName(name: String):
void
+ getUnitPrice(): double
+ setUnitPrice(price:
double): void
+ getPrice(): double
+ toString(): String

WeighedItem CountedItem

- weight: double - quantity: int

+ WeighedItem(name: String, + CountedItem(name: String,


unitPrice: double, weight: unitPrice: double, quantity:
double) int)
+ getWeight(): double + getQuantity(): int
+ setWeight(weight: + setQuantity(quantity:
double): void int): void
+ getPrice(): double + getPrice(): double
+ toString(): String + toString(): String

Solution:
Main class:
package lab7_task5;

public class Lab7_Task5 {

public static void main(String[] args) {


WeighedItem w=new WeighedItem("Pencils",100,20);
CountedItem c=new CountedItem("Makeup",3000,3);

YOUR NAME 17
22/3/2024 Object Oriented Programming
[Inheritance]

System.out.println("__________________RECEIPT_____________________");
System.out.println(w);
System.out.println(c);
}

Items class:
package lab7_task5;

public class Items {


private String name;
private double unitPrice;

public Items(String name, double unitPrice) {


this.name = name;
this.unitPrice = unitPrice;
}
public Items() {
this("no item",0);
}
public double getPrice(){
return unitPrice;
}

public String getName() {


return name;
}

public void setName(String name) {


this.name = name;
}

public double getUnitPrice() {


return unitPrice;
}

public void setUnitPrice(double unitPrice) {


this.unitPrice = unitPrice;
}

@Override
public String toString() {
return name + " @ " + unitPrice;
}

WeighedItem class:
package lab7_task5;

public class WeighedItem extends Items {


private double weight;

public WeighedItem(String name, double unitPrice,double weight) {

YOUR NAME 18
22/3/2024 Object Oriented Programming
[Inheritance]

super(name, unitPrice);
this.weight = weight;
}

@Override
public double getPrice() {
return super.getPrice()*weight;
}

public double getWeight() {


return weight;
}

public void setWeight(double weight) {


this.weight = weight;
}

@Override
public String toString() {
return super.toString()+" "+weight+" Kg "+getPrice()+" PKR";
}
}

CountedItem class:
package lab7_task5;

public class CountedItem extends Items{


private int quantity;

public CountedItem(String name, double unitPrice, int quantity) {


super(name, unitPrice);
this.quantity = quantity;
}

@Override
public double getPrice() {
return super.getPrice()*quantity;
}

public int getQuantity() {


return quantity;
}

public void setQuantity(int quantity) {


this.quantity = quantity;
}

@Override
public String toString() {
return super.toString()+" "+quantity+" units "+getPrice()+" PKR";
}

YOUR NAME 19
22/3/2024 Object Oriented Programming
[Inheritance]

Output:

YOUR NAME 20

You might also like