[go: up one dir, main page]

0% found this document useful (0 votes)
51 views7 pages

22-SE-096 Lab Report 9

Download as pdf or txt
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 7

OBJECT ORIENTED PROGRAMMING

Lab Report #09

Submitted To:
Sir Talha Saeed
Submitted By:
Talha Ghafoor

Roll Number:
22-SE-096
Task #01
Write a program that consists of a class “Polygon”. This class contains a
member function: void displayResult
Derive two classes “Rectangle” and “Triangle” from it and override the
displayResult method in them. Calculate the area of rectangle
(width*height) and triangle
(width*height/2) and then display it. The output of the program should be like:

Code :
import java.util.Scanner;

class Polygon{
float aera , length ,width;
Scanner sc=new Scanner(System.in);
void displayResult(){

}
class Rectangle extends Polygon{
void calculate(){
System.out.println(" enter a length : ");
length = sc.nextFloat();
System.out.println(" enter a length : ");
width = sc.nextFloat();
aera = length * width;

}
void displayResult(){
System.out.println(" Aera of RECTANGLE is : "+ aera);
}

}
class Triangle extends Polygon{
void calculate(){
System.out.println(" enter a length : ");
length = sc.nextFloat();
System.out.println(" enter a length : ");
width = sc.nextFloat();
aera = (length * width)/2;

}
void displayResult(){
System.out.println(" Aear of TIANGLE Class is "+ aera);
}
}

public class lab_09_task01 {


public static void main(String[] args) {
Rectangle r1 = new Rectangle(); Triangle
t1 = new Triangle();
r1.calculate(); r1.displayResult();
t1.calculate();
t1.displayResult();

}
}
Output :-
Task #02 :-
In this task you are going to implement the concept of polymorphism.
Create a class named Person o A getName() method.
Create a class named Student o
Student inherits the Person Class. o
Override getName() method.
Create a class named Professor o
Professor inherits the Person Class.
o Override getName() method.
In the MainClass, you have to create one method:
A main method, in which you create object of a Student and a Teacher and print
student and teacher name with reference of Student and Teacher.This class must have
inputmarks() function to get its data (marks in three subjects) public class A {
public A() { System.out.println("I'm A"); }
}
public class B extends A {
public B() { System.out.println("I'm B"); }
}
public class C extends B {
public C() { System.out.println("I'm C"); } }
What does this print out?
C x = new C();
from the user. Then calculate the total marks and average marks in the three
subjects. Create another function show_detail() to display the marks in three
subjects, total marks and average marks.
The output of the program should be like:

Code :-
package Java_projects;
import java.util.Scanner;

class person{
Scanner sc = new Scanner(System.in);
void getname(){
}}
class student01 extends person{
void getname(){
System.out.println(" \tSTUDENT DETAIL ");
System.out.print(" enter your name of Student ");
String name = sc.next();
System.out.println(" person name is "+ name);
}}
class professor extends person{
void getname(){
System.out.println(" \tPROFESSOR DETAIL ");
System.out.print(" enter your name of professor ");
String name = sc.next();
System.out.println(" person name is "+ name);
}}
public class lab_09_task02 { public
static void main(String[] args) {
student01 s1 = new student01();
professor p1 = new professor();
s1.getname();
p1.getname();
}
}
Output :-

Task #03
In this task you are going to implement the concept of polymorphism.
Create a parent class named Shape o
A draw( ) method.
Create a class named Circle o
Circle inherits the Shape class.
o Override draw() method.
Create a class named Square
Square extends the Shape class.
o Override draw() method.

Code :-
class shape{
void draw(){

}}
class circle extends shape{
void draw(){
System.out.println(" It is a Circle");
}
}
class Square extends shape{
void draw(){
System.out.println(" It is a Square " );
}
}
public class lab9_task03 {
public static void main(String[] args) {
circle c1= new circle();
Square s1 =new Square();
c1.draw();
s1.draw();
}
}
Output :

THE END…!

You might also like