Java Practical File 2024
Java Practical File 2024
SUBMITTED BY
Name:- DNYANESHWARI
Class:- XII COMMERCE
Board Roll No.:-
CERTIFICATE
2
INDEX
Sr.no Topic Page
no.
1. Write a program in Java to implement the
formula: Volume = length * width *
height.
2. Write a program in Java to find the result
of arithmetic expressions.
3
11. Write a program to create three threads
in Java and set their priority.
4
PROGRAMMING
IN JAVA
5
PRACTICAL NO: 1
Aim:
Write a program in Java to implement the formula: Volume = length * width * height.
Program:
import java.util.Scanner;
double l= Double.parseDouble(length);
double b= Double.parseDouble(breadth);
double h= Double.parseDouble(height);
double Volume=l*b*h;
6
Output:
7
PRACTICAL NO: 2
Aim:
Write a program in Java to find the result of the following expressions.
i) a%b
ii) a /= b
iii) (a + b * 100)/10
iv) a++
Program:
public class Practical2{
int a=20;
int b=30;
Output:
PRACTICAL NO: 3
8
Aim:
Write a program in Java to print the square of every alternate number of an array.
Program:
public class Practical3{
System.out.println(numbers[i]*numbers[i]);
Output:
9
PRACTICAL NO: 4
Aim:
Write a java application to find the maximum of three integers entered by the user.
Program:
import Java . util . Scanner;
int x, y, z ;
x = s . nextInt ( ) ;
y = s . nextInt ( ) ;
z = s . nextInt ( ) ;
else
10
Output:
PRACTICAL NO: 5
Aim:
Accept any three names from the keyboard and display “Your Name is” name.
11
Program:
import java.util.Scanner;
Output:
12
PRACTICAL NO: 6
Aim:
Java Program to check Even or Odd number.
Program:
import java.util.Scanner;
13
public static void main(String[] args)
int num;
num = input.nextInt();
if (num%2==0)
else
Output:
PRACTICAL NO: 7
Aim:
WAP to accept 5 names from user and store them in an array and print them on screen in
order.
Program:
package helloworld;
import java.util.Scanner;
import java.util.Arrays;
14
public class Practical7{
System.out.println("Enter 10 names");
names[i] = in.nextLine();
Arrays.sort(names);
System.out.println(name);
Output:
15
PRACTICAL NO: 8
Aim:
WAP to print highest and lowest total marks of 10 students using Arrays.
Program:
16
/*
*/
package helloworld;
import java.util.Arrays;
/**
* @author student
*/
/**
*/
Arrays.sort(marks);
System.out.print("lowest marks");
System.out.println(marks[0]);
System.out.print("Highest marks");
System.out.println(marks[marks.length-1]);
17
Output:
PRACTICAL NO: 9
Aim:
Write a program in Java to enable user to handle divide by zero exception.
.
Program:
import java.util.Scanner;
18
/**
*/
return dividend/divisor;
try{
System.out.println(number);
catch(Exception e){
Output:
19
PRACTICAL NO: 10
Aim:
Write a program in Java to create class Triangle with the data members base, height, area
and color. The members base, height, area are of type double and color is of type string.
Write getter and setter methods for base, height and color, and write method to
compute_area (). Create two object of class Triangle, compute their area, and compare
their area and color. If area and color both are same for the objects then display "Matching
Triangles" otherwise display "Non matching Triangles".
Program:
public class Practical10{
private double base;
private double height;
20
private double area;
private String color;
double getBase( ) {
return base;
}
double getHeight( ) {
return height;
}
String getColor ( ) {
return color;
}
void setBase(double newbase) {
base = newbase;
}
void setHeight(double newheight) {
base = newheight;
}
void setColor(String newcolor) {
color = newcolor;
}
double ComputeArea ( ) {
area= 0.5*base*height;
return area;
}
public static void main(String[] args) {
Triangle T1 = new Triangle();
Triangle T2 = new Triangle();
T1.setBase(40.0);
T1.setColor("green");
T1.setHeight(20.0);
T2.setBase(50.0);
T2.setColor("blue");
T2.setHeight(10.0);
T1.ComputeArea();
T2.ComputeArea();
if(T1.getColor()==T2.getColor()&&T1.ComputeArea()==T2.ComputeArea())
21
System.out.println("Matching Triangles");
else
System.out.println("Non Matching Triangles");
}
}
Output:
PRACTICAL NO: 11
Aim:
Write a program to create three threads in Java and set their priority.
Program:
package multithread;
/**
*
* @author student
*/
public class Practical11{
public void run(){
String threadName = Thread.currentThread().getName();
System.out.println(threadName);
22
}
t1.setPriority(10);
t1.setPriority(10);
t1.setPriority(10);
t1.start();
t2.start();
t3.start();
}
}
Output:
23
PRACTICAL NO: 12
Aim:
Write a Java code to search an element from the array using binary search technique.
Program:
package helloworld;
import java.util.Arrays;
import java.util.Scanner;
24
if(index<=-1)
System.out.println("Element not found");
else
System.out.println("Element found at position" + (index+1));
}
}
Output:
25
PRACTICAL NO: 13
Aim:
Program to swap two numbers.
Program:
package booktest;
import java.util.Scanner;
public class Practical13{
public static void main(String[] args) {
// TODO code application logic here
int x, y, t;
Scanner sc = new Scanner(System.in);
System.out.println("Enter the value of X and Y");
x = sc.nextInt();
y = sc.nextInt();
System.out.println("before swapping numbers:"+x +" "+y);
t = x;
x = y;
y = t;
System.out.println("After swapping: "+x +" "+y);
System.out.println();
}
}
26
Output:
27
PRACTICAL NO: 14
Aim:
Write a java program for calculator which performs addition, subtraction, multiplication and
division.
Program:
import java.util.Scanner;
public class Practical14{
public static void main(String[] args){
int a,b,c;
int choice;
Scanner scanner = new Scanner(System.in);
while(true) {
System.out.println("Press 1 for Addition");
System.out.println("Press 2 for Subtraction");
System.out.println("Press 3 for Multiplication");
System.out.println("Press 4 for Division");
System.out.println("Press 5 to Quit\n \n ");
System.out.println("Make your choice");
choice = scanner.nextInt();
switch (choice) {
case 1:
System.out.println("Enter the first number ");
a = scanner.nextInt();
System.out.println("Enter the second number");
b = scanner.nextInt();
c = a + b;
System.out.println("The sum of the numbers is = " + c +"\n");
break;
case 2:
System.out.println("Enter the first number ");
28
a = scanner.nextInt();
System.out.println("Enter the second number");
b = scanner.nextInt();
c = a - b;
System.out.println("The difference of the numbers is = " + c +"\n");
break;
case 3:
System.out.println("Enter the first number");
a = scanner.nextInt();
System.out.println("Enter the second number");
b = scanner.nextInt();
c = a * b;
System.out.println("The product of the numbers is = " + c + "\n");
break;
case 4:
System.out.println("Enter the first number");
a = scanner.nextInt();
System.out.println("Enter the second number");
b = scanner.nextInt();
c = a / b;
System.out.println("The quotient is = " + c +"\n");
break;
case 5:
System.exit(0);
default:
System.out.println("Invalid choice!!! Please make a valid choice. \\n\\n");
}
}
}
}
29
Output:
PRACTICAL NO: 15
30
Aim:
Write a Java program with method to print cube of a number.
Program:
package hellworld5;
import java.util.Scanner;
public class Practical15{
public static void cube(int num) {
int cube =num*num*num;
System.out.println("cube of"+num+ "is"+cube);
}
public static void main(String[] args){
Scanner scan = new Scanner (System.in);
System.out.println("Enter number");
int number =scan.nextInt();
cube(number);
}
}
Output:
31
SQL Queries
32
OUTPUT:
33
mysql> DESCRIBE Student;
OUTPUT:
OUTPUT:
34
mysql> SELECT * FROM Student;
OUTPUT:
10. Change the newly added Games column to hold integers(in place of character
data)
mysql> ALTER TABLE Student MODIFY games INTEGER;
OUTPUT:
OUTPUT:
35
13.Displays two columns: Roll numbers and names of all the students.
OUTPUT:
OUTPUT:
mysql> SELECT DISTINCT Marks1 FROM Student;
36
16. Using Column Alias
17. Display the names and marks of all those students who have secured marks above
80.
18. Display Roll numbers and names of students who have secured marks above 70
but below 80.
37
AND Marks1 < 80;
OUTPUT:
20.Display details of students who have their names ending with 'Sen’;
21. Display rows from the table Student that have names not starting with 'G'.
38
22. Display data of students in ascending order of their marks.
mysql> SELECT * FROM Student ORDER BY Marks1;
OUTPUT:
24. One of the students with Roll number 14 has left the school and Ms. Sujata wants
to delete his/her row.
mysql> DELETE FROM Student WHERE Rollno = 14;
OUTPUT:
39