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

OOPS Lab Manual AR-23 (1)

This document is a lab manual for the Object Oriented Programming Lab at Aditya Institute of Technology and Management, detailing course objectives, outcomes, and experiments related to Java programming. It outlines the program outcomes, educational objectives, and specific outcomes for computer science students, along with a series of practical experiments designed to enhance their programming skills. Each experiment includes aims, sample programs, and expected outputs to guide students in their learning process.

Uploaded by

rameshrohit225
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)
3 views

OOPS Lab Manual AR-23 (1)

This document is a lab manual for the Object Oriented Programming Lab at Aditya Institute of Technology and Management, detailing course objectives, outcomes, and experiments related to Java programming. It outlines the program outcomes, educational objectives, and specific outcomes for computer science students, along with a series of practical experiments designed to enhance their programming skills. Each experiment includes aims, sample programs, and expected outputs to guide students in their learning process.

Uploaded by

rameshrohit225
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/ 31

AR23

ADITYA INSTITUTE OF TECHNOLOGY AND MANAGEMENT


(AUTONOMOUS)

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

LAB MANUAL
Object Oriented Programming Lab
II B.TECH-II Semester

Prepared by:
M.Divya
AssistantProfessor
Dept of CSE

1
Object Oriented Programming Lab

SubjectCode: 23CSL203 ExternalMarks:60


Credits:1.5 InternalMarks:40

PROGRAM OUTCOMES(POs):

Engineering Graduates will be able to:


1. Engineering Knowledge: Apply the knowledge of mathematics, science, engineering fundamentals, and an
engineering specialization to the solution of complex engineering problems.
2. Problem Analysis: Identify, formulate, review research literature, and analyze complex engineering
problems reaching substantiated conclusions using first principles of mathematics, natural sciences, and
engineering sciences.
3. Design/Development Of Solutions: Design solutions for complex engineering problems and design system
components or processes that meet the specified needs with appropriate consideration for the public health and
safety, and the cultural, societal, and environmental considerations.
4. Conduct Investigations Of Complex Problems: Use research-based knowledge and research methods
includingdesignof experiments, analysis andinterpretationof data,andsynthesis of theinformationtoprovide valid
conclusions.
5. Modern Tool Usage: Create, select, and apply appropriate techniques, resources, and modern engineering
and IT tools including prediction and modeling to complex engineering activities with an understanding of the
limitations.
6. The Engineer and Society: Apply reasoning informed by the contextual knowledge to assess societal,
health, safety, legal and cultural issues and the consequent responsibilities relevant to the professional
engineering practice.
7. Environment and Sustainability: Understand the impact of the professional engineering solutions in
societal andenvironmental contexts, anddemonstratethe knowledge of, and needfor sustainable development.
8. Ethics: Apply ethical principles and commit to professional ethics and responsibilities and norms of the
engineering practice.
9. Individual and Team Work: Function effectively as an individual, and as a member or leader in diverse
teams, and in multidisciplinary settings.
10. Communication: Communicate effectively on complex engineering activities with the engineering
community and with society at large, such as, being able to comprehend and write effective reports and design
documentation, make effective presentations, and give and receive clear instructions.
11. Project Management and Finance: Demonstrate knowledge and understanding of the engineering and
management principles and apply these to one’s own work, as a member and leader in a team, to manage
projects and in multidisciplinary environments.
2
12. Life-Long Learning: Recognize the need for, and have the preparation and ability to engage in
independent and life-long learning in the broadest context of technological change.

PROGRAMME EDUCATIONAL OBJECTIVES(PEOS)

PEO1.Be employed as a practicing engineer in fields such as design,development,testing and


research or undertake higher studies.

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.

PEO4.Conduct themselves as ethical and responsible professionals with good


communication skills and demonstrate leadership skills.

PROGRAMSPECIFICOUTCOMES(PSOs):

By the completion of Computer Science program the student will be able to:

PSO1. Apply mathematical foundations, algorithmic principles, and theoretical


computerscience in the modeling and design of computer-based systems in a way that
demonstrates comprehension of the trade offs involved in design choices.
PSO2. Demonstrate understanding of the principles and working of the hardware and
software aspects of computer systems.
PSO3. Use knowledge in various domains to identify research gaps and hence to provide
solution to new ideas and innovations

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

1. Able to write, compile and execute simple java programs


2. Explain the concept of class and objects with access control to represent real world entities
3. Use overloading methodology on methods and constructors to develop application programs
4. Describe the concept of interface and abstract classes to define generic classes
5. Able to create user defined packages and handle exceptions at run time and Apply Threading
concept based on application requirement and design Applet programming

Mapping Cos with Pos and PSOs:

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.

4 A) Write a java program to illustrate method overloading and method overriding. 15


B) Writ a java program that illustrates how java achieved Run Time Polymorphism.

5 A) Write a java program to demonstrate the use of subclass. 19


B) Write a java program for abstract class to find areas of different shapes

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.

Aim: Develop a java program that displays welcome to follow by user


nameWhichacceptusernamefromtheuser

Program:

importjava.util.*;

class Welcome{

publicstaticvoidmain(Stringargs[]){

Scanner obj=new Scanner(System.in);


System.out.print("EnteraUsername:");

String str=obj.nextLine();
System.out.print("Welcome "+str);

Output:

Enter a User name: AITAM


Welcome AITAM

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:

import java.util.Scanner; class

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

2. A)Write a javaprogram to create a class Rectangle.The class has


attributes Length and Width.It should have methods that calculate
Area and Perimeter of the Rectangle. It should have
readattributes()method to read Length and Width from the user.

Aim:Developajavaprogramtocreatearectangleclasstocalculatearea and perimeter

Program:

import java.util.Scanner; class

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:

Enter the length of Rectangle:


10
Enter the width of Rectangle:
20
Area of Rectangle =200.0
Perimeter of Rectangle =60.0

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;

System.out.println("Fibonacci series upto "+n+" is :-");


while(c<=n)
{
System.out.print(c+"");
a=b;
b=c;
c=a+b;

}
}
}

Output:

Enter a number: 10 Fibonacci


series upto 10 is :- 1 1 2 3 5 8

10
EXPERIMENT-3

3. A) Write a java program that uses both Recursive and Non-Recursive


functions to find the factorial of a givennumber.

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:

Enter any integer:5


Factorial of 5 :120

11
Recursive:
Program:

import java.util.Scanner; class


Factorial{
publicstaticvoidmain(Stringargs[]){

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.

Aim:Developajavaprogramthatcheckswhetherthegivenstringis Palindrome or not

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

4. A) Write a java program to illustrate method overloading and method


overriding.

Aim:Develop a java program to illustrate method overloading and methodoverriding

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[])
{

Bikeobj = new Bike();


obj.run();
}
}

Output:

Bike is running safely

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

5. A)Write a javaprogram to demonstrate theuse of subclass.

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:

values in subclass:a=10 b=20 addition


of values in subclass:30

18
5. B) Write a java program for abstract class to find areas of different
shapes.

Aim:Developajavaprogramforabstractclasstofindareasofdifferent shapes.

Program:

import java.util.Scanner;

abstract class Shape_area {

abstract void rectangle(double l, double b); abstract


void square(double s);
abstractvoidcircle(doubler);
}

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:

Enter Length & Breadth of Rectangle: 10 20Area


of Rectangle: 200.0

Enter Side of a Square: 10


Area of Square: 100.0

Enter Radius of Circle: 10


Area of Circle: 314.0

20
EXPERIMENT-6

6. Write a Java program to implement the concept of importing classes


from user defined package and creating packages

Aim:DevelopaJavaprogramtoimplementtheconceptofimportingclasses
fromuserdefinedpackageandcreatingpackages

GRectangle,java

package Geometry;

public class GRectangle


{
publicvoidarea(doublelength,doublewidth)
{
System.out.println("AreaofRectangle:"+(length*width));
}
}

GSquare.java

package Geometry;

public class GSquare


{
publicvoidarea(doublelength)
{
System.out.print("AreaofRectangle:"+(length*length));
}
}

Demo_packages.java

import Geometry.*; class

Demo_packages
{
publicstaticvoidmain(Stringargs[])
{

GRectangle rec=new GRectangle();


rec.area(10,20);
GSquare s=new GSquare();
s.area(10);

21
}

Output:

Area of Rectangle :200.0


AreaofRectangle:100.0

22
EXPERIMENT-7

7. Write a java program to implement the concept of Exception


handling by using predefined and user defined exceptions.

Aim:Develop a java program to implement the concept of Exception handling


by using predefined and user define dexceptions

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

User defined exceptions

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.

Aim: Develop a java program to implement the concept of Threading by


extendingThread 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 firstThread = new FirstThread();


SecondThread secondThread = new SecondThread();

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[])
{

FirstThread firstThread = new FirstThread();


SecondThread secondThread = new SecondThread();

Thread t1=new Thread(firstThread);


Threadt2=newThread(secondThread);

t1.start();
t2.start();
}
}

Output:

Messag from First Thread : 1


MessagfromSecondThread:1 Messag
from First Thread : 2
MessagfromSecondThread:2 Messag
from First Thread : 3
MessagfromSecondThread:3 Messag
from First Thread : 4
MessagfromSecondThread:4 Messag
from First Thread : 5
MessagfromSecondThread:5

28
EXPERIMENT-9
9. Write a program using Applet to display a message in the Applet
andfor configuring Applets by passing parameters.

Aim:DevelopaprogramusingApplettodisplayamessageintheAppletand for 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

10. Write a java program to implement thread priorities

Aim:Develop a java program to implement thread priorities

Program:

classThreadDemoextendsThread
{
publicvoidrun()
{
System.out.println("Insiderunmethod");
}

publicstaticvoidmain(String[]args)
{
ThreadDemo t1 = new ThreadDemo();
ThreadDemo t2 = new ThreadDemo();
ThreadDemot3=newThreadDemo();

System.out.println("t1 thread priority : " +t1.getPriority());


System.out.println("t2 thread priority : " +t2.getPriority());
System.out.println("t3threadpriority:"+t3.getPriority());

t1.setPriority(2);
t2.setPriority(5);
t3.setPriority(8);

System.out.println("t1 thread priority : " +t1.getPriority());


System.out.println("t2 thread priority : " +t2.getPriority());
System.out.println("t3threadpriority:"+t3.getPriority());

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

You might also like