Java Practical File
Java Practical File
MANAGEMENT SCHOOL
(DEPT. OF INFORMATION TECHNOLOGY)
PRACTICAL-1
Write down the steps to compile and run a Java Program. Write a program to print Hello
World.
PRACTICAL-2
A. Write a program to read name, age, phone no, gender and CGPA from user
and print the value.
import java.util.Scanner;
public class userinput
{
public static void main(String args[]){
System.out.println("Khush Bhatt 02321402020\n");
Scanner s= new Scanner(System.in);
String name,gender;
int phone,cgpa, age;
System.out.println("Enter name: \n");
name = s.nextLine();
System.out.println("Enter gender: \n");
gender=s.nextLine();
System.out.println("Enter age: \n");
age = s.nextInt();
System.out.println("Enter phone number: \n");
phone=s.nextInt();
System.out.println("Enter cgpa: \n");
cgpa = s.nextInt();
System.out.println("NAME: "+ name +" "+ "AGE: "+ age +" "+"gender:
"+gender+" "+"phone number: "+phone+" "+"CGPA: "+ cgpa);
}}
B. WAP to find the greatest and smallest of 3 numbers using if else statement
import java.util.Scanner;
public class greatest{
public static void main(String args[]){
System.out.println("Khush bhatt 02321402020\n");
int num1,num2,num3;
Scanner s=new Scanner(System.in);
System.out.println("Enter 1st number: \n");
num1=s.nextInt();
System.out.println("Enter the 2nd number: \n");
num2=s.nextInt();
System.out.println("Enter the 3rd Number: \n");
num3=s.nextInt();
if(num1>num2 && num1>num3){
System.out.println(num1+" "+"is the greatest");}
else if(num2>num1 && num2>num3){
System.out.println(num2+" "+"is the greatest");}
else{
System.out.println(num3+" "+"is the greatest");}
}
}
import java.util.Scanner;
public class small{
public static void main(String args[]){
System.out.println("Shubham Patel 05121402020\n");
Scanner s=new Scanner(System.in);
int num1,num2,num3;
System.out.println("Enter 1st number: ");
num1=s.nextInt();
System.out.println("Enter the 2nd number: ");
num2=s.nextInt();
System.out.println("Enter the 3rd Number: ");
num3=s.nextInt();
if(num1<num2 && num1<num3){
System.out.println(num1+" "+"is the smallest");}
else if(num2<num1 && num2<num3){
System.out.println(num2+" "+"is the smallest");}
else{
System.out.println(num3+" "+"is the smallest");}
}
}
C. Write a Java program that takes three numbers as input to calculate and print
the average of the numbers.
import java.util.Scanner;
public class average{
public static void main(String args[]){
System.out.println("Khush Bhatt 02321402020\n");
int num1,num2,num3;
double avg=0.0;
Scanner s=new Scanner(System.in);
System.out.println("Enter 1st number: \n");
num1=s.nextInt();
System.out.println("Enter the 2nd number: \n");
num2=s.nextInt();
System.out.println("Enter the 3rd Number: \n");
num3=s.nextInt();
avg=(num1+num2+num3)/3;
System.out.println("The average of 3 numbers is: "+avg);
}
}
import java.util.Scanner;
public class factorial{
public static void main(String args[]){
Scanner s=new Scanner(System.in);
System.out.println("Khush Bhatt 02321402020\n");
int num,i,fact=1;
System.out.println("Enter the number: \n");
num=s.nextInt();
for(i=1;i<=num;i++){
fact=fact*i;}
System.out.println("Factorial of the number is: "+fact);
}
}
E. WAP to check whether a given number is Armstrong or not
import java.util.Scanner;
public class armstrong
{
public static void main(String[] args)
{
System.out.println("Khush Bhatt 02321402020\n");
int x=0,a,temp;
int n;
System.out.println("Enter number");
Scanner s=new Scanner(System.in);
n=s.nextInt();
temp=n;
while(n>0)
{
a=n%10;
n=n/10;
x=x+(a*a*a);
}
if(temp==x)
System.out.println(temp+" is an armstrong number");
else
System.out.println(temp+" is not an armstrong number");
}
}
F. WAP to check leap year.
import java.util.Scanner;
public class LeapYear {
public static void main(String[] args){
System.out.println("Khush bhatt 05121402020");
int year;
System.out.println("Enter an Year :: ");
Scanner sc = new Scanner(System.in);
year = sc.nextInt();
G. Write a Java program that checks whether a given string is a palindrome or not.
import java.util.Scanner;
class pl
{
public static void main(String args[])
{
System.out.println("Khush bhatt 02321402020\n");
int l,i;
String str,rev="";
System.out.println("Enter the string::");
Scanner s = new Scanner(System.in);
str=s.next();
l=str.length();
for(i=l-1;i>=0;i--)
{
rev=rev+str.charAt(i);
}
if(str.equals(rev))
{
System.out.println(str+" is a palindrome");
}
else
System.out.println(str+" is not a palindrome");
}
}
PRACTICAL-3
WAP to display integer and string values of an array using for each loop.
import java.util.Scanner;
public class loop
{
public static void main(String args[])
{
System.out.println("Khush Bhatt 02321402020");
int arr[]={2,3,4,5,6,7};
String arr1[]={"a","e","i","o","u"};
for(int i:arr)
{
System.out.println(i);
}
for(String j:arr1){
System.out.println(j);}
}
}
PRACTICAL-4
WAP to read the array dynamically, sort the array and display the sorted array.
import java.util.Scanner;
import java.util.Arrays;
public class arraydn
{
public static void main(String args[])
{
int size,i;
System.out.println("enter the size of an array:");
Scanner s= new Scanner(System.in);
size=s.nextInt();
int[] a=new int[size];
System.out.println("Enter elements in the array :");
for(i=0;i<size;i++)
{
a[i]=s.nextInt();
}
System.out.println(Arrays.toString(a));
}}
PRACTICAL-5
Write a Java Program to demonstrate use of nested class.
class outer1
{
void outmethod()
{
class inner
{
void inmethod()
{
System.out.println("Inner class");
}
}
inner i=new inner();
i.inmethod();
}
public static void main(String args[])
{
outer c=new outer();
c.outmethod();
}
}
PRACTICAL-6
Java program to demonstrate example of static variable and static method.
import java.util.Scanner;
class static1
{
static int a;
static int fact(int a)
{
int b=1;
for(int i=a;i>1;i--)
{
b=b*i;
}
System.out.println("Factorial of "+a+" is : "+b);
return b;
}
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter number to find factorial");
a=sc.nextInt(); fact(a);
}
}
PRACTICAL-7
WAP to implement Single Inheritance
class employee
{
int salary=10000;
}
public class programmer extends employee
{
int bonus=5000;
public static void main(String args[]){
System.out.println("Shubham Patel 05121402020");
programmer p=new programmer();
System.out.println("Salary: "+p.salary);
System.out.println("Bonus: "+p.bonus);
}
}
PRACTICAL-8
WAP to implement Multilevel and Hierarchy Inheritance
class dog
{
String name="dog";
}
class cat extends dog
{
String name1="cat";
}
public class rat extends cat
{
String name2="rat";
public static void main(String args[]){
System.out.println("Shubham Patel 05121402020\n");
rat r=new rat();
System.out.println(r.name);
System.out.println(r.name1);
System.out.println(r.name2);
}
}
PRACTICAL-9
WAP to create a package in java
package pack2;
public class C{
public void msg(){
System.out.println("Enter a number: ");
}
}
package pack3;
import pack2.*;
import java.util.Scanner;
class D{
public static void main(String args[]){
C obj=new C();
obj.msg();
int num;
Scanner s=new Scanner(System.in);
num=s.nextInt();
System.out.println("The number entered by you is: "+num);
}
}
PRACTICAL-10
WAP to illustrate the concept of Method Overriding and constructor overriding
class Vehicle{
void run(){System.out.println("Vehicle is running");}
}
public class Bike2 extends Vehicle{
void run()
{System.out.println("Bike is running safely");}
import java.util.*;
import java.lang.*;
public class program1
{
}
PRACTICAL-13
WAP to demonstrate the use of implementing interfaces
import java.util.Scanner;
interface area
{
public void dimensions();
public void area();
}
public class Inter implements area
{
int length,breadth,area;
public void dimensions()
{
Scanner s=new Scanner(System.in);
System.out.print("Enter length: ");
length=s.nextInt();
System.out.print("Enter breadth: ");
breadth=s.nextInt();
}
public void area()
{
area=length*breadth;
System.out.print("\nArea :"+area);
}
public static void main(String[] args)
{
System.out.println("\nName:- khush bhatt Enroll. no.:-
02321402020 \n");
Inter obj=new Inter();
obj.dimensions();
obj.area();
}
}
PRACTICAL-14
WAP to implement the concept of exception handling using predefined and user
define exceptions.
A. PREDINED EXCEPTION
import java.util.Scanner;
class MyException extends Exception
{
try
{
Scanner emp=new Scanner(System.in);
String name,dep;
int salary;
System.out.print("\nEnter your Name:- ");
name=emp.nextLine();
System.out.print("\nEnter your Department Name:-");
dep=emp.nextLine();
System.out.print("\nEnter your Salary:-");
salary=emp.nextInt();
System.out.println("Your Name is: "+name);
System.out.println("Your Department Name is: "+dep);
System.out.println("Your Salary is: "+salary);
}
catch(Exception obj)
{
System.out.println("Exception Occured:- " + obj);
}
}
}
B . CUSTOM EXCEPTION
// main method
public static void main(String args[])
{
try
{
// calling the method
validate(13);
}
catch (InvalidAgeException ex)
{
System.out.println("Caught the exception");
import java.lang.*;
// Method 1
// Whenever the start() method is called by a thread
// the run() method is invoked
public void run()
{
// the print statement
System.out.println("Inside the run() method");
}
// 1st Thread
// Displaying the priority of the thread
// using the getPriority() method
System.out.println("Priority of the thread th1 is : " + th1.getPriority());
// 2nd Thread
// Display the priority of the thread
System.out.println("Priority of the thread th2 is : " + th2.getPriority());
// 3rd Thread
// // Display the priority of the thread
System.out.println("Priority of the thread th2 is : " + th2.getPriority());
// 6
System.out.println("Priority of the thread th1 is : " + th1.getPriority());
// 3
System.out.println("Priority of the thread th2 is : " + th2.getPriority());
// 9
System.out.println("Priority of the thread th3 is : " + th3.getPriority());
// Main thread
import java.util.Set;
// Main Class
public class Multithread1 {
public static void main(String[] args)
{
System.out.println("\nName:- khush bhatt Enroll. no.:-
023321402020 \n");
int n = 8; // Number of threads
for (int i = 0; i < n; i++) {
MultithreadingDemo object
= new MultithreadingDemo();
object.start();
}
}
}
B. MULTITHREADING BY IMPLEMENTS RUNNABLE THREAD
class MultithreadingDemo implements Runnable {
public void run()
{
try {
// Displaying the thread that is running
System.out.println(
"Thread " + Thread.currentThread().getId()
+ " is running");
}
catch (Exception e) {
// Throwing an exception
System.out.println("Exception is caught");
}
}
}
// Main Class
class Multithread2 {
public static void main(String[] args)
{
System.out.println("\nName:- khush bhatt Enroll. no.:-
02321402020 \n");
int n = 8; // Number of threads
for (int i = 0; i < n; i++) {
Thread object
= new Thread(new MultithreadingDemo());
object.start();
} }
PRACTICAL-18
WAP to explain all the string operations
import java.util.Scanner;
public class string
{
public static void main(String[] args)
{
Scanner obj=new Scanner(System.in);
String string1,string2,string3;
System.out.println("\nName:- khush bhatt Enroll. no.:-
02321402020 \n");
// get the length of string
System.out.println("***Length Operation***\n");
System.out.print("Enter the value in First String: ");
string1=obj.nextLine();
int length = string1.length();
System.out.println("Length: " + length);
System.out.println("\n***Concatination Operation***\n");
System.out.print("Enter the value in Second String to concatenate: ");
string2=obj.nextLine();
// join two strings
String joinedString = string1.concat(string2);
System.out.println("Joined String: " + joinedString);
System.out.println("\n***Comparision Operation***\n");
System.out.print("Enter the value in Third String for Comparision: ");
string3=obj.nextLine();
}
}
PRACTICAL-19
WAP to explain all the string operations using string buffer method.
import java.util.Scanner;
System.out.println("***append() method***\n");
//The append() method concatenates the given argument with this String.
StringBuffer sb=new StringBuffer("Hello ");
sb.append("Java");//now original string is changed
System.out.println(sb);//prints Hello Java
System.out.println("\n***delete() method***\n");
//The delete() method of the StringBuffer class deletes the String from the
specified beginIndex to endIndex.
StringBuffer z=new StringBuffer("Hello");
z.delete(1,3);
System.out.println(z);//prints Hlo
A. TO CREATE A FILE
import java.awt.*;
public class Tes extends java.applet.Applet
{
public void init()
{
setLayout(new FlowLayout(FlowLayout.LEFT));
add(new Button("Register"));
add(new Button("Exit"));
}}
HTML Code: -
<html>
<head><title>Register</title></head>
<body>
<applet code="Tes.class" width=230 height=300></applet>
</body>
</html>
PRACTICAL-22
WAP to explain important fields of AWT.
HTML Code: -
<html>
<body>
<applet code="cont.class" height=1000 width=1000></applet>
</body>
</html>
PRACTICAL-23
WAP to explain important fields of event handling.
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
public class MouseExample extends MouseAdapter
{
Frame f;
MouseExample()
{
f=new Frame("Mouse Adapter");
f.addMouseListener(this);
f.setSize(300,300);
f.setLayout(null);
f.setVisible(true);
}
public void mouseClicked(MouseEvent e)
{
Graphics g=f.getGraphics();
g.setColor(Color.BLUE);
g.fillOval(e.getX(),e.getY(),30,30);
}
public static void main(String args[])
{
System.out.println("\nName:- Sameer Enroll. no.:- 04321402020 \n");
new MouseExample();
}
}
PRACTICAL-24
Write a GUI Program to Add Two numbers Using AWT and event
handling
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
try
{
Class.forName("com.mysql.jdbc.Driver");
System.out.println("Driver loaded");
// query="select * from student ";
con =DriverManager.getConnection("jdbc:mysql://localhost:3306/student"
, "root", "Sameer@000");
System.out.println("Connection established");
}
catch (ClassNotFoundException | SQLException e)
{
System.out.println("Exception caught " + e.getMessage());
}
}
}
PRACTICAL-26
WAP to make a login GUI using TextField, Password Field and Login Button using
Swings.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.lang.Exception;
CreateLoginForm()
{
userLabel = new JLabel();
userLabel.setText("Username");
textField1 = new JTextField(15);
else
{
System.out.println("Please enter valid username and password");
}
}
}
catch (Exception e)
{
JOptionPane.showMessageDialog(null, e.getMessage());
}
}
}