Documente Academic
Documente Profesional
Documente Cultură
Fahad Siddiqui
Roll #: 206
Section : E
LAB # 8 :
TASK 1:
Write a program that creates a calculator using inheritance..
Source Code:
package inheritence;
class Calculator
{ int x,y;
void display()
{System.out.println("calculation:"); }}
class Sum extends Calculator
{ void sum()
{System.out.println("sum of x and y: "+(x+y));
}}class Min extends Calculator
{ void min()
{System.out.println("subtraction of x and y: "+(x-y));
}}class Div extends Calculator
{ void div()
{System.out.println("division of x and y: "+(x/y));
}}class Mul extends Calculator
{ void mul()
{System.out.println("multiplication of x an d y: "+(x*y));
}}
public class Inheritence {
public static void main(String[] args) {
Calculator c=new Calculator();
Sum s=new Sum();// TODO code application logic here
Min mi=new Min();
Div d=new Div();
Mul m=new Mul();
s.x=10;
s.y=20;
mi.x=30;
mi.y=25;
d.x=50;
d.y=25;
m.x=4;
LAB # 8 :
m.y=4;
c.display();
s.sum();
mi.min();
d.div();
m.mul();
}}
Output:
TASK 2:
Write a program that calculate area of different shapes using polymorphism.
Source Code:
package shapes_polymorphism;
class Figure {
double no1;
Figure(double n)
{
this.no1=n;
}
void Area()
{System.out.println("use to print area of diffeerent shapes:");
}}
LAB # 8 :
{
Circle(double no)
{super(no);
}
void Area()
{
System.out.println("area of circle is: "+(Math.PI*Math.pow(no1, 2)));
}
}
class Rectangle extends Figure {
double no2;
Rectangle(double x, double y)
{
super(x);
this.no2=y;
}
void Area()
{ System.out.println("area of rectangle is: "+(no1*no2));}
}
class Square extends Figure{
Square(double no3)
{ super(no3);
}
void Area()
{System.out.println("area of square is: "+(Math.pow(no1, 2)));
}}
public class Shapes_polymorphism {
public static void main(String[] args) {
Figure f = new Figure(3);
Circle c = new Circle(21);
Square s = new Square(22);
Rectangle r = new Rectangle(6,7);
f=r;
r.Area();
f=c;
c.Area();
f=s;
LAB # 8 :
s.Area();
}}
Output:
TASK 3:
Write a program that creates Greeting card using polymorphism.
Source Code:
package greet_card;
class Cards
String str1,str2;
{ this.str1=s1;
this.str2=s2;
void cards()
{System.out.println(" "+str1);
System.out.println("MSG=_________________");
LAB # 8 :
System.out.println(" "+str2);
super(s4,s5);
void bwish()
System.out.println("\n");
System.out.println("************************************");
System.out.println(" "+str1);
System.out.println("HAPPY BIRTHDAY....:)");
System.out.println(" "+str2);
System.out.println("************************************");
System.out.println("\n");
LAB # 8 :
Eidwishes(String s7,String s8)
super(s7,s8);
void ewish()
System.out.println("\n");
System.out.println("************************************");
System.out.println(" "+str1);
System.out.println("EID MUBARAK....:)");
System.out.println(" "+str2);
System.out.println("************************************");
System.out.println("\n");
super(s10,s11);
void wwish()
LAB # 8 :
System.out.println("\n");
System.out.println("************************************");
System.out.println(" "+str1);
System.out.println(" "+str2);
System.out.println("************************************");
System.out.println("\n");
c=c;
c.cards();
c=b;
b.bwish();
c=e;
e.ewish();
Weddingcards w=new Weddingcards("TO= Mr. & Mrs. Ali Aqdas","FROM= Fahad Siddiqui");// TODO
code application logic here
LAB # 8 :
c=w;
w.wwish();
}}Output: