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

20tuee030 Oops Lab Manual 1

The document discusses Java programs implementing various OOP concepts like classes, objects, methods, constructors, inheritance, polymorphism and exception handling. It includes programs for single, multilevel, hierarchical and polymorphic inheritance. The programs are well written to demonstrate the concepts clearly.

Uploaded by

CHIRANJEEVI V
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)
40 views

20tuee030 Oops Lab Manual 1

The document discusses Java programs implementing various OOP concepts like classes, objects, methods, constructors, inheritance, polymorphism and exception handling. It includes programs for single, multilevel, hierarchical and polymorphic inheritance. The programs are well written to demonstrate the concepts clearly.

Uploaded by

CHIRANJEEVI V
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
You are on page 1/ 36

20CSS02

OBJECT ORIENTED
PROGRAMMING LAB

ROLL NO:20TUEE030

NAME:KATHIRVEL.K

DEPARTMENT:EEE-A
EX:NO:1a Implementation of Simple Java Program
Comments, Data Types

Objective:

To write simple java program using comments and data


types.

Algorithm:

Step1: Start

Step2: Input Tamil=90,English=91

Step3: Total=English + tamil

Step4: Display Total

Step5: Stop

Program:

public class Mark


{
public static void main(String args[])
{
// int data type for integer values
int Tamil, English, Total; // Variable declaration
Tamil = 90; // Initialization
English = 91;
Total = Tamil + English;
// printing output
System.out.println("Sub 1: " +Tamil);
System.out.println("Sub 2: " +English);
System.out.println("Sub 1 + Sub 2: " +Total);
}
}

OUTPUT:

Result:
The given output is verified.
EX.NO:1b Implementation of simple Java Program
Operators and Expression Evaluation

Objective:
To write simple java program using operators and
expression evaluation.

Algorithm:
Step1: Start
Step2: Input a=10,b=5,c=3,d=1,e=4,f=2
Step3: Display the a+b
Step4: Display a*a/b+c-d*e/f
Step5: Stop

Program:

public class OperatorExpression


{
public static void main(String args[])
{
int a=10;
int b=5;
int c=3;
int d=1;
int e=4;
int f=2;
System.out.println(a+b);
System.out.println(a*a/b+c-d*e/f);
}
}

Output:

Result:
The given output is verified.
EX.NO:1c Implementation of simple Java Program
Conditional Statements and looping

Objective:
To write simple java program using conditional statements
and looping.

Algorithm:
Step1: Start
Step2: Input number=9
Step3: Condition(number<=10)
Step4: If the condition is true display if block
Step5: Inside If block use for loop
Step6: In for loop Display i until condition falce
Step7: If the condition is false display else block
Step8: Stop

Program:

public class numbers


{
public static void main(String[] args)
{
int number=9;
if(number<=10)
{
System.out.println("Print the number 1 to 10");

for(int i=1;i<=10;i++)
{
System.out.println(i);
}
}
else
{
System.out.println("The given value is greater than 10");
}
}
}

Output:
Result:
The given output is Verified.
EX.NO:2 Implementation of classes,Objects and
Method in Java

Objective:
To write java program using class,object and method.

Algorithm:
Step1: Start
Step2: Define a class Pubg
Step3: Create main function inside Pubg class
Step4: To create Methods inside main function
Step5: Create object and call methods
Step6: Execute and Display output
Step7: stop

Program:

public class Pubg //Class


{
public static void main(String[] args)
{
Pubg devil=new Pubg(); //Creating object
devil.kill();
devil.end();
}
public void kill() //Method
{
System.out.println("12 kills by kathir OP");
}
public void end()
{
System.out.println("winner winner chicken dinner");
}
}

Output:

Result:
The given output is Verified.
EX.NO:3a Implementation of constructors in java
Default Constructor

Objective:
To write java program using default constructor.

Algorithm:
Step1: Start
Step2: Define a class Devil
Step3: Inside Devil class to create a default constructor
Step4: Create main function inside Devil class
Step5: Create object and call Default constructor
Step6: Execute and Display output
Step7: Stop

Program:

public class Devil


{

Devil() //Default constructor


{
System.out.println("Default constructor is created");
}

public static void main(String args[])


{
Devil b=new Devil();
}
}

Output:

Result:
The given output is verified.
EX.NO:3b Implementation of constructors in java
Parameterized Constructor

Objective:
To write java program using constructor.

Algorithm:

Step1: Start
Step2: Define a class Pubg
Step3: Inside class to create a parameterized constructor
Step4: Create method to display the values
Step5: Create main function inside Pubg class
Step6: Inside main function to create objects and passing
values
Step7: Inside main function call method
Step8: Execute and Display output
Step9: Stop

Program:

public class Pubg


{
long Id;
String Name;
//creating a parameterized constructor
Pubg(long i,String n){
Id = i;
Name = n;
}
//method to display the values
void display()
{
System.out.println(Id+" "+Name);

public static void main(String args[])


{
//creating objects and passing values
Pubg player1 = new Pubg(59034154,"࿐╰‿╯M͜͡ r๛𝔇𝒆𝒗𝒊𝒍★࿐");

Pubg player2 = new Pubg(59034153,"Op๛𝔇𝒆𝒗𝒊𝒍");


//calling method to display the values of object
player1.display();
player2.display();
}
}
Output:

Result:
The given output is verified.
EX.NO:3c Implementation of constructors in java
Copy Constructor

Objective:
To right java program using copy constructor.

Algorithm:

Step1: Start
Step2: Create class Pubg
Step3: Inside class to create a constructor with arguments
Step4: Create copy constructor using object for input
Step5: Create method to display the values
Step6: Inside main function to create objects and passing
values
Step7: Using copy constructor copy the Player1 value to
Player2 inside the main function
Step8: Inside main function call methods
Step9: Execute and Display output
Step10: Stop
Program:

public class Pubg


{
long Id;
String Name;
Pubg(long i,String n){
Id = i;
Name = n;
}

Pubg(Pubg p)
{
Id=p.Id;
Name=p.Name;
}
void display()
{
System.out.println(Id+" "+Name);

public static void main(String args[])


{
Pubg player1 = new Pubg(59034154,"࿐╰‿╯M͜͡ r๛𝔇𝒆𝒗𝒊𝒍★࿐");

Pubg player2 = new Pubg(player1);


player1.display();
player2.display();
}
}

Output:

Result:
The given output is verified.
EX.NO:4 Arrays in Java

Objective:
To write java program using arrays.

Algorithm:
Step1: Start
Step2: Define a class Devil
Step3: Create main function inside a Devil class
Step4: Inside main function declare array and initialize
Step5: Inside main function Create for loop using traversing
array
Step6: Execute and Display a[i] until condition falce
Step7: Stop

Program:

public class Devil


{
public static void main(String args[])
{
int a[]=new int[5]; //declaration and instantiation
a[0]=10; //initialization
a[1]=20;
a[2]=70;
a[3]=40;
a[4]=50;
//traversing array
for(int i=0;i<a.length;i++) //length is the property of array
System.out.println(a[i]);
}
}

Output:

Result:
The given output is verified.
EX.NO:5 Access specifier and packages

Objective:
To write java program using access specifier and packages.

Algorithm:
Step1: Start
Step2: Define a package pack
Step3: Create class A in public
Step4: Create method in Protected
Step5: It’s Save by A.java
Step6: Define another package mypack
Step7: Create another class B which inherit from A
class using the keyword ‘extends’
Step8: Create main function and create object and call
methods
Step9: Execute and display the output
Step10: Stop

Program:
//save by A.java
package pack;
public class A
{
protected void msg()
{
System.out.println("Hello");
}
}
//save by B.java
package mypack;
import pack.*;

class B extends A
{
public static void main(String args[])
{
B obj = new B();
obj.msg();
}
}
Output:

Result:
The given output is verified.
EX.NO:6 Inheritance in java

Objective:
To write java program using Inheritance.

Algorithm:
Step1: Start
Step2: Define a class Single for single inheritance
Step3: Create method inside the Single class
Step4: Create another class Boys which inherit from single
class using the keyword ‘extends’.
Step5: Create method inside boys class
Step6: Define another class one for multilevel inheritance
Step7: Create method inside the one class
Step8: Create another class two which inherit from one
class using the keyword ‘extends’.
Step9: Create method inside two class
Step10: Create another class three which inherit from two
class using the keyword ‘extends’.
Step11: Create method inside three class
Step12: Define another class A for hierarchical inheritance
Step13: Create method inside the A class
Step14: Create another class B which inherit from A
class using the keyword ‘extends’.
Step15: Create method inside A class
Step16: Create another class C which inherit from A
class using the keyword ‘extends’.
Step17: Create method inside C class
Step18: Create another class D which inherit from A
class using the keyword ‘extends’.
Step19: Create method inside D class
Step20: Create Derived class in the name Class Main
Step21: create main function inside main class
Step22: Create object for single inheritance and and call
methods
Step23: Create object for multilevel inheritance and and call
methods
Step24: Create object for single inheritance and and call
methods
Step25: Create object for Hierarchical inheritance and and
Call methods
Step26: Execute and display the output
Step27: Stop
Program:

import java.io.*;
import java.lang.*;
import java.util.*;
//Single Inheritance
class Single {
public void one()
{
System.out.println("Op");
}
}

class Boys extends Single {


public void two() { System.out.println("Devil"); }
}

//Multilevel Inheritance
class one {
public void Mr()
{
System.out.println("Mr");
}
}
class two extends one {
public void Devil() { System.out.println("Devil"); }
}

class three extends two {


public void End()
{
System.out.println("Call me a Devil bro");
}
}

//Hierarchical Inheritance
class A {
public void print_A() { System.out.println("Name"); }
}

class B extends A {
public void print_B() { System.out.println("Mr Devil"); }
}

class C extends A {
public void print_C() { System.out.println("Op Devil"); }
}

class D extends A {
public void print_D() { System.out.println("Devil Op"); }
}
// Drived class
public class Main {
public static void main(String[] args)
{
//Derived Class for Single inheritance
System.out.println("Single Inheritance is start");
Boys w = new Boys();
w.one();
w.two();
System.out.println("Single Inheritance is End");

//Derived Class for multilevel inheritance


System.out.println("Multilevel Inheritance is start");
three g = new three();
g.Mr();
g.Devil();
g.End();
System.out.println("Multilevel Inheritance is End");

//Derived Class for Hierarchical Inheritance


System.out.println("Hierarchical Inheritance is start");
B obj_B = new B();
obj_B.print_A();
obj_B.print_B();

C obj_C = new C();


obj_C.print_A();
obj_C.print_C();

D obj_D = new D();


obj_D.print_A();
obj_D.print_D();
System.out.println("Hierarchical Inheritance is End");
}
}

Output:

Result:
The given output is verified.
EX.NO:7 Abstract Classes and Interfaces

Objective:
To write java program using Abstract classes and interfaces.

Algorithm:
Step1: Start
Step2: Define a package Pubg
Step3: Create interface player
Step4: Create method
Step5: Create class Information which implements from
player
Step6: Create method
Step7: Create another class Pubg
Step8: Inside pubg class create main function
Step9: Inside main function Create object and call methods
Step10: Execute and display the output
Step11: Stop

Program:

package Pubg;
interface Player
{
String Name="Devil";
void computepoints();
}
class Information implements Player
{
int point;
public void computepoints()
{
point=2*200;
}
void display()
{
System.out.println(Name);
System.out.println(point);
}

}
public class Pubg
{
public static void main (String[] args)
{
Information Mr=new Information();
Mr.computepoints();
Mr.display();
}
}
Output:

Result:
The given output is verified.
EX.NO:8 Strings and String Functions

Objective:
To write java program using strings and string function.

Algorithm:
Step1: Start
Step2: Define a Class Player
Step3: Create main function inside Player class
Step4: Create String by java String literal
Step5: Create char
Step6: Convert char array to string
Step7: Create java string by new keyword
Step8: Using print statement to display the output
Step9: Stop

Program:

public class Player


{
public static void main(String args[])
{
String s1="Iam a Devil"; //creating string by Java string literal
char ch[]={'M','r','.','D','e','v','i','l'};
String s2=new String(ch); //converting char array to string
String s3=new String("King of Racing"); //creating Java string by new keyword
System.out.println(s1);
System.out.println(s2);
System.out.println(s3);
}
}

Output:

Result:
The given output is verified.
EX.NO:9 Exception Handling

Objective:
To write java program using exception handling.

Algorithm:
Step1: Start
Step2: Create a main class (“NestTry”) and main method in
it.
Step3: Now,create multiple try and catch blocks for nested
try to catch the exceptions.
Step4: Catch block should contain the type of exceptions
which you need to catch (Arithmetic Exception, Array
Index Out Of Bounds Exception).
Step5: Stop.

Program:

public class NestTry


{
public static void main(String args[])
{
try
{
int a = args.length;
int b = 50/a;
System.out.println("a= "+a);
try
{
if(a==1) a=a/(a-a);

if(a==2)
{
int c[]={1};
c[42]=99;

}
catch(ArrayIndexOutOfBoundsException e)
{
System.out.println("Array Index Out-Of-Bounds: "+e);
}
}
catch(ArithmeticException e)
{
System.out.println("Divide by 0 "+e);
}
}
}

Output:

Result:
The given output is verified.

You might also like