Java Oops
Java Oops
Bahria University,
Karachi Campus
Submitted By:
Submitted To:
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");
}
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.
CLASS DIAGRAM:
INPUT:
class item {
private String Name;
private double 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);
OUTPUT:
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;
class Main {
public static void main(String[] args) {
OUTPUT:
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;
public item() {
this("no item", 0.0);
}
@Override
public String toString() {
return name + " @ " + unitPrice;
}
}
@Override
public double getPrice() {
return super.getPrice() * weight;
}
@Override
public String toString() {
return super.toString() + " " + weight + " Kg\t" + getPrice() + "
PKR";
}
}
@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);
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 Circle() {
super();
radius = 0.0;
}
@Override
public void onAreaChange() {
System.out.println("Area of Circle changed.");
}
}
public Triangle() {
super();
base = 0.0;
height = 0.0;
}
@Override
public void onAreaChange() {
System.out.println("Area of Triangle changed.");
}
}
public Rectangle() {
super();
height = 0.0;
width = 0.0;
}
@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);
OUTPUT: