Java Lab File
Java Lab File
P)
(A Govt. Aided UGC Autonomous &NAAC Accredited Institute Affiliated to RGPV, Bhopal
SESSION: 2022-2023
1
INDEX
PROGRAM:-
import java .util.*;
public class cmd {
public static void main(String args[]){
int a=Integer.parseInt(args[0]);
int b=Integer.parseInt(args[1]);
int c=a+b;
System.out.println("Sum is: "+c);
}
}
OUTPUT:-
3
EXPERIMENT-2
Q. Write a program to print Fibonacci series without using recursion and using recursion.
PROGRAM:-
//**** FIBONACCI SERIES WITHOUT RECURSION****\\
import java.util.*;
public class fibonacci {
public static void main(String []args){
int a=0;
int b=1;
int c;
int n;
Scanner sc=new Scanner(System.in);
System.out.println("RITESH MAHOUR-0901IT211045");
System.out.println("Enter the no.of digits for the series:");
n= sc.nextInt();
System.out.println("Fibonacci Series:-");
System.out.print(a+" "+b);
for (int i=2;i<n;++i){
c=a+b;
System.out.print(" "+c);
a=b;
b=c; }; }}
OUTPUT:-
4
//**** FIBONACCI SERIES RECURSION****\\
import java.util.*;
public class f_rec {
int f(int n){ //RECURSIVE FUNCTION
if(n==0 || n==1){
return n; }
else
return f(n-1)+f(n-2); }
public static void main(String []args){
int n;
f_rec obj = new f_rec();
System.out.println("RITESH MAHOUR-0901IT211045");
System.out.println("Enter the no. of digits series :");
Scanner sc=new Scanner(System.in);
n= sc.nextInt();
System.out.println("Fibonacci Series:-");
for(int i=0;i<n;i++){
System.out.print(obj.f(i)+" ");
}
}}
OUTPUT:-
5
EXPERIMENT-3
Q. Write a program to check prime numbers and palindrome numbers.
PROGRAM:-
import java.util.*;
public class palindrom{
public static void main(String[] args) {
Integer m = 0,n;
int r;
System.out.println("RITESH MAHOUR-0901IT211045");
System.out.println("Enter a no.:");
Scanner sc = new Scanner(System.in);
n = sc.nextInt();
int k=n;
sc.close();
while (n != 0) {
r = n % 10;
m = 10 * m + r;
n = n / 10;}
System.out.print(m);
System.out.println("");
if (k==m) {
System.out.println("Is a palindrome no."); }
else{
System.out.println("Not a palindrome no. "); } } }
OUTPUT:-
6
EXPERIMENT-4
Q. Write a program to sort an array of elements using bubble sort algorithm.
PROGRAM:-
import java.util.*;
public class bubble_sort {
public static void printArray(int a[],int n){
for (int i = 0; i < n; i++) {
System.out.print(a[i]+" "); }
}
public static void Bubblesort(int arr[], int n) {
int i,j;
for ( i = 0; i < n - 1; i++) {
int flag = 0;
for ( j = 0; j < n - 1 - i; j++) {
if (arr[j] > arr[j + 1]) {
int temp;
temp = arr[j + 1];
arr[j + 1] = arr[j];
arr[j] = temp;
flag = 1;
}
}
if (flag == 0)
break;
}
System.out.println(" ");
}
public static void main(String[] args) {
int n=6;
int arr[] = { 5, 2, 8, 4, 10, 23 };
7
System.out.println("RITESH MAHOUR-0901IT211045");
System.out.println("Array before sorting:-");
printArray(arr,n);
Bubblesort(arr, n);
System.out.println("Array after sorting:-");
printArray(arr,n);
}
}
OUTPUT:-
8
EXPERIMENT-5
Q. Write a program to sort an array of elements using insertion sort algorithm.
PROGRAM:-
import java.util.*;
public class Insertion_sort {
public static void insertionSort(int[] array) {
int n = array.length;
for (int j = 1; j < n; j++) {
int key = array[j];
int i = j-1;
while ( (i > -1) && ( array [i] > key ))
{
array [i+1] = array [i];
i--;
}
array[i+1] = key; } }
public static void main(String... a)
{
int[] arr1 = {9,14,3,2,43,11,58,22};
System.out.println("RITESH MAHOUR-0901IT211045");
System.out.println("Array before Insertion Sort:-");
for(int i:arr1)
{
System.out.print(i+" ");
}
System.out.println();
insertionSort(arr1);
System.out.println("Array after Insertion Sort:-");
9
for(int i:arr1)
{
System.out.print(i+" ");
}
}
}
OUTPUT:-
10
EXPERIMENT-6
Q. Write a non-static function in java that prints the sum of two numbers.
PROGRAM:-
import java.util.Scanner;
public class Non_static {
public static void main(String[] args) {
int x, y, sum;
Scanner sc = new Scanner(System.in);
System.out.println("RITESH MAHOUR-0901IT211045");
System.out.print("Enter the first number: ");
x = sc.nextInt();
System.out.print("Enter the second number: ");
y = sc.nextInt();
sum = sum(x, y);
System.out.println("The sum of two numbers x and y is: " + sum); }
public static int sum(int a, int b) {
return a + b;
}
}
OUTPUT:-
11
EXPERIMENT-7
Q. Create an abstract class Shape which has a field PI=3.14 as final and it has an abstract
method Volume. Make two subclasses Cone and Sphere from this class and they print their
volume.
PROGRAM:-
import java.util.*;
abstract class Shape{
final float PI=3.14f;
Scanner s=new Scanner(System.in);
abstract void volume();
}
class Cone extends Shape{
float r,h,v;
void accept(){
System.out.println("Enter the radius and Height ");
r=s.nextFloat();
h=s.nextFloat();
}
void volume(){
v=PI*(r*r*(h/3));
System.out.println("Volume of Cone is :"+v);
}
}
class Sphere extends Shape{
float a,r,h,v;
void accept(){
System.out.println("Enter the radius : ");
r=s.nextFloat();
12
}
void volume(){
v=((1.33f*PI)*(r*r*r));
System.out.println("Volume of Sphere is :"+v);
}
}
class Vol{
public static void main(String args[]){
Cone s1=new Cone();
Sphere s2=new Sphere();
s1.accept();
s2.accept();
s1.volume();
s2.volume();
}
}
OUTPUT:-
13
EXPERIMENT-8
Q. Write a Java program that works as a simple calculator. Use a grid layout to arrange
buttons for the digits and for the +, -,*, % operations. Add a text field to display the result.
Handle any possible exceptions like divide by zero.
PROGRAM:-
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class BuildCalculator extends JFrame implements ActionListener{
JFrame actualWindow;
JPanel resultPanel, buttonPanel, infoPanel;
JTextField resultTxt;
JButton btn_digits[] = new JButton[10];
JButton btn_plus, btn_minus, btn_mul, btn_div, btn_equal, btn_dot,
btn_clear;
char eventFrom;
JLabel expression, appTitle, siteTitle ;
double oparand_1 = 0, operand_2 = 0;
String operator = "=";
BuildCalculator() {
Font txtFont = new Font("SansSerif", Font.BOLD, 20);
Font titleFont = new Font("SansSerif", Font.BOLD, 30);
Font expressionFont = new Font("SansSerif", Font.BOLD, 15);
actualWindow = new JFrame("Calculator");
resultPanel = new JPanel();
buttonPanel = new JPanel();
infoPanel = new JPanel();
actualWindow.setLayout(new GridLayout(3, 1));
buttonPanel.setLayout(new GridLayout(4, 4));
14
infoPanel.setLayout(new GridLayout(3, 1));
actualWindow.setResizable(false);
appTitle = new JLabel("My Calculator");
appTitle.setFont(titleFont);
expression = new JLabel("Expression shown here");
expression.setFont(expressionFont);
siteTitle = new JLabel("www.btechsmartclass.com");
siteTitle.setFont(expressionFont);
siteTitle.setHorizontalAlignment(SwingConstants.CENTER);
siteTitle.setForeground(Color.BLUE);
resultTxt = new JTextField(15);
resultTxt.setBorder(null);
resultTxt.setPreferredSize(new Dimension(15, 50));
resultTxt.setFont(txtFont);
resultTxt.setHorizontalAlignment(SwingConstants.RIGHT);
for(int i = 0; i <10; i++) {
btn_digits[i] = new JButton(""+i);
btn_digits[i].addActionListener(this);
}
btn_plus = new JButton("+");
btn_plus.addActionListener(this);
btn_minus = new JButton("-");
btn_minus.addActionListener(this);
btn_mul = new JButton("*");
btn_mul.addActionListener(this);
btn_div = new JButton("/");
btn_div.addActionListener(this);
btn_dot = new JButton(".");
btn_dot.addActionListener(this);
btn_equal = new JButton("=");
15
btn_equal.addActionListener(this);
btn_clear = new JButton("Clear");
btn_clear.addActionListener(this);
resultPanel.add(appTitle);
resultPanel.add(resultTxt);
resultPanel.add(expression);
for(int i = 0; i < 10; i++) {
buttonPanel.add(btn_digits[i]);
}
buttonPanel.add(btn_plus);
buttonPanel.add(btn_minus);
buttonPanel.add(btn_mul);
buttonPanel.add(btn_div);
buttonPanel.add(btn_dot);
buttonPanel.add(btn_equal);
infoPanel.add(btn_clear);
infoPanel.add(siteTitle);
actualWindow.add(resultPanel);
actualWindow.add(buttonPanel);
actualWindow.add(infoPanel);
actualWindow.setSize(300, 500);
actualWindow.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
eventFrom = e.getActionCommand().charAt(0);
String buildNumber;
if(Character.isDigit(eventFrom)) {
buildNumber = resultTxt.getText() + eventFrom;
resultTxt.setText(buildNumber);
16
} else if(e.getActionCommand() == ".") {
buildNumber = resultTxt.getText() + eventFrom;
resultTxt.setText(buildNumber);
}
else if(eventFrom != '='){
oparand_1 = Double.parseDouble(resultTxt.getText());
operator = e.getActionCommand();
expression.setText(oparand_1 + " " + operator);
resultTxt.setText("");
} else if(e.getActionCommand() == "Clear") {
resultTxt.setText("");
}
else {
operand_2 = Double.parseDouble(resultTxt.getText());
expression.setText(expression.getText() + " " + operand_2);
switch(operator) {
case "+": resultTxt.setText(""+(oparand_1 +
operand_2)); break;
case "-": resultTxt.setText(""+(oparand_1 -
operand_2)); break;
case "*": resultTxt.setText(""+(oparand_1 *
operand_2)); break;
case "/": try {
if(operand_2 == 0)
throw new
ArithmeticException();
resultTxt.setText(""+(oparand_1 / operand_2)); break;
} catch(ArithmeticException ae) {
JOptionPane.showMessageDialog(actualWindow, "Divisor can not be
ZERO");
17
} } }
} }
public class Calculator {
public static void main(String[] args) {
new BuildCalculator();
} }
OUTPUT:-
18
EXPERIMENT-9
Q. Develop an Applet that receives an integer in one text field & compute its factorial value
& returns it in another text field when the button "Compute" is clicked.
PROGRAM:-
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class fact implements ActionListener{
private static JFrame frame;
private static JPanel panel;
private static JLabel integer;
private static JLabel factorial;
private static JTextField n;
private static JTextField nnot;
private static JButton button;
public static void main(String[] args) {
frame = new JFrame();
frame.setSize(400,400);
frame.setTitle("Factorial");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
panel = new JPanel();
frame.add(panel);
panel.setBackground(Color.green);
panel.setLayout(null);
integer = new JLabel("n");
19
integer.setBounds(10, 20, 80, 25);
panel.add(integer);
factorial = new JLabel("n!");
factorial.setBounds(10, 50, 80, 25);
panel.add(factorial);
n = new JTextField();
n.setBounds(100, 20, 165, 25);
n.setBackground(Color.red);
panel.add(n);
nnot = new JTextField();
nnot.setBounds(100, 50, 165, 25);
nnot.setBackground(Color.yellow);
panel.add(nnot);
button = new JButton("Compute");
button.setBounds(10, 80, 100, 25);
button.addActionListener(new fact());
button.setBackground(Color.cyan);
panel.add(button);
frame.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
String num = n.getText();
int num1 = Integer.parseInt(num);
int fact = 1;
if(num1>0) {
for(int i=num1;i>=1;i--) {
fact *= i;
}
}
nnot.setText(""+fact);
20
}
}
OUTPUT:-
21
EXPERIMENT-10
Q Write a Java program that implements a multi-thread application that has three threads.
First thread generates a random integer every first second and if the value is even, the
second thread computes the square of the number and prints. If the value is odd, the third
thread will print the value of the cube of the number.
PROGRAM:-
import java.util.Random;
randomInteger);
if((randomInteger%2) == 0) {
SquareThread(randomInteger);
sThread.start();}
else {
CubeThread(randomInteger);
cThread.start();}
try {
Thread.sleep(1000);}
System.out.println(ex);} }} }
int number;
SquareThread(int randomNumbern) {
number = randomNumbern;}
22
System.out.println("Square of " + number + " = " + (number *
number));
} }
int number;
CubeThread(int randomNumber) {
number = randomNumber;
number * number);
} }
rnThread.start();
} }
OUTPUT:-
23