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

22sp 041 Cs - Lab2

The document contains 6 tasks related to object oriented programming concepts in Java. The tasks include creating classes to calculate the area of a rectangle, average of numbers, inheritance, method overloading, polymorphism, and abstract classes. Code solutions to each task are provided that demonstrate concepts like encapsulation, inheritance, polymorphism, abstraction etc.

Uploaded by

Subul Raza
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
48 views

22sp 041 Cs - Lab2

The document contains 6 tasks related to object oriented programming concepts in Java. The tasks include creating classes to calculate the area of a rectangle, average of numbers, inheritance, method overloading, polymorphism, and abstract classes. Code solutions to each task are provided that demonstrate concepts like encapsulation, inheritance, polymorphism, abstraction etc.

Uploaded by

Subul Raza
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

DATA STRUCTURES & ALGORITHMS

#02
Object Oriented Programming

Student Name: SUBUL RAZA


Roll Number: 22SP-041-CS Section: A
Work submitted on:

Maximum Marks Performance Viva Total


Marks Obtained
Remarks (if any)

Experiment evaluated by

Instructor Name:

Signature:

OOP Concepts Related Tasks


Object Oriented Programming | Page 2

Task 1

Write a program to print the area of a rectangle by creating a class named


'Area' having two methods. The first method named 'setDim' takes the length
and breadth of the rectangle as parameters and the second method named
'getArea' returns the area of the rectangle. The length and breadth of the
rectangle are entered through the keyboard.

import java.util.*;

public class Lab2 {


    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("Please enter Length");
        int L = sc.nextInt();
        System.out.println("Please enter Breadth");
        int b = sc.nextInt();
        Area obj1 = new Area();
        obj1.SetDim(L, b);
        System.out.println("The Area of Triangle is " +
obj1.getArea());

    }
}

public class Area {


    int area;

    public void SetDim(int l, int b) {


        area = l * b;
    }

    public int getArea() {


        return area;
    }
}

Task 2:

Print the average of three numbers entered by user by creating a class


named 'Average' having a method to calculate and print the average.

EL-213 Data Structures and Algorithms


Object Oriented Programming | Page 3

import java.util.*;

public class Lab2 {


    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter first number");
        int a = sc.nextInt();
        System.out.println("Enter second number");
        int c = sc.nextInt();
        System.out.println("Enter third number");
        int b = sc.nextInt();
        Average obj1 = new Average();
        System.out.println("The Area of Triangle is " +
obj1._Average(a, b, c));

    }
}
public class Average {
    double average;

    public double _Average(int a, int b, int c) {


        average = (a + b + c) / 3;
        return average;
    }
}

Task 3:

Create a class named 'Member' having the following members:

Data members

1 - Name

2 - Age

3 - Phone number

4 - Address

EL-213 Data Structures and Algorithms


Object Oriented Programming | Page 4

5 - Salary

It also has a method named 'printSalary' which prints the salary of the
members.

Two classes 'Employee' and 'Manager' inherits the 'Member' class. The
'Employee' and 'Manager' classes have data members 'specialization' and
'department' respectively. Now, assign name, age, phone number, address
and salary to an employee and a manager by making an object of both of
these classes and print the same.

import java.util.*;

public class Lab2 {


    public static void main(String[] args) {
        // Scanner sc = new Scanner(System.in);

        Employee employee = new Employee("Alice", "xyz", "6595", 25,


2500, "abc");
        Manager manager = new Manager("Diana", "abc", "00", 30, 50000,
"xyz");

        // manager
        System.out.print("----Manager---");
        System.out.println("Name:" + manager.name);
        System.out.println("Address:" + manager.Address);
        System.out.println("Age:" + manager.age);
        System.out.println("Phone Number:" + manager.Phonenumber);
        System.out.println("Specialization:" + manager.specialization);
        System.out.println("Salary:" + manager.Salary);
        // Employee
        System.out.print("----Employee---");
        System.out.println("Name:" + employee.name);
        System.out.println("Address:" + employee.Address);
        System.out.println("Age:" + employee.age);
        System.out.println("Phone Number:" + employee.Phonenumber);
        System.out.println("Specialization:" + employee.department);
        System.out.println("Salary:" + employee.Salary);

    }
}
public class Member {
    String name, Address, Phonenumber;
    int age, Salary;

    public int printSalary() {


        return Salary;

    }

EL-213 Data Structures and Algorithms


Object Oriented Programming | Page 5

    public Member(String name, String address, String phonenumber, int


age, int salary) {
        this.name = name;
        Address = address;
        Phonenumber = phonenumber;
        this.age = age;
        Salary = salary;
    }

public class Employee extends Member {


    String department;

    public Employee(String name, String address, String phonenumber,


int age, int salary, String department) {
        super(name, address, phonenumber, age, salary);
        this.department = department;
    }

public class Manager extends Member {


    String specialization;

    public Manager(String name, String address, String phonenumber, int


age, int salary, String specialization) {
        super(name, address, phonenumber, age, salary);
        this.specialization = specialization;
    }

EL-213 Data Structures and Algorithms


Object Oriented Programming | Page 6

Task 4:

Create a class to print an integer and a character with two methods having
the same name but different sequence of the integer and the character
parameters.
For example, if the parameters of the first method are of the form (int n, char
c), then that of the second method will be of the form (char c, int n).

import java.util.*;

public class Lab2 {


    public static void main(String[] args) {
        // Scanner sc = new Scanner(System.in);
        Task4 obj1 = new Task4();
        obj1.print(5, 'B');
        obj1.print('B', 5);

    }
}
public class Task4 {
    int n;
    char x;

    void print(int n, char x) {


        System.out.println(n + " " + x);

    }

    void print(char x, int n) {


        System.out.println(x + " " + n);

    }
}

Task 5:

Suppose a class 'A' has a static method to print "Parent". Its subclass 'B' also
has a static method with the same name to print "Child". Now call this method
by the objects of the two classes. Also, call this method by an object of the
parent class refering to the child class i.e. A obj = new B()

import java.util.*;

public class Lab2 {

EL-213 Data Structures and Algorithms


Object Oriented Programming | Page 7

    public static void main(String[] args) {


        A obj1 = new A();
        A obj2 = new B();
        obj1.Print();
        obj2.Print();
    }
}
public class A {
    public static void Print() {
        System.out.println("Parent");

    }
}

public class B extends A {


    public static void Print() {
        System.out.println("Child");
    }
}

Task 6:

We have to calculate the percentage of marks obtained in three subjects


(each out of 100) by student A and in four subjects (each out of 100) by
student B. Create an abstract class 'Marks' with an abstract method
'getPercentage'. It is inherited by two other classes 'A' and 'B' each having a
method with the same name which returns the percentage of the students.
The constructor of student A takes the marks in three subjects as its
parameters and the marks in four subjects as its parameters for student B.
Create an object for eac of the two classes and print the percentage of marks
for both the students.

import java.util.*;

public class Lab2 {


    public static void main(String[] args) {
        Marks marksA = new A(80, 50, 75);
        Marks marksB = new B(90, 75, 80, 56);
        System.out.println("Marks of Student A is " +
marksA.getPercentage() + "%");
        System.out.println("Marks of Student B is " +
marksB.getPercentage() + "%");

EL-213 Data Structures and Algorithms


Object Oriented Programming | Page 8

    }
}
public class A extends Marks {
    int a, b, c;

    public A(int a, int b, int c) {


        this.a = a;
        this.b = b;
        this.c = c;
    }

    @Override
    public double getPercentage() {
        return ((a + b + c) / 3.0);

    }
}

public class B extends Marks {


    int a, b, c, d;

    public B(int a, int b, int c, int d) {


        this.a = a;
        this.b = b;
        this.c = c;
        this.d = d;
    }

    @Override
    public double getPercentage() {
        return ((a + b + c + d) / 4.0);
    }

public abstract class Marks {


    public abstract double getPercentage();

EL-213 Data Structures and Algorithms

You might also like