0% found this document useful (0 votes)
5 views

Java Oops

The document describes a lab assignment on object-oriented programming and inheritance. It contains 5 tasks related to inheritance concepts like inheriting behaviors and properties from parent classes to subclasses. Example code is provided for each task to demonstrate inheritance principles.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Java Oops

The document describes a lab assignment on object-oriented programming and inheritance. It contains 5 tasks related to inheritance concepts like inheriting behaviors and properties from parent classes to subclasses. Example code is provided for each task to demonstrate inheritance principles.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 14

[OBJECT ORIENTED PROGRRAMING] [INHERITANCE]

Bahria University,
Karachi Campus

Course: CSC-210– Object Oriented Programming


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

Submitted By:

SHAMRAIZ TASAWAR 89252


(Name) (Reg. No.)

Submitted To:

Engr. Mahawish/Engr. Saniya Sarim

[SHAMRAIZ TASAWAR] [REG #89252]


[OBJECT ORIENTED PROGRRAMING] [INHERITANCE]

Signed Remarks: Score:

Bahria University,
Karachi Campus

LAB NO. 07
______________
LIST OF TASKS
TASK NO OBJECTIVE
01 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
02 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.
03 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.
04 Consider a superclass Items which models customer’s purchases. This class has:
Two private instance variables name (String) and unit Price (double).
05
Write the classes below containing the given instancevariables

Submitted On:
[SHAMRAIZ TASAWAR] [REG #89252]
[OBJECT ORIENTED PROGRRAMING] [INHERITANCE]

31/03/2024
(Date: DD/MM/YY)

LAB # 07
TASK # 01: 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.
CLASS DIAGRAM:

INPUT:
class Main {
public static void main(String[] args) {
aliens a1 = new aliens();
pirates p1 = new pirates();
a1.eat();
a1.talk();
a1.oel();
p1.eat();
p1.oel();
p1.talk();
a1.craw();
a1.fght();
p1.steal();
p1.sail();
}
}

class human {
public void eat() {
System.out.println("Human is eating");
}

[SHAMRAIZ TASAWAR] [REG #89252]


[OBJECT ORIENTED PROGRRAMING] [INHERITANCE]

public void oel() {


System.out.println("Human is driving");
}

public void talk() {


System.out.println("Human is talking");
}
}

class aliens extends human {


public void craw() {
System.out.println("Alien is crawling");
}

public void fght() {


System.out.println("Alien is fighting");
}
}

class pirates extends human {


public void steal() {
System.out.println("Pirate is stealing money");
}

public void sail() {


System.out.println("Pirate is sailing");
}
}

OUTPUT:

TASK # 02: 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.

[SHAMRAIZ TASAWAR] [REG #89252]


[OBJECT ORIENTED PROGRRAMING] [INHERITANCE]

CLASS DIAGRAM:

INPUT:
class item {
private String Name;
private double Price;

public item(String name, double price) {


this.Name = name;
this.Price = price;
}

public String getName() {


return Name;
}

public double getPrice() {


return Price;
}
}

class produce extends item {


public produce(String name, double price) {
super(name, price);
}
}

class fresh extends produce {


public fresh(String name, double price) {
super(name, price);
}
}

class frozen extends produce {


public frozen(String name, double price) {
super(name, price);
}

[SHAMRAIZ TASAWAR] [REG #89252]


[OBJECT ORIENTED PROGRRAMING] [INHERITANCE]

class cosmetic extends item {


public cosmetic(String name, double price) {
super(name, price);
}
}

class pharmacy extends item {


public pharmacy(String name, double price) {
super(name, price);
}
}

class electronicitem extends item {


public electronicitem(String name, double price) {
super(name, price);
}
}

class cloth extends item {


public cloth(String name, double price) {
super(name, price);
}
}

class Main {
public static void main(String[] args) {
fresh mango = new fresh("mango", 15);
frozen bun = new frozen("apple", 5);
cosmetic lipstick = new cosmetic("Lipstick", 50);
pharmacy panadol = new pharmacy("bat", 20);
electronicitem fan = new electronicitem("fan", 600);
cloth coat = new cloth("ball", 100);

System.out.println(mango.getName() + " - RS" + mango.getPrice());


System.out.println(bun.getName() + " - RS" + bun.getPrice());
System.out.println(lipstick.getName() + " - RS" +
lipstick.getPrice());
System.out.println(panadol.getName() + " - RS" + panadol.getPrice());
System.out.println(fan.getName() + " - RS" + fan.getPrice());
System.out.println(coat.getName() + " - RS" + coat.getPrice());
}
}

OUTPUT:

[SHAMRAIZ TASAWAR] [REG #89252]


[OBJECT ORIENTED PROGRRAMING] [INHERITANCE]

TASK # 03: 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
CLASS DIAGRAM:

INPUT:
class cuisine {
protected String name;
protected int quantity;
protected double price;

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


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

public void displayDetails() {


System.out.println("Name: " + name);
System.out.println("Quantity: " + quantity);
System.out.println("Price: RS " + price);
}
}

class pakistani extends cuisine {


public pakistani(String name, int quantity, double price) {
super(name, quantity, price);
}
}

class BBQ extends cuisine {


public BBQ(String name, int quantity, double price) {
super(name, quantity, price);
}
}

class chinese extends cuisine {


public chinese(String name, int quantity, double price) {
super(name, quantity, price);
}
}

[SHAMRAIZ TASAWAR] [REG #89252]


[OBJECT ORIENTED PROGRRAMING] [INHERITANCE]

class fastFood extends cuisine {


public fastFood(String name, int quantity, double price) {
super(name, quantity, price);
}
}

class Beverages extends cuisine {


public Beverages(String name, int quantity, double price) {
super(name, quantity, price);
}
}

class Main {
public static void main(String[] args) {

pakistani biryani = new pakistani("Biryani", 2, 400);


BBQ malaiboti = new BBQ("malai boti", 5, 300);
chinese noodles = new chinese("noodles", 1, 200);
fastFood wings = new fastFood("wings", 4, 280);
Beverages coke = new Beverages("Coke", 1, 150);

System.out.println("----- Pakistani Cuisine -----");


biryani.displayDetails();
System.out.println();
System.out.println("----- BBQ Cuisine -----");
malaiboti.displayDetails();
System.out.println();
System.out.println("----- Chinese Cuisine -----");
noodles.displayDetails();
System.out.println();
System.out.println("----- Fast Food -----");
wings.displayDetails();
System.out.println();
System.out.println("----- Beverages -----");
coke.displayDetails();
}
}

OUTPUT:

[SHAMRAIZ TASAWAR] [REG #89252]


[OBJECT ORIENTED PROGRRAMING] [INHERITANCE]

TASK # 04: 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:
CLASS DIAGRAM:

INPUT:
class item {
private String name;
private double unitPrice;

[SHAMRAIZ TASAWAR] [REG #89252]


[OBJECT ORIENTED PROGRRAMING] [INHERITANCE]

public item(String name, double unitPrice) {


this.name = name;
this.unitPrice = unitPrice;
}

public item() {
this("no item", 0.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;
}
}

class Weighteditem extends item {


private double weight;

Weighteditem(String name, double unitPrice, double weight) {


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

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

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

class counteditem extends item {


private int quantity;

[SHAMRAIZ TASAWAR] [REG #89252]


[OBJECT ORIENTED PROGRRAMING] [INHERITANCE]

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


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

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

@Override
public String toString() {
return super.toString() + " " + quantity + " units\t" + getPrice() + "
PKR";
}
}class NewMain {
public static void main(String[] args) {
Weighteditem apple = new Weighteditem("apple", 2.00, 400);
System.out.println(apple);

counteditem pencils = new counteditem("Pencils", 5, 15);


System.out.println(pencils);
}
}

OUTPUT;

TASK # 05: Write the classes below containing the given instance variables and
methods, following the inherited hierarchy
INPUT:
class Shapes {
protected double area;

public Shapes() {
area = 0.0;
}

public double getArea() {


return area;
}

public void onAreaChange() {

[SHAMRAIZ TASAWAR] [REG #89252]


[OBJECT ORIENTED PROGRRAMING] [INHERITANCE]

}
}

class Circle extends Shapes {


private double radius;
private final double pi = Math.PI;

public Circle() {
super();
radius = 0.0;
}

public void setRadius(double radius) {


this.radius = radius;
calculateArea();
onAreaChange();
}

private void calculateArea() {


area = pi * radius * radius;
}

@Override
public void onAreaChange() {
System.out.println("Area of Circle changed.");
}
}

class Triangle extends Shapes {


private double base;
private double height;

public Triangle() {
super();
base = 0.0;
height = 0.0;
}

public void setBase(double base) {


this.base = base;
calculateArea();
onAreaChange();
}

public void setHeight(double height) {


this.height = height;
calculateArea();
onAreaChange();
}

private void calculateArea() {


area = 0.5 * base * height;
}

@Override
public void onAreaChange() {
System.out.println("Area of Triangle changed.");

[SHAMRAIZ TASAWAR] [REG #89252]


[OBJECT ORIENTED PROGRRAMING] [INHERITANCE]

}
}

class Rectangle extends Shapes {


private double height;
private double width;

public Rectangle() {
super();
height = 0.0;
width = 0.0;
}

public void setHeight(double height) {


this.height = height;
calculateArea();
onAreaChange();
}

public void setWidth(double width) {


this.width = width;
calculateArea();
onAreaChange();
}

private void calculateArea() {


area = height * width;
}

@Override
public void onAreaChange() {
System.out.println("Area of Rectangle changed.");
}
}

class Main {
public static void main(String[] args) {
Circle circle = new Circle();
Triangle triangle = new Triangle();
Rectangle rectangle = new Rectangle();

circle.setRadius(2.0);
triangle.setBase(3.0);
triangle.setHeight(4.0);
rectangle.setWidth(5.0);
rectangle.setHeight(6.0);

System.out.println("Area of Circle: " + circle.getArea());


System.out.println("Area of Triangle: " + triangle.getArea());
System.out.println("Area of Rectangle: " + rectangle.getArea());
}
}

OUTPUT:

[SHAMRAIZ TASAWAR] [REG #89252]


[OBJECT ORIENTED PROGRRAMING] [INHERITANCE]

[SHAMRAIZ TASAWAR] [REG #89252]

You might also like