0% found this document useful (0 votes)
326 views6 pages

Oop Lab Report

This lab report discusses solving two inheritance problems in Java. The student created classes like Animal, Bird, Cls1, and Cls2 to demonstrate inheritance and polymorphism. Bird extends Animal and can walk, fly, and sing. Cls2 extends Cls1 and can add, multiply, and calculate the sum of squares. The student analyzed that inheritance allows defining a general class and extending it to create specific child classes with shared and extended behaviors. The problems were solved correctly in NetBeans IDE with expected output.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
326 views6 pages

Oop Lab Report

This lab report discusses solving two inheritance problems in Java. The student created classes like Animal, Bird, Cls1, and Cls2 to demonstrate inheritance and polymorphism. Bird extends Animal and can walk, fly, and sing. Cls2 extends Cls1 and can add, multiply, and calculate the sum of squares. The student analyzed that inheritance allows defining a general class and extending it to create specific child classes with shared and extended behaviors. The problems were solved correctly in NetBeans IDE with expected output.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Green University of Bangladesh

Department of Computer Science and Engineering (CSE)


Faculty of Sciences and Engineering
Semester: (Spring, Year:2021), [Link]. in CSE (Day)

LAB REPORT NO #2
Course Title: Object Oriented LAB
Course Code: CSE-202 Section: DN

Lab Experiment Name: Solving two Inheritance related problem from


online judge in java

Student Details

Name ID

1. Md. Jahid Hasan Pintu ID: 201002001

Lab Date : 11/08/2021


Submission Date : 15/08/2021
Course Teacher’s Name : Md. Parvez Hossain

[For Teachers use only: Don’t Write Anything inside this box]

Lab Report Status


Marks: ………………………………… Signature: .....................
Comments: .............................................. Date: ..............................
Solving two Inheritance related problem from online judge in java

OBJECTIVES:
To learn Java Inheritance
To become comfortable problem solving in online judges

PROCEDURE :
Problem 1 from Hacker Rank OJ:
Algorithm :
1. Create a new class name Animal
2. Declare a method name walk under Animal class which can print i am walking
3. Create a new class name Bird which extends Animal class
4. Under Bird class declare 2 method names fly and sing which can print i am singing and i
am flying.
5. Create a new object of Bird class in the main method
6. Call those methods.
7. Print output

IMPLEMENTATION :
Source Code :

package [Link].pkg2;

class Animal{
void walk(){
[Link]("I am walking");
}
}

class Bird extends Animal{


void fly(){
[Link]("I am flying");
}
void sing(){
[Link]("I am singing");
}
}

public class Solution {

public static void main(String[] args) {

Bird bird = new Bird();


[Link]();
[Link]();
[Link]();

OUTPUT :
Problem 2 from Geeks for Geeks :
Algorithm :
8. Create a new class name Cls1
9. Declare a method name add which take two inputs as parameter and print their sum
10. Create a new class name Cls2 which extends Cls1 class
11. Under Cls2 class declare 2 method names mul and task. Mul method can print the
multiplication of two numbers and task can print the sum of squares of those numbers
12. Create a new object of Cls2 class in the main method
13. Create a scanner oject
14. Call those methods.
15. Print output

IMPLEMENTATION :
Source Code :

package [Link].pkg2;

import [Link];

class cls1
{
void add(int p,int q)
{
[Link](p+q);
}
}

class cls2 extends cls1


{
void mul(int p,int q)
{
[Link](p*q);
}
void task(int p,int q)
{
[Link]((p*p)+(q*q));
}
}
public class Sol {
Scanner sc=new Scanner([Link]);
int t=[Link]();
while(t-->0){
cls2 obj=new cls2();
int a=[Link]();
int b=[Link]();

[Link](a,b);
[Link](a,b);
[Link](a,b);

}
}

OUTPUT :

ANALYSIS AND DISCUSSION:


1. In this problem I had to implement inheritance in java.
2. I used Net bean as a IDE.
3. I Scanner object which was i have to import from [Link];
4. Actually I did not face any trouble to solve this problem.
5. I have solved all of the problem in my netbeans but i took screen shot from Geek for
Geek because my Netbeans Screenshot becomes blur in the first problem
6. Output is absolutely correct as expected .

The END

You might also like