Java - Lab Manual - 2023-2024 (SANCHIT YADAV)
Java - Lab Manual - 2023-2024 (SANCHIT YADAV)
LABORATORY MANUAL
For
OBJECT ORIENTED PROGRAMMING WITH JAVA LAB
(BCS-452)
Supervisor: Ms. Amrita Mishra
Submitted by:
NAME : SANCHIT YADAV
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.
3
Create Java programs using inheritance and polymorphism.
2
9 To write Java Program to reverse a string
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 {
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();
Enter value of t:
10
1 1 2 3 5 8 13 21 34 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);
Intn=Integer.parseInt(br.readLine());
6
System.out.println("Fibonacci series is as
for(int i=1;i<=n;i++)
{
res=ob.fib(i);
System.out.println(" "+res);
System.out.println();
}}
7
PROGRAM-2
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:
OUTPUT 2:
9
PROGRAM-3
Program Code:
class Animal {
public void animalSound() {
System.out.println("The animal makes a
sound");
}
}
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:
bow bow
11
PROGRAM-4
Program Code:
// Java program to Use exceptions with thread
// ImportingClasses/Files
import java.io.*;
System.out.println("Throwing in "+
t.start();
// try block to deal with
exception try {
Thread.sleep(2000);
}
12
// catch block to handle the exception
catch (Exception x) {
System.out.println("Exception" + x);
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
// Main
Class class
USE_P{
// Display message
// nextInt() method
userName = myObj.nextLine();
14
}
OUPUT:
Enter Your Name:
JAVA_MANUAL
Your Name is :
JAVA_MANUAL
15
PROGRAM-6
Program Code:
// Java code to illustrate standard
import java.io.*;
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 Code:
//Initialize array
int [] arr = new int [] {5, 2, 8, 7, 1};
int temp = 0;
System.out.println();
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 Code:
OUTPUT:
268
486
469
PROGRAM-9
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);
}
Output:
Original string: Dream big
Reverse of given string: gib maerD
21
PROGRAM-10
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