0% found this document useful (0 votes)
83 views22 pages

Java - Lab Manual - 2023-2024 (SANCHIT YADAV)

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)
83 views22 pages

Java - Lab Manual - 2023-2024 (SANCHIT YADAV)

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/ 22

BABU BANARASI DAS ENGINEERING COLLEGE, LUCKNOW

LABORATORY MANUAL
For
OBJECT ORIENTED PROGRAMMING WITH JAVA LAB
(BCS-452)
Supervisor: Ms. Amrita Mishra
Submitted by:
NAME : SANCHIT YADAV

ROLL NO.: 2205080100090

SECTION : CS22

Course: B.Tech.
Branch: Computer Science & Engineering and Computer Science and
Engineering -Artificial Intelligence
Year IInd, Semester IVth

AFFILIATED TO
Dr. APJ ABDUL KALAM TECHNICAL UNIVERSITY,
LUCKNOW

1
LIST OF PROGRAMS
S.NO PROGRAM NAME DATE SIGN

1
Write a Java program that uses both recursive and non-
recursive functions to print the nth value of the Fibonacci
sequence.

2 Create Java programs to find Peterson Number.

3
Create Java programs using inheritance and polymorphism.

4 Write a Java Program to implement error-handling techniques


using exception handling and multithreading.

5 To create java program with the use of java packages.

6 To Construct java program using Java I/O package.

7 To write Java Program to sort the elements of an array in


ascending order

8 To write Java Program to add two matrices

2
9 To write Java Program to reverse a string

10 Java Program to implement Linear Search in Java

3
PROGRAM-1

PROGRAM : The Fibonacci sequence is defined by the following rule. The first 2
values in the sequence are 1, 1. Every subsequent value is the sum of the 2 values
preceding it. Write a Java program that uses both recursive and non-recursive
functions to print the nth value of the Fibonacci sequence.

Program Code:

/*Non RecursiveSolution*/

Import java.util.Scanner;

class Fib {

public static void main(String args[ ]) {

Scanner input=new

Scanner(System.in); int

i,a=1,b=1,c=0,t;

System.out.println("Enter value of

t:"); t=input.nextInt();

System.out.print(a);

System.out.print(" "+b);

for(i=0;i<t-2;i++) {

c=a+b;

a=b;

b=c;

System.out.print(" "+c);

4
System.out.println();

System.out.print(t+"th value of the series is: "+c);

Input & Output:-

Enter value of t:
10
1 1 2 3 5 8 13 21 34 55

10th value of the series is: 55

5
/* Recursive Solution*/

import java.io.*;

import java.lang.*;

class Demo {

int fib(int n) {

if(n==1)

return (1);

else if(n==2)

return (1);

else

return (fib(n-1)+fib(n-2));

class RecFibDemo
{
public static void main(String args[])throws IOException

InputStreamReaderobj=newInputStreamReader(System.in);

Bufferedeader br=new BufferedReader(obj); System.out.println("Enter last number-");

Intn=Integer.parseInt(br.readLine());

Demo ob=new Demo();

6
System.out.println("Fibonacci series is as

follows:-"); int res=0;

for(int i=1;i<=n;i++)
{

res=ob.fib(i);

System.out.println(" "+res);

System.out.println();

System.out.println(n+"th value of the series is "+res);

}}

Input & Output :


Enter last number-
9
Fibonacci series is as follows:-
1 1 2 3 5 8 13 34

7
PROGRAM-2

Program : Create Java programs to find Peterson Number.

Program Code:
import java.io.*;
import java.util.*;
public class PetersonNumberExample1
{
//an array is defined for the quickly find the factorial

static long[] factorial = new int[] { 1, 1, 2, 6, 24, 120, 720, 5040, 40320, 362880, 3628800, 39
916800, 479001600};
//driver code
public static void main(String args[])
{
//constructor of the Scanner class
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number to check: ");
//reading a number from the user
int n=sc.nextInt();
//calling the user-defined function to check Peterson number
if (isPeterson(n))
System.out.println("The given number is a Peterson number.");
else
System.out.println("The given number is not a Peterson number.");
}
//function to check the given number is Peterson or not
static boolean isPeterson(int n)
{
int num = n;
int sum = 0;
//loop executes until the condition becomes false
while (n > 0)
{
//determines the last digit of the given number
8
int digit = n % 10;
//determines the factorial of the digit and add it to the variable sum
sum += factorial[digit];
//removes the last digit of the given number
n = n / 10;
}
//compares sum with num if they are equal returns the number itself
return (sum == num);
}
}

OUTPUT 1:

Enter a number to check: 145


The given number is a Peterson number.

OUTPUT 2:

Enter a number to check: 773


The given number is not a Peterson number.

9
PROGRAM-3

Program : Create Java programs using inheritance and polymorphism.

Program Code:
class Animal {
public void animalSound() {
System.out.println("The animal makes a
sound");
}
}

class Pig extends Animal {


public void animalSound() {
System.out.println("The pig says: wee wee");
}
}

class Dog extends Animal {


public void animalSound() {
System.out.println("The dog says: bow wow");
}
}

class Main {
public static void main(String[] args) {
Animal myAnimal = new Animal(); // Create a Animal
object Animal myPig = new Pig(); // Create a Pig object
Animal myDog = new Dog(); // Create a Dog object
myAnimal.animalSound();
myPig.animalSound();
myDog.animalSound();
}
}

10
OUTPUT:

The animal makes a

sound The pig says:

wee wee The dog says:

bow bow

11
PROGRAM-4

Program : Write a Java Program to implement error-handling techniques using


exception handling and multithreading.

Program Code:
// Java program to Use exceptions with thread

// ImportingClasses/Files

import java.io.*;

// Child Class(Thread) is inherited


// from parent Class(GFG)

class ABC extends Thread {

public void run()


{

System.out.println("Throwing in "+

"MyThread"); throw new RuntimeException();

// Main driver method

public class Main {

public static void main(String[] args)


{

ABC t = new ABC();

t.start();
// try block to deal with

exception try {

Thread.sleep(2000);
}
12
// catch block to handle the exception

catch (Exception x) {

// Print command when exception encountered

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

// Print command just to show program


// run successfully

System.out.println("Completed");

OUTPUT:

Throwing in MyThread
Exception in thread "Thread-0"
java.lang.RuntimeException at
testapp.MyThread.run(Main.java:19)
Completed

13
PROGRAM-5

Program :To create java program with the use of java packages.

Program Code:
// Java Program to Import a package

// Importing java utility

package import java.util.*;

// Main

Class class

USE_P{

// Main driver method

public static void main(String[] args)

// Scanner to take input from the user

object Scanner myObj = new

Scanner(System.in); String userName;

// Display message

// Enter Your Name And Press Enter

System.out.println("Enter Your Name:");

// Reading the integer age entered using

// nextInt() method

userName = myObj.nextLine();

// Print and display

System.out.println("Your Name is : " + userName);

14
}

OUPUT:
Enter Your Name:
JAVA_MANUAL

Your Name is :
JAVA_MANUAL

15
PROGRAM-6

Program : To Construct java program using Java I/O package.

Program Code:
// Java code to illustrate standard

// input output streams

import java.io.*;

public class SimpleIO {

public static void main(String args[]) throws IOException

// InputStreamReader class to read input

InputStreamReader inp = null;

// Storing the input in inp

inp = new InputStreamReader(System.in);

System.out.println("Enter characters, "+ " and '0' to

quit.");

char

c; do

c = (char)inp.read();

System.out.println(c);

} while (c != '0');

}
OUTPUT:
Enter characters, and '0' to quit
JAVALEARNERS
0
J
A
V
A
L
E
A
R
N
E
R
S
PROGRAM-7

Program : Java Program to sort the elements of an array in ascending order

Program Code:

public class SortAsc {


public static void main(String[] args) {

//Initialize array
int [] arr = new int [] {5, 2, 8, 7, 1};
int temp = 0;

//Displaying elements of original array


System.out.println("Elements of original array: ");
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
}

//Sort the array in ascending order


for (int i = 0; i < arr.length; i++) {
for (int j = i+1; j < arr.length; j++) {
if(arr[i] > arr[j]) {
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}

System.out.println();

//Displaying elements of array after sorting


System.out.println("Elements of array sorted in ascending order: ");
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
}
}
}

OUTPUT:
Elements of original array:
5 2 8 7 1
Elements of array sorted in ascending order:
1 2 5 7 8
PROGRAM-8

Program : To write Java Program to add two matrices

Program Code:

public class MatrixAdditionExample


{
public static void main(String args[])
{
//creating two matrices
int a[][]={{1,3,4},{2,4,3},{3,4,5}};
int b[][]={{1,3,4},{2,4,3},{1,2,4}};

//creating another matrix to store the sum of two matrices


int c[][]=new int[3][3]; //3 rows and 3 columns

//adding and printing addition of 2 matrices


for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
c[i][j]=a[i][j]+b[i][j]; //use - for subtraction
System.out.print(c[i][j]+" ");
}
System.out.println();//new line
}
}}

OUTPUT:

268
486
469
PROGRAM-9

Program : Java Program to find Reverse of the string.

Program Code:
public class Reverse
{
public static void main(String[] args) {
String string = "Dream big";
//Stores the reverse of given string
String reversedStr = "";

//Iterate through the string from last and add each character to variable reversedStr
for(int i = string.length()-1; i >= 0; i--){
reversedStr = reversedStr + string.charAt(i);
}

System.out.println("Original string: " + string);


//Displays the reverse of given string
System.out.println("Reverse of given string: " + reversedStr);
}
}

Output:
Original string: Dream big
Reverse of given string: gib maerD

21
PROGRAM-10

Program : Java Program to implement Linear Search in Java

Program Code:
public class LinearSearchExample{
public static int linearSearch(int[] arr, int key){
for(int i=0;i<arr.length;i++){
if(arr[i] == key){
return i;
}
}
return -1;
}
public static void main(String a[]){
int[] a1= {10,20,30,50,70,90};
int key = 50;
System.out.println(key+" is found at index: "+linearSearch(a1, key));
}
}

Output:

50 is found at index: 3

22

You might also like