OOPS Lab Manual AR-23 (1)
OOPS Lab Manual AR-23 (1)
LAB MANUAL
Object Oriented Programming Lab
II B.TECH-II Semester
Prepared by:
M.Divya
AssistantProfessor
Dept of CSE
1
Object Oriented Programming Lab
PROGRAM OUTCOMES(POs):
PEO2.Engage in lifelong self directed learning,a capacity that is vital for success in today’s global
and rapidly changing engineering environment.
PEO3. Create new methods/processes to meet the society needs with their knowledge.
PROGRAMSPECIFICOUTCOMES(PSOs):
By the completion of Computer Science program the student will be able to:
3
CourseObjectives:
To develop skills to design and analyze the applications with respect to java programming
To strengthen the ability to identify and apply the suitable object oriented concept for the
Given Real world problem
CourseOutcomes:
By the end of this course the student will be able to
CO PO1 PO2 PO3 PO4 PO5 PO6 PO7 PO8 PO9 PO10 PO11 PO12 PSO1 PSO2 PSO3
CO1 3 2 3 2 3 - - - - - - 1 2 2 1
CO2 3 3 2 2 3 - - - - - - 1 2 2 2
CO3 3 2 2 1 3 - - - - - - 1 1 1 1
CO4 3 3 2 1 3 - - - - - - 1 2 1 1
CO5 3 2 2 1 3 - - - - - - 1 2 1 1
CO6 3 2 2 1 2 1 1 2 1
INDEX
SNo Experiments PgNo
1 A) Write a java program that displays welcome to follow by user name. Accept user
name from the user.
B) Write a java program that prompts the user for an integer and then prints out all 7
the prime numbers up to that integer
2 A) Write a java program to create a class Rectangle. The class has attributes Length 9
and Width. It should have methods that calculate Area and Perimeter of the
Rectangle. It should have read Attributes () method to read Length and Width from
the user.
B) The Fibonacci sequence is defined by the following rule: The first two values in
the sequence are1 and 1. Every subsequent value is the sum of the two values
preceding it.
4
3 A) Write a java program that uses both Recursive and Non-Recursive functions to 12
find the factorial of a given number.
B) Write a java program that checks whether the given string is Palindrome or not.
Ex: MALAYALAM is a Palindrome.
6 Write a Java program to implement the concept of importing classes from user 22
defined package and creating packages.
7 Write a java program to implement the concept of Exception Handling by using 24
predefined and user defined exceptions.
8 Write a java program to implement the concept of Threading by Extending Thread 26
class and by Implementing Runnable Interface
9 Write a program using Applet to display a message in the Applet and for 30
configuring Applets by passing parameters.
10 Write a java program to implement thread priorities 31
5
EXPERIMENT-1
1. A) Write a java program that displays welcome to follow by user
name.Accept username from the user.
Program:
importjava.util.*;
class Welcome{
publicstaticvoidmain(Stringargs[]){
String str=obj.nextLine();
System.out.print("Welcome "+str);
Output:
6
1.B) Write a java program that prompts the user for an integer and then
printsout all the primenumbers upto that integer.
Aim:Develop a javaprogram that prompts the user for an integer and then prints
out all the prime numbers up to that integer.
Program:
PrimeNumber
publicstaticvoidmain(String[]args)
{
intn,count;
Scanner s=new Scanner(System.in);
System.out.print("Enternumber:");
n=s.nextInt();
for(inti=2;i<=n;i++)
count=0;
for(intj=2;j<i;j++)
{
if(i%j==0) count=1;
}
if(count==0){
System.out.print(""+i);
Output:
Enternumber:10 2 3
57
7
EXPERIMENT-2
Program:
Rectangle
{
double length,width;
voidread_attributes()
{
Scanner scanner = new
Scanner(System.in);System.out.println("Enter the length of
Rectangle:"); length = scanner.nextDouble();
System.out.println("Enter the width of Rectangle:"); width
= scanner.nextDouble();
voidarea()
{
double area=length*width; System.out.println("Area
of Rectangle ="+area);
}
voidperimeter()
{
doubleperimeter=2*(length+width); System.out.println("Perimeter
of Rectangle ="+perimeter);
8
classCalRectangle{
publicstaticvoidmain(String[]args)
{
Rectangle rec=new Rectangle();
rec.read_attributes();rec.area();
rec.perimeter();
}
}
Output:
9
2. B) The Fibonacci sequence is defined by the following rule: The first
two values in the sequence are1 and 1. Every subsequent value is
the sum of the two values preceding it.
Aim:DevelopajavaprogramtogenerateFibonaccisequence.
Program:
importjava.util.*;
class Fibo
{
publicstaticvoidmain(Stringargs[])
{
intc=1,n;
Scannersc=newScanner(System.in);
System.out.print("Enter a number:"); n=sc.nextInt();
inta=1;
intb=0;
}
}
}
Output:
10
EXPERIMENT-3
Aim:DevelopajavaprogramthatusesbothRecursiveandNon-Recursive
functionstofindthefactorialofagivennumber
Non-Recursive:
Program:
import java.util.Scanner; class
Factorial_non
{
publicstaticvoidmain(String[]args)
{
intn,mul=1;
Scanner s = new Scanner(System.in);
System.out.print("Enter any integer:"); n
= s.nextInt();
for(inti=1;i<=n;i++)
{
mul=mul*i;
}
System.out.println("Factorialof"+n+":"+mul);
}
}
Output:
11
Recursive:
Program:
Scannerscanner=newScanner(System.in);
System.out.print("Enter the number:");
intnum=scanner.nextInt();
intfactorial=fact(num);
System.out.println("Factorialofenterednumberis:"+factorial);
}
staticintfact(intn)
{
int output;
if(n==1){
return1;
}
output = fact(n-1)* n;
return output;
}
}
Output:
Enterthenumber:5
Factorialofenterednumberis:120
12
3. B)Write a javaprogram that checks whether the given string is
Palindrome or not. Ex:MALAYALAM is a Palindrome.
Program:
importjava.util.*;
publicclassPalindromes
{
publicstaticvoidmain(Stringargs[])
{
Stringstr1,str2="";
Scanners=newScanner(System.in);
System.out.print("Enterthestring:");
str1 = s.nextLine();
int n = str1.length(); for(int i
= n - 1; i >= 0; i--)
{
str2=str2+str1.charAt(i);
}
if(str1.equalsIgnoreCase(str2))
{
System.out.println("Thestringispalindrome.");
}
else
{
System.out.println("Thestringisnotpalindrome.");
}
} }
Output:
Enterthestring:malayalam The
string is palindrome.
13
EXPERIMENT-4
MethodOverloading
Program:
class OverloadDemo
{void test()
{
System.out.println("Noparameters");
}
voidtest(inta)
{
System.out.println("a:"+a);
}
voidtest(inta,intb)
{
System.out.println("aandb:"+a+""+b);
}
doubletest(doublea)
{
System.out.println("double a: " + a);
return a*a;
}
}
classOverload
{
publicstaticvoidmain(Stringargs[])
{
OverloadDemo ob = new OverloadDemo();
double result;
ob.test();
ob.test(10);
ob.test(10,20);
result=ob.test(123.25);
14
System.out.println("Resultofob.test(123.25):"+result);
}
}
Output:
No parameters a:
10
aandb:1020
doublea:123.25
Resultofob.test(123.25):15190.5625
15
Method overriding
Program:
class Vehicle{
void run(){
System.out.println("Vehicleisrunning");
}
}
class Bike extends Vehicle{
void run()
{
System.out.println("Bikeisrunning safely");
}
}
classOverride{
publicstaticvoidmain(Stringargs[])
{
Output:
16
4. B) Write a java program that illustrates how java achieved Run Time
Polymorphism.
Aim:DevelopajavaprogramthatillustrateshowjavaachievedRunTime Polymorphism
Program:
classA{
voidcallme(){
System.out.println("InsideA'scallmemethod");
}
}
classBextendsA{
voidcallme(){
System.out.println("InsideB'scallmemethod");
}
}
classCextendsA{
voidcallme(){
System.out.println("InsideC'scallme method");
}
}
classOverride{
publicstaticvoidmain(Stringargs[]){
Aa=newA();
Bb=newB();
Cc=newC();
Ar;
r = a;
r.callme();r
= b;
r.callme();r
= c;
r.callme();
}
}
Output:
InsideA'scallmemethod
InsideB'scallmemethod
InsideC'scallmemethod
17
EXPERIMENT-5
Aim:Developajavaprogramtodemonstratetheuseofsubclass.
Program:
class Superclass{
int a ,b;
Superclass() {
a=10;
b=20;
}
}
class Subclass extends Superclass{ int
c;
voiddisplay(){
System.out.println("valuesinsubclass:a="+a+"b="+b);
}
voidadd()
{
c=a+b;
System.out.println("additionofvaluesinsubclass:"+c);
}
classDemo_inherit{
publicstaticvoidmain(Stringargs[]){
Subclassob=newSubclass();
ob.display();
ob.add();
}
}
Output:
18
5. B) Write a java program for abstract class to find areas of different
shapes.
Aim:Developajavaprogramforabstractclasstofindareasofdifferent shapes.
Program:
import java.util.Scanner;
classShapeextendsShape_area{
voidrectangle(doublel,doubleb)
{
doublearea=l*b;
System.out.println("AreaofRectangle:"+area);
}
voidsquare(doubles)
{
doublearea=s*s;
System.out.println("AreaofSquare:"+area);
}
voidcircle(doubler)
{
double area = 3.14*r*r; System.out.println("Area
of Circle: "+area);
}
}
classCalcarea{
publicstaticvoidmain(Stringargs[])
{
doublel,b,h,r,s;
Shapearea=newShape();
Scannerget=newScanner(System.in);
System.out.print("\nEnterLength&BreadthofRectangle:"); l =
get.nextDouble();
19
b = get.nextDouble();
area.rectangle(l, b);
System.out.print("\nEnterSideofaSquare:"); s =
get.nextDouble();
area.square(s);
System.out.print("\nEnterRadiusofCircle:"); r =
get.nextDouble();
area.circle(r);
}
}
Output:
20
EXPERIMENT-6
Aim:DevelopaJavaprogramtoimplementtheconceptofimportingclasses
fromuserdefinedpackageandcreatingpackages
GRectangle,java
package Geometry;
GSquare.java
package Geometry;
Demo_packages.java
Demo_packages
{
publicstaticvoidmain(Stringargs[])
{
21
}
Output:
22
EXPERIMENT-7
PredefinedException
Program
classDemo_Exception{
publicstaticvoidmain(Stringargs[])
{
int d, a;
try
{
d=0;
a=42/d;
System.out.println("Thiswillnotbeprinted.");
}
catch(ArithmeticExceptione)
{
System.out.println("Divisionbyzero.");
}
System.out.println("Aftercatchstatement.");
}
}
Output:
Divisionbyzero.
After catch statement
classMyExceptionextendsException
{
publicMyException()
{
23
}
publicclassException_demo
{
publicstaticvoidmain(Stringargs[])
{
try
{
thrownewMyException();
}
catch(MyExceptionex)
{
System.out.println("Exceptionhandled");
}
}
}
Output:
Exception handled
24
EXPERIMENT-8
8. Write a java program to implement the concept of Threading by
extending Thread class and by Implementing Runnable Interface.
Threadclass
Program:
classFirstThreadextendsThread
{
publicvoidrun()
{
for(inti=1;i<=5;i++)
{
System.out.println("MessagfromFirstThread:"+i); try
{
Thread.sleep(1000);
}
catch(InterruptedExceptioninterruptedException)
{
System.out.println("FirstThreadisinterruptedwhenitissleeping"
+interruptedException);
}
}
}
}
classSecondThreadextendsThread
{
publicvoidrun()
{
for(inti=1;i<=5;i++)
{
System.out.println("MessagfromSecondThread:"+i); try
{
Thread.sleep(1000);
}
catch(InterruptedExceptioninterruptedException)
{
System.out.println( "Second Thread is interrupted when it issleeping"
+interruptedException);
}
25
}
}
}
publicclassDemo_Thread
{
publicstaticvoidmain(Stringargs[])
{
firstThread.start();
secondThread.start();
}
}
Output:
MessagfromSecondThread:1 Messag
from First Thread : 1 Messag from
First Thread : 2
MessagfromSecondThread:2 Messag
from First Thread : 3
MessagfromSecondThread:3 Messag
from First Thread : 4
MessagfromSecondThread:4
MessagfromSecondThread:5 Messag
from First Thread : 5
26
Runnable Interface
Program:
classFirstThreadimplementsRunnable
{
publicvoidrun()
{
for(inti=1;i<=5;i++)
{
System.out.println("MessagfromFirstThread:"+i); try
{
Thread.sleep(1000);
}
catch(InterruptedExceptioninterruptedException)
{
System.out.println("FirstThreadisinterruptedwhenitissleeping"
+interruptedException);
}
}
}
}
classSecondThreadimplementsRunnable
{
publicvoidrun()
{
for(inti=1;i<=5;i++)
{
System.out.println("MessagfromSecondThread:"+i); try
{
Thread.sleep(1000);
}
catch(InterruptedExceptioninterruptedException)
{
System.out.println( "Second Thread is interrupted when it issleeping"
+interruptedException);
}
}
}
}
publicclassDemo_Thread
{
27
publicstaticvoidmain(Stringargs[])
{
t1.start();
t2.start();
}
}
Output:
28
EXPERIMENT-9
9. Write a program using Applet to display a message in the Applet
andfor configuring Applets by passing parameters.
Program:
import java.awt.*;
import java.applet.*;
/*
<appletcode="Demo_Applet"width="400"height="200">
<paramname="Name"value="AITAM">
<paramname="Location"value="Tekkali">
</applet>
*/
publicclassDemo_AppletextendsApplet
{
String name; String
location;
publicvoidinit()
{
name = getParameter("Name");
location=getParameter("Location");
}
publicvoidpaint(Graphicsg)
{
g.drawString("Reading parameters passed to this applet -", 20,
20);g.drawString("Name -" + name, 20, 40);
g.drawString("Location-"+location,20,60);
}
}
Output:
29
EXPERIMENT-10
Program:
classThreadDemoextendsThread
{
publicvoidrun()
{
System.out.println("Insiderunmethod");
}
publicstaticvoidmain(String[]args)
{
ThreadDemo t1 = new ThreadDemo();
ThreadDemo t2 = new ThreadDemo();
ThreadDemot3=newThreadDemo();
t1.setPriority(2);
t2.setPriority(5);
t3.setPriority(8);
System.out.print(Thread.currentThread().getName()); System.out.println("Main
thread priority : "+ Thread.currentThread().getPriority());
Thread.currentThread().setPriority(10); System.out.println("Main
thread priority : "+ Thread.currentThread().getPriority());
}
}
30
Output:
t1threadpriority:5
t2threadpriority:5
t3threadpriority:5
t1threadpriority:2
t2threadpriority:5
t3threadpriority:8
mainMainthreadpriority:5
Main thread priority : 10
31