0% found this document useful (0 votes)
20 views39 pages

Java Practical File 2024

hihi

Uploaded by

himanshii.naggg
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
0% found this document useful (0 votes)
20 views39 pages

Java Practical File 2024

hihi

Uploaded by

himanshii.naggg
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 39

SĀNDIPANI SCHOOL

Hazari Pahad, Nagpur


PRACTICAL
ASSIGNMENTS

SUBJECT:- INFORMATION TECHNOLOGY


SUBJECT CODE:- 802
SESSION:- 2024-2025

SUBMITTED BY
Name:- DNYANESHWARI
Class:- XII COMMERCE
Board Roll No.:-
CERTIFICATE

This is to certify that Dnyaneshwari of class

XII has successfully completed the

practical assignments for the subject

Information Technology (sub.code-

802) for the session 2024-2025.

Signature of Teacher Signature of


Principal

Signature of External Examiner

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. Write a program in Java to print the


square of every alternate number of an
array.

4. Write a java application to find the


maximum of three integers entered by
the user.

5. Accept any three names from the


keyboard and display “Your Name is”
name.

6. Java Program to check Even or Odd


number.

7. WAP to accept 5 names from user and


store them in an array and print them on
screen in order.

8. WAP to print highest and lowest total


marks of 10 students using Arrays.

9. Write a program in Java to enable user to


handle divide by zero exception.

10. Write a program in Java to create class.

3
11. Write a program to create three threads
in Java and set their priority.

12. Write a Java code to search an element


from the array using binary search
technique.

13. Program to swap two numbers.

14. Write a java program for calculator which


performs addition, subtraction,
multiplication and division.
15. Write a Java program with method to
print cube of a number.

16. SQL Queries

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;

public class Practical1{

public static void main(String[] args){

Scanner user_input= new Scanner(System.in);

System.out.println("Enter length of cuboid");

String length = user_input.next();

double l= Double.parseDouble(length);

System.out.println("Enter breadth of cuboid");

String breadth = user_input.next();

double b= Double.parseDouble(breadth);

System.out.println("Enter height of cuboid");

String height = user_input.next();

double h= Double.parseDouble(height);

double Volume=l*b*h;

System.out.println("Volume of cuboid: "+Volume);

6
Output:

7
PRACTICAL NO: 2
Aim:
Write a program in Java to find the result of the following expressions.

(Assume a = 20 and b = 30)

i) a%b

ii) a /= b

iii) (a + b * 100)/10

iv) a++

Program:
public class Practical2{

public static void main (String[ ] args) {

int a=20;

int b=30;

System.out.println("The result of a%b is : " +(a%b));

System.out.println("The result of a/=b is : “ +(a/=b));

System.out.println("The result of (a+b*100)/10 is : " +((a+b*100)/10));

System.out.println("The result of a++ is : " +(a++));

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{

public static void main(String[] args){

int [] numbers = {10,20,14,12,1,8,40,90,100,7};

for(int i=0; i<numbers.length; i=i+2)

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;

public class Practical4{

public static void main ( String [ ] args ) {

int x, y, z ;

Scanner s = new Scanner ( System.in) ;

System . out . print (“Enter the first number:”) ;

x = s . nextInt ( ) ;

System . out . print (“Enter the second number:”) ;

y = s . nextInt ( ) ;

System . out . print (“Enter the third number”) ;

z = s . nextInt ( ) ;

if ( x > y && x > z )

System . out . println (“ Largest number is:” + x ) ;

else if ( y > z && y > x )

System . out . println (“ Largest number is:” + y ) ;

else if ( z > x && z > y )

System .out . println (“Largest number is:”+ z ) ;

else

System . out . println (“check input is correct”) ; } }

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;

public class Practical5{

public static void main(String[]args)

Scanner user_input=new Scanner(System.in);

System.out.println("Enter first name");

String name1= user_input.next();

System.out.println("Enter second name");

String name2= user_input.next();

System.out.println("Enter third name");

String name3= user_input.next();

System.out.println("Your Name is "+ name1);

System.out.println("Your Name is "+ name2);

System.out.println("Your Name is "+ name3);

Output:

12
PRACTICAL NO: 6
Aim:
Java Program to check Even or Odd number.

Program:
import java.util.Scanner;

public class Practical6{

13
public static void main(String[] args)

int num;

System.out.println("Enter an Integer number: ");

Scanner input= new Scanner(System.in);

num = input.nextInt();

if (num%2==0)

System.out.println("Entered number is even");

else

System.out.println("Entered number is odd");

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{

public static void main(String[] args){

Scanner in = new Scanner(System.in);

String names[] = new String[10];

System.out.println("Enter 10 names");

for (int i = 0; i < names.length; i++) {

names[i] = in.nextLine();

Arrays.sort(names);

for (String name : 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
/*

* To change this template, choose Tools | Templates

* and open the template in the editor.

*/

package helloworld;

import java.util.Arrays;

/**

* @author student

*/

public class Practical8{

/**

* @param args the command line arguments

*/

public static void main(String[] args) {

double[] marks = {103,144,256.5,346,387.5,120.3,145.7,390.5,100.8,230.7};

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;

public class Practical9{

18
/**

* @param args the command line arguments

*/

static int divide(int dividend, int divisor)

return dividend/divisor;

public static void main(String[] args) {

System.out.println("enter the dividend and divisor");

Scanner user_input = new Scanner(System.in);

int divid = user_input.nextInt();

int divis = user_input.nextInt();

try{

int number = divide(divid,divis);

System.out.println(number);

catch(Exception e){

System.out.println("caught Exception while trying to divide 100 byzero ;"+


e.getMessage());

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
}

public static void main(String[] args) {


MultiThread t1 = new MultiThread();
MultiThread t2 = new MultiThread();
MultiThread t3 = new MultiThread();

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;

public class Practical12{


public static void main(String args){
int[] arr ={2,4,5,7,6,9,12,15,18};
System.out.println("Given array element:");
for (int i=0; i<arr.length ;i++ ) {
System.out.print(arr[i] + “ “);
}
System.out.println();
Scanner scn = new Scanner(Syster.in);
System.out.print("\nEnter the element to search:");
int key = scn.nextInt();
int index = Arrays.binarySearch(arr,key);

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

Objective: Understanding the use of MySQL queries.

1. Write a command to display the current month.

mysql> Select month(curdate());

32
OUTPUT:

2. Write a query to find out the result of 6 3.

mysql> select pow(6,3);


OUTPUT:

3. Create a Database named MYSCHOOL.

mysql> Create database MYSCHOOL;


OUTPUT:

4. Write command to show the Tables in the MYSCHOOL Database.


mysql> Use MYSCHOOL;
mysql> Show tables;
OUTPUT:

5. Create a table STUDENT in the database MYSCHOOL.

mysql> CREATE TABLE Student(Rollno INTEGER, Name VARCHAR(25),


Gender CHAR(1),Marks1 DECIMAL(4,1));
OUTPUT:

6. Display the structure of the table STUDENT.

33
mysql> DESCRIBE Student;
OUTPUT:

7. Add some rows in the Student table.


mysql> INSERT INTO Student VALUES (1,'Mona Sehgal','M',93);
mysql> INSERT INTO Student VALUES (2,'Gurpreet Kaur','F',91);
mysql> INSERT INTO Student VALUES (3,'Monica Rana','F',93);
mysql> INSERT INTO Student VALUES (4,'Jatinder Sen','M',78);
mysql> INSERT INTO Student VALUES (5,'George Jacob','M',76);
mysql> INSERT INTO Student VALUES (6,'Deepa Bhandari','F',77);
mysql> INSERT INTO Student VALUES (7,'Akshay Nath','M',65);
OUTPUT:

8. Add a column named Games.


mysql> ALTER TABLE Student ADD Games VARCHAR(20);

OUTPUT:

9. Display the contents of the STUDENT table.

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:

11.Deletes the Games column


mysql> ALTER TABLE Student DROP Games;

OUTPUT:

12. Display Roll numbers of all her students


mysql> SELECT Rollno FROM Student;
OUTPUT:

35
13.Displays two columns: Roll numbers and names of all the students.
OUTPUT:

14. Eliminating duplicate values.

OUTPUT:
mysql> SELECT DISTINCT Marks1 FROM Student;

15.Display marks of all students increased by 5.


OUTPUT:
mysql> SELECT Marks1+5 FROM Student;

36
16. Using Column Alias

mysql> SELECT Marks1 AS "Marks Secured" FROM Student;


OUTPUT:

17. Display the names and marks of all those students who have secured marks above
80.

mysql> SELECT Name,Marks1 FROM Student WHERE Marks1 > 80;

18. Display Roll numbers and names of students who have secured marks above 70
but below 80.

mysql> SELECT Rollno, Name,Marks1 FROM Student WHERE


Marks1>70

37
AND Marks1 < 80;
OUTPUT:

19. Display data of Students whose marks are 68 or 76 or 78.


mysql> SELECT Rollno, Name, Marks1 FROM Student WHERE Marks1
IN (68,76,78);
OUTPUT:

20.Display details of students who have their names ending with 'Sen’;

mysql> SELECT * FROM Student WHERE Name LIKE '%Sen';


OUTPUT:

21. Display rows from the table Student that have names not starting with 'G'.

mysql> SELECT * FROM Student WHERE Name NOT LIKE 'G%';


OUTPUT:

38
22. Display data of students in ascending order of their marks.
mysql> SELECT * FROM Student ORDER BY Marks1;
OUTPUT:

23. Change the marks of the student 'Monica Rana' to 94.

mysql> UPDATE Student SET Marks1 = 94 WHERE name = 'Monica


Rana';
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

You might also like