Java Lab Manual Naman Sharma EN19CS301214
Java Lab Manual Naman Sharma EN19CS301214
Semester – IV
Branch – CSE
Section – CS–D
Submitted To – Submitted By –
Mr. Vivek Kumar Gupta Naman Sharma
Enrollment No. –
EN19CS301214
Introduction to java
1. What is java?
Object Oriented
• Platform Independent
• Simple
• Secure
• Architecture-neutral
• Portable
• Robust
• Multithreaded
• Interpreted
• High Performance
• Distributed
• Dynamic
Ans. C++ is derived from C and has the features of both procedural and object-
oriented programming languages.
Ans. JDK is a software development kit whereas JRE is a software bundle that
allows Java program to run, whereas JVM is an environment for executing
bytecode. The full form of JDK is Java Development Kit, while the full form
of JRE is Java Runtime Environment, while the full form
of JVM is Java Virtual Machine.
Ans. The Just-In-Time (JIT) compiler is an essential part of the JRE i.e. Java
Runtime Environment, that is responsible for performance optimization of java
based applications at run time. Compiler is one of the key aspects in deciding
performance of an application for both parties i.e. the end user and the
application developer.
Ans. Compiling and running a java program is very easy after JDK installation.
Following are the steps −
• Open a command prompt window and go to the directory where you saved
the java program (MyFirstJavaProgram.java). Assume it's C:\.
• Type 'javac MyFirstJavaProgram.java' and press enter to compile your
code. If there are no errors in your code, the command prompt will take
you to the next line (Assumption: The path variable is set).
• Now, type ' java MyFirstJavaProgram ' to run your program.
• You will be able to see the result printed on the window.
Basic Programs:
import java.util.*;
public class Hello{
public static void main(String[] args)
{
System.out.println("Hello World");
}
}
Output:
Hello World
import java.util.Scanner;
import java.util.Scanner;
class SI {
public static void main(String[] args) {
input.close();
}
}
Output :
Enter the principal: 1000
Enter the rate: 5
Enter the time: 2
Principal: 1000.0
Time Duration: 2.0
Simple Interest: 100.0
import java.util.Scanner;
if(num % 2 == 0)
System.out.println(num + " is even");
else
System.out.println(num + " is odd");
}
}
Output :
Enter a number: 69
69 is odd
import java.util.*;
public class factorial
{
public static void main(String[] args)
{
int i,number , fact=1;
System.out.println("Enter a number");
Scanner sc = new Scanner(System.in);
number = sc.nextInt();
for(i=1;i<=number;i++)
{
fact = fact*i;
}
System.out.printf("Factorial of %d is %d ",number,fact);
}
Output
Enter a number
7
Factorial of 7 is 5040
import java.util.*;
public class FtoC {
public static void main(String[] args) {
float F, C;
System.out.println("Enter Temperature in Fahrenheit");
Scanner sc = new Scanner(System.in);
F = sc.nextFloat();
System.out.println("Converting F To C");
C = ((F - 32)*5)/9;
System.out.printf("Temperature in Celsius is %f C ",C);
}
}
Output:
Enter Temperature in Fahrenheit
212
Converting F To C
Temperature in Celsius is 100.000000 C
7.Write a program to find largest no is 3 numbers.
import java.util.*;
public class Largestno {
public static void main(String [] args){
int a ,b,c;
Scanner sc = new Scanner(System.in);
System.out.println("Input the value of number 1");
a = sc.nextInt();
System.out.println("Input the value of number 2");
b = sc.nextInt();
System.out.println("Input the value of number 3");
c = sc.nextInt();
Output :
Input the value of number 1
5
Input the value of number 2
6
Input the value of number 3
9
9 is largest number
8.Write a program to print first 10 terms of Fibonacci series.
import java.util.*;
public class Fibonacci {
public static void main(String[] args) {
{
{
int i, number, Fibonacci;
int a = 0, b = 1;
System.out.println("Enter a number");
Scanner sc = new Scanner(System.in);
number = sc.nextInt();
System.out.println("Fionacci series of number is ");
for (i = 1; i <= number; i++) {
Fibonacci = a + b;
System.out.printf("%d\t", Fibonacci);
a = b;
b = Fibonacci;
}
}
}
}
}
Output:
Enter a number
8
Fionacci series of number is
1 2 3 5 8 13 21 34
import java.util.*;
public class Table
{
public static void main(String[] args)
{
int i,num;
System.out.println("Enter any value");
Scanner sc = new Scanner(System.in);
num = sc.nextInt();
System.out.println("Table of given number is ");
for(i=1;i<=10;i++)
{
System.out.printf("%d X %d = %d\n",num,i,num*i );
}
}
}
Output:
Enter any value
69
Table of given number is
69 X 1 = 69
69 X 2 = 138
69 X 3 = 207
69 X 4 = 276
69 X 5 = 345
69 X 6 = 414
69 X 7 = 483
69 X 8 = 552
69 X 9 = 621
69 X 10 = 690
import java.util.*;
}
System.out.println(rev);
}
}
Output:
Enter any value
369
Reverse of a given number 369 is
963
import java.util.*;
public class BinarytoD {
public static void main(String args[])
{
int dec_num, quot, i=1, j;
int bin_num[] = new int[100];
Scanner sc = new Scanner(System.in);
quot = dec_num;
while(quot != 0)
{
bin_num[i++] = quot%2;
quot = quot/2;
}
Output:
import java.util.Scanner;
public class ElectricityBill
{
private static Scanner sc;
public static void main(String[] args)
{
int Units;
double X,Amount,Total_Amount;
sc = new Scanner(System.in);
if (Units <=100)
{
Amount = Units * 2;
}
else
{
Amount = 1050+ ((Units - 150 ) * 7);
}
X = Amount;
Total_Amount = Amount + 200;
System.out.println("\nElectricity Charges = " + X );
System.out.println("Fixed Charge = 200");
System.out.println("Total Electricity Bill = " + Total_Amount);
}
}
Output:
try
{
BufferedReader br=new BufferedReader(new InputStreamReader(Sys
tem.in));
ch=Integer.parseInt(br.readLine());
}
catch(Exception e) { }
if(ch==4)
break;
else
{
switch(ch)
{
case 1:
push();
break;
case 2:
pop();
break;
case 3:
display();
break;
}
}
}
}
static void push()
{
if(top==max)
System.out.println("stack is full");
else
try
{
BufferedReader br=new BufferedReader(new InputStreamReader(Sys
tem.in));
System.out.println("enter the element:");
item=Integer.parseInt(br.readLine());
a[++top]=item;
}
catch(Exception e) { }
}
static void pop()
{
if(top==-1)
System.out.println("stack is empty");
else
top--;
System.out.println("poped item:"+a[top]);
}
static void display()
{
System.out.println("elements in stack are:");
for(i=top; i>0; i--)
System.out.println(a[i]);
}
}
Output:
enter 1.Push 2.Pop 3.Display 4.Exit
1
enter the element:
5
enter 1.Push 2.Pop 3.Display 4.Exit
1
enter the element:
6
enter 1.Push 2.Pop 3.Display 4.Exit
1
enter the element:
7
enter 1.Push 2.Pop 3.Display 4.Exit
2
poped item:7
enter 1.Push 2.Pop 3.Display 4.Exit
3
elements in stack are:
6
5
enter 1.Push 2.Pop 3.Display 4.Exit
4
Queue
import java.io.*;
class Queue
{
static int i,front,rear,item,max=5,ch;
static int a[]=new int[5];
QueueArr()
{
front=-1;
rear=-1;
}
public static void main(String args[])throws IOException
{
while((boolean)true)
{
try
{
System.out.println("Select Option 1.insert 2.delete 3.display
4.Exit");
BufferedReader br=new BufferedReader(new InputStreamReader(Sys
tem.in));
ch=Integer.parseInt(br.readLine());
}
catch(Exception e)
{ }
if(ch==4)
break;
else
{
switch(ch)
{
case 1:
insert();
break;
case 2:
delete();
break;
case 3:
display();
break;
}
}
}
}
static void insert()
{
if(rear>=max)
{
System.out.println("Queue is Full");
}
else
{
try
{
BufferedReader br=new BufferedReader(new InputStreamReader(Sys
tem.in));
System.out.println("Enter the Element: ");
item=Integer.parseInt(br.readLine());
}
catch(Exception e)
{}
rear=rear+1;
a[rear]=item;
}
}
static void delete()
{
if(front==-1)
{
System.out.println("Queue is Empty");
}
else
{
front=front+1;
item=a[front];
System.out.println("Deleted Item: "+item);
}
}
static void display()
{
System.out.println("Elements in the Queue are:");
for(int i=front+1; i<=rear; i++)
{
System.out.println(a[i]);
}
}
}
Output:
Select Option 1.insert 2.delete 3.display 4.Exit
1
Enter the Element:
5
Select Option 1.insert 2.delete 3.display 4.Exit
1
Enter the Element:
6
Select Option 1.insert 2.delete 3.display 4.Exit
1
Enter the Element:
7
Select Option 1.insert 2.delete 3.display 4.Exit
2
Deleted Item: 5
Select Option 1.insert 2.delete 3.display 4.Exit
3
Elements in the Queue are:
6
7
Select Option 1.insert 2.delete 3.display 4.Exit
4
Experiment 2
int passengers;
int fuelCapacity;
int mileage;
Vehicle1() {
this.passengers = passengers;
this.fuelCapacity = fuelCapacity;
this.mileage = mileage;
void fuelConsumption() {
}
public static void main(String args[]) {
v.fuelConsumption();
Output:
This is Parameterized constructor
This is Default constructor
Fuel consumption : 428 litre
Experiment 3
Inheritance
Aim: Write a Java Program and create a class Shape which has width,
height, radius and create three classes Rectangle, Square and Circle and
shows Inheritance Properties among them.
import java.util.*;
import java.util.*;
class Shape{
int width,height,radius;
Shape()
{
}
Shape(int p)
{
}
Shape(int c,int b)
{
}
}
class Rectangle extends Shape{
Rectangle(int width, int height)
{
super(width,height);
this.width = width;
this.height = height;
}
void area()
{
int a = width*height;
System.out.println("Area of Rectangle is :"+a);
}
}
class Square extends Shape{
Square(int width)
{
super(width);
this.width = width;
}
void area()
{
int a = width*width;
System.out.println("Area of Square is :"+a);
}
}
class Circle extends Shape{
Circle(int radius)
{
super(radius);
this.radius = radius;
}
void area()
{
double a = (double) (Math.PI*radius*radius);
System.out.println("Area of Circle is :"+a);
}
}
public class exp3{
public static void main(String[] args){
Rectangle n = new Rectangle(4,5);
n.area();
Square p = new Square(6);
p.area();
Circle m = new Circle(3);
m.area();
Shape s = new Shape();
}
}
Output:
Area of Rectangle is :20
Area of Square is :36
Area of Circle is :28.274333882308138
Experiment 4
Aim : Program on String
import java.util.Scanner;
public class exp4
{
public static void main (String args[])
{
String S1 = new String ("This is string");
String S2 = new String ("\nHi");
String S3 = S1.concat(S2);
System.out.println (S3);
int index1=S1.indexOf("is");
System.out.println("Index:" +index1);
}
}
Output:
String Length :14
String Length :3
This is string
Hi
Index:2
true
false
Experiment 5
Aim: Program using StringBuffer Class.
public class exp5
{
public static void main(String[] args)
{
StringBuffer a = new StringBuffer("Hello");
a.append(50.55);
System.out.println("appending double = " + a);
a.append('N');
System.out.println("appending character = " + a);
a.append("java");
System.out.println("appending string = "+ a);
a.insert(11, "program5");
System.out.println("inserting string = "+ a);
a.insert(5, 60.66);
System.out.println("inserting double = "+ a);
a.reverse();
System.out.println("reverse = " + a);
}
}
Output :
appending double = Hello50.55
appending character = Hello50.55N
appending string = Hello50.55Njava
inserting string = Hello50.55Nprogram5java
inserting double = Hello60.6650.55Nprogram5java
reverse = avaj5margorpN55.0566.06olleH
Experiment 6
Aim: Program to convert int, float integer, gloat object into
string.
public class exp6
{
float x = 50.55f;
System.out.println("\nfloat:"+x);
}
Output:
int:10
After int to stirng = 10
float:50.55
After float to stirng = 50.55
float object:10.25
Float converted to String as 10.25
Experiment 7
Aim: Program to display values of the field by overriding toString()
method.
public class exp7_1
{
public static void main(String[] args)
{
student s = new student();
System.out.println(s);
}
}
class student{
int Enrollment_Number;
float cgpa;
String name;
student()
{
Enrollment_Number = 214;
name = "Naman Sharma";
cgpa = 8.0F;
}
};
Output:
Name:Naman Sharma
Enrollment_Number:214
CGPA:8.0
Experiment 8
Aim: Exception Handling program
import java.util.*;
import java.io.*;
public class exp8 {
public static void main(String[] args)
{
//Arithmetic Exception
try {
int num1 = 10;
int num2 = 0;
int output = num1 / num2;
System.out.println("Output" + output);
} catch (ArithmeticException e1) {
System.out.println("Arithmetic Exception :");
System.out.println("Can't divide num1 by 0");
}
}
//String Index Out of Bounds
try {
String s = "Naman";
char c = s.charAt(26);
System.out.println(c);
} catch (StringIndexOutOfBoundsException e3) {
System.out.println("String Index is Out of Bounds Exception");
System.out.println("\n");
}
try {
File f = new File(("D://xy.txt"));
FileReader fr = new FileReader(f);
} catch (FileNotFoundException e6) {
}
}
}
Output:
Arithmetic Exception :
Can't divide num1 by 0
Array Index is Out of Bounds Exception
class exp9_1
{
public static void main(String args[]){
try{
try{
System.out.println("going to divide");
int b =39/0;
}catch(ArithmeticException e)
{
System.out.println(e);
}
try{
int a[]=new int[5];
a[5]=4;
}catch(ArrayIndexOutOfBoundsException e)
{
System.out.println(e);
}
System.out.println("other statement");
}catch(Exception e)
{
System.out.println("handeled");
}
System.out.println("normal flow");
}
}
Output:
going to divide
java.lang.ArithmeticException: / by zero
java.lang.ArrayIndexOutOfBoundsException: Index 5 out of bounds for length
5
other statement
normal flow
ii) Multiple Catch Clause
public class exp9_2 {
public static void main(String[] args) {
try
{
int arr[] = new int[10];
arr[10] = 10/0;
}
catch (ArithmeticException e)
{
System.out.println("Arithmetic Exception Occurs First");
}
catch (ArrayIndexOutOfBoundsException e)
{
System.out.println("Array Index Out of Bounds Exception Occurs ")
;
}
}
}
Output:
Arithmetic Exception Occurs First
Experiment 10
i) Thread Class
class MyThread1 extends Thread{
public MyThread1(String name)
{
super(name);
}
public void run()
{
int i =0;
while (i<=40)
{
System.out.println(this.getName()+":"+i);
i++;
try { Thread.sleep(300);}
catch (Exception e){}
}
}
}
public class exp10_1 {
public static void main(String[] args) {
MyThread1 t1 = new MyThread1("Hello");
MyThread1 t2 = new MyThread1("Java");
t1.start();
t2.start();
}
}
Output :
Java:0
Hello:0
Java:1
Hello:1
Java:2
Hello:2
Hello:3
Java:3
Java:4
Hello:4
Hello:5
Java:5
Hello:6
Java:6
Hello:7
Java:7
Hello:8
Java:8
Hello:9
Java:9
Hello:10
Java:10
Hello:11
Hello:12
Java:11
Hello:13
Java:12
Hello:14
Java:13
Hello:15
Java:14
Hello:16
Java:15
Hello:17
Java:16
Hello:18
Java:17
Hello:19
Hello:20
Java:18
Hello:21
Java:19
Java:20
Hello:22
Hello:23
Java:21
Java:22
Hello:24
Hello:25
Java:23
Hello:26
Java:24
Hello:27
Java:25
Hello:28
Java:26
Hello:29
Java:27
Java:28
Hello:30
Hello:31
Java:29
Hello:32
Java:30
Java:31
Hello:33
Hello:34
Java:32
Hello:35
Java:33
Hello:36
Java:34
Hello:37
Java:35
Hello:38
Java:36
Hello:39
Hello:40
Java:37
Java:38
Java:39
Java:40
ii) Runnable Interface
import java.util.*;
class MyThreadRunnable implements Runnable{
}
}
}
public class exp10_2 {
public static void main(String[] args) {
new Thread(new MyThreadRunnable(),"Hello").start();
new Thread(new MyThreadRunnable(),"Naman").start();
}
}
Output:
Naman:0
Hello:0
Naman:1
Hello:1
Hello:2
Naman:2
Naman:3
Hello:3
Naman:4
Hello:4
Naman:5
Hello:5
Naman:6
Hello:6
Naman:7
Hello:7
Hello:8
Naman:8
Hello:9
Naman:9
Hello:10
Naman:10
Hello:11
Naman:11
Hello:12
Naman:12
Hello:13
Naman:13
Hello:14
Naman:14
Hello:15
Naman:15
Hello:16
Naman:16
Naman:17
Hello:17
Naman:18
Hello:18
Naman:19
Hello:19
Naman:20
Hello:20
Naman:21
Naman:22
Hello:21
Naman:23
Naman:24
Hello:22
Naman:25
Naman:26
Naman:27
Hello:23
Naman:28
Naman:29
Hello:24
Naman:30
Hello:25
Naman:31
Hello:26
Hello:27
Naman:32
Hello:28
Naman:33
Naman:34
Hello:29
Naman:35
Hello:30
Hello:31
Naman:36
Hello:32
Naman:37
Hello:33
Naman:38
Hello:34
Naman:39
Hello:35
Naman:40
Hello:36
Hello:37
Hello:38
Hello:39
Hello:40
Experiment 11
Aim: Program to read text file
import java.io.File;
import java.util.Scanner;
class exp11
{
public static void main(String[] args)
{
try
{
}
catch (Exception e)
{
e.getStackTrace();
}
}
}
Output:
Text: Experiment 11 !
Experiment 12
Aim: Program to read line and password using console class.
import java.io.Console;
try {
cnsl = System.console();
if (cnsl != null)
{
} catch(Exception ex) {
ex.printStackTrace();
}
}
}
Output:
Name: Naman
Name is: Naman
Password:
Password is: [C@6acbcfc0