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

Java Lab Experiments

Uploaded by

rehaanparkar
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views

Java Lab Experiments

Uploaded by

rehaanparkar
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 20

EXP 1:

Aim:

Write Program to Find Largest of Three Numbers


Program
import java.util.Scanner;
public class LargestNumberExample3
{
public static void main(String[] args)
{
int a, b, c;
Scanner sc = new Scanner(System.in);
System.out.println("Enter the first number:");
a = sc.nextInt();
System.out.println("Enter the second number:");
b = sc.nextInt();
System.out.println("Enter the third number:");
c = sc.nextInt();
if(a>=b && a>=c)
System.out.println(a+" is the largest Number");

else if (b>=a && b>=c)


System.out.println(b+" is the largest Number");

else
System.out.println(c+" is the largest number");
}
}
Output
Enter the first number:
10
Enter the second number:
5
Enter the third number:
6
10 is the largest Number
EXP 2
Aim:

Write a program to input week number and print week day.


Program:
public class Week {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("Enter a number from 1 to 7");
int today=sc.nextInt();
String day = " ";
switch(today)
{
case 1: day = "Monday";
break;
case 2: day = "Tuesday";
break;
case 3: day="Wednesday";
break;
case 4: day="Thursday";
break;
case 5: day="Friday";
break;
case 6: day="Saturday";
break;
case 7: day="Sunday";
break;
default: day="Invalid Number ";
break;
}
System.out.println(day);
}}
OUTPUT
Enter the nuber
4
Thrusday
****************************************************************************************************************
EXP 3
Aim:
Write a program to find the factorial of a number
Program:
import java.util.Scanner;
public class FactorialExample{
public static void main(String args[]){
int i,fact=1;
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number:");
int number= sc.nextInt();
for(i=1;i<=number;i++){
fact=fact*i;
}
System.out.println("Factorial of "+number+" is: "+fact);
}
Output
Enter the number: 5
Factorial of 5 is 120
Exp 4:
Aim:

Write a program in Java to implement the formula of


Volume of a cuboid= length * Breath * height

Program:
import java.util.Scanner;
public class cuboid {
public static void main(String[] args)
{
int volume,length,width,height;
Scanner sc=new Scanner(System.in);
System.out.println("Enter the Length ");
length=sc.nextInt();
System.out.println("Enter the Width ");
width=sc.nextInt();
System.out.println("Enter the Height ");
height=sc.nextInt();
volume=length*width*height;
System.out.println("Volume= "+volume);
}
Output:

EXP 5
Aim:
Write a program to input the name and marks in 3 subjects of a student.
Display the total mark and the average marks.
Program:
import java.util.Scanner;
public class LabExperiment1
{
public static void main(String[] args)
{
String name;
int m[]=new int[3];
System.out.println("Enter the name ");
Scanner sc=new Scanner(System.in);
name=sc.next();
System.out.println("Enter 3 marks ");
for(int i=0;i<3;i++)
{
m[i]=sc.nextInt();
}
//Calculate total and average
int tot=0;
float avg;
for(int i=0;i<3;i++)
{
tot=tot+m[i]; //m[0]+m[1]+m[2];
}
avg=tot/3;
System.out.println("Name = "+name);
System.out.println("Marks are "+m[0]+","+m[1]+" , "+m[2]);
System.out.println("TOTAL MARK = "+tot);
System.out.println("AVERAGE MARK = " +avg);
}
}
OUTPUT

Exp:6
Aim:

Write a program to implement Array Sorting in java


Program:
import java.util.Arrays;
public class ArrayDemo {
public static void main(String[] args) {
String [] names={"Lion","Dog","Cat","Pig","Rabbit","Elephant","Gorilla"};
System.out.println("Array Elements before Sort ");
for(int i=0;i<names.length;i++)
{
System.out.println(names[i]);
}
Arrays.sort(names);//Sorting "names" array in ascending order
System.out.println("Array elememts after sorting ");
for(int i=0;i<names.length;i++)
{
System.out.println(names[i]);
}
}

OUTPUT
EXP 7
Aim:

Write a program to implement binary search in java using


array.
Program:-
import java.util.Arrays;
import java.util.Scanner;
public class ArraySearch {
public static void main(String[] args) {
int [] marks={10,8,45,32,67,88,4,5,33,21};
Scanner sc=new Scanner(System.in);
System.out.println("Enter the element to search ");
int key=sc.nextInt();
Arrays.sort(marks);
int result=Arrays.binarySearch(marks,key);
if(result!=-1)
System.out.println("Element found at position "+(result+1));
else
System.out.println("Element not found ");
}
}

Output:-
Enter the element to search
67
Element found at position 5s
Exp 8
Aim:

Write a program to input the name and basic salary of n employees.


If basic salary>=70000
HRA= 5% of basic salary
DA=10% of basic salary
Otherwise
HRA= 3% of basic salary
DA=1000
Calculate H.R A ,DA and Total Salary of all the employees.

Program:

import java.util.Scanner;
public class Employee {
public static void main(String args[])
{
Scanner sc= new Scanner(System.in);
System.out.println("Enter the NUMBER of Employees ");
int n=sc.nextInt();
String empname[]=new String[n];
double basicsalary[]=new double [n];
double HRA[]=new double [n];
double DA[]=new double [n];
double netsalary[]=new double [n];
for(int i=0;i<n;i++)
{
System.out.println("Enter the "+(i+1) +"employee name ");
empname[i]=sc.next();
System.out.println("Enter the basic salary of "+(i+1)+" employee ");
basicsalary[i]=sc.nextInt();
if(basicsalary[i] >= 70000)
{
HRA[i]=(0.05*basicsalary[i]);
DA[i]=(0.1*basicsalary[i]);
}
else
{
HRA[i]=(.03*basicsalary[i]);
DA[i]=1000;
}
netsalary[i]=basicsalary[i]+HRA[i]+DA[i];
}
System.out.println(" \n**********Employee details are**********\n ");
for(int i=0;i<n;i++)
{
System.out.println("Employename : "+empname[i]);
System.out.println("Employee basic salary : "+basicsalary[i]);
System.out.println("HRA : "+HRA[i]);
System.out.println("DA : "+DA[i]);
System.out.println("netsalary : "+netsalary[i]);
}
}
}

OUTPUT:
Enter the NUMBER of Employees

2
Enter the 1employee name
PRANAV
Enter the basic salary of 1 employee
45000
Enter the 2employee name
KALYANI
Enter the basic salary of 2 employee
35000

**********Employee details are**********


Employename : PRANAV
Employee basic salary : 45000.0
HRA : 1350.0
DA : 1000.0
netsalary : 47350.0
Employename : KALYANI
Employee basic salary : 35000.0
HRA : 1050.0
DA : 1000.0
netsalary : 37050.0
****************************************************************************************************************

EXP 9//Not to write in your record book .Whoever written its ok

Aim:

Write a menu driven program to implement


calculator
Program:
import java.util.Scanner;

public class calculator


{
int a;
int b;
int results;

public void read()


{
Scanner SC= new Scanner(System.in);
System.out.println("enter 2 number");
a=SC.nextInt();
b=SC.nextInt();

}
public void add()
{
System.out.println("sum ="+(a+b));
}
public void diff()

{
System.out.println("diff="+(a-b));
}
public void mul()

{
System.out.println("mul="+(a*b));
}
public void div()

{
System.out.println("div="+(a/b));
}
public static void main(String[] args)
{
int ch;
Scanner SC= new Scanner(System.in);
calculator ob=new calculator();
for(;;)
{
System.out.println("1.sum");
System.out.println("2.difference");
System.out.println("3.product");
System.out.println("4.division");
System.out.println("5.exit 0");
System.out.println("********");
System.out.println(" enter your choice");

ch=SC.nextInt();
switch(ch)
{
case 1: ob.read();ob.add();break;
case 2: ob.read();ob.diff();break;
case 3: ob.read();ob.mul();break;
case 4: ob.read();ob.div();break;
case 5: System.exit(0);
default: System.out.println("wrong choice");break;
}

}
}
}un:
1.sum
2.difference
3.product
4.division
5.exit 0
********
enter your choice
1
Object array
Factorial un:
1.sum
2.difference
3.product
4.division
5.exit 0
********
enter your choice
1

Constructor and disp


String handling
*******************************************************************************************************************

EXP 10
Aim:

Write a program in Java to enable user to handle divide by zero


exception.
Program:
public class ExceptionSample {
public static int divSample(int a,int b)
{
return (a/b);
}
public static void main(String[] args)
{
try {
int result;
result=divSample(10,0);
System.out.println(result);
}
catch(Exception e)
{
System.out.println("Exception Caught ");
}
finally
{
System.out.println("Exception Handled ");
}
}
}
Output:-
*******************************************************************************************************************

Exp 11:
Aim:
Write a program to check whether the entered number is odd or
even using static user defined method.
Program:
import java.util.Scanner;
public class EvenOdd
{
public static void main (String args[])
{
//creating Scanner class object
Scanner scan=new Scanner(System.in);
System.out.print("Enter the number: ");
//reading value from user
int num=scan.nextInt();
//method calling
findEvenOdd(num);
}
//user defined method
public static void findEvenOdd(int num)
{
//method body
if(num%2==0)
System.out.println(num+" is even");
else
System.out.println(num+" is odd");
}
}

OUTPUT
Enter the number: 12
s Enter the number: 99
99 is odd even s even

******************************************************************************************************************
EXP 12

Aim:
Write a Program to demonstrate having the main
method in another class
Program:

//Creating Student class.


class Student{
int id;
String name;
}
//Creating another class TestStudent1 which contains
the main method
class TestStudent1{
public static void main(String args[]){
Student s1=new Student();
System.out.println(s1.id);
System.out.println(s1.name);
}
}

OUTPUT
0
Null

*****************************************************************************************
EXP 13
Aim:

Write a Program to check whether a character entered is a vowel or


consonent

import java.util.Scanner;
public class vowelconso {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter a character: ");
char ch = scanner.next().charAt(0);

if((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z')) {
if(ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u' || ch
== 'A' || ch == 'E' || ch == 'I' || ch == 'O' || ch == 'U') {
System.out.println(ch + " is a vowel.");
} else {
System.out.println(ch + " is a consonant.");
}
} else {
System.out.println("Invalid input. Please enter an alphabet.");
}
Enter a character:
a
a is a vowel.

Enter a character:
b
b is a consonant.

EXP 14
Aim:

Write a Program to perform string manipulation on given string

class Stringdemo {
public static void main(String[] args) {
String myString = "Welcome to icsk senior";
System.out.println("UpperCase: " +
myString.toUpperCase());
System.out.println("LowerCase: " +
myString.toLowerCase());
System.out.println("position: " + myString.charAt(5));
System.out.println("join: " + myString.concat("\t Salmiya"));
System.out.println("contain: " + myString.contains("or"));
System.out.println("Endswith: " + myString.endsWith("ni"));
System.out.println("Compaire: " + myString.equals("senior"));
System.out.println("equalbutignorecase: " +
myString.equalsIgnoreCase("Welcome to icsk senior"));
}
}
Output :
UpperCase: WELCOME TO ICSK SENIOR
LowerCase: welcome to icsk senior
position: m
join: Welcome to icsk senior Salmiya
contain: true
Endswith: false
Compaire: false
equalbutignorecase: true

After 14 practicals
sql query :
15Flight table with query

16.Item Table sql query

6)count total records of Items in Item table


7)find total of Price in item table
17.Employee table Query

e.Find Average salary in above table


f.find total salary

18.sql Query on order table

19.sql query on store and owner

You might also like