JAGANNATH INTERNATIONAL
MANAGEMENT SCHOOL
(DEPT. OF INFORMATION TECHNOLOGY)
(P-VII) Java Lab Session: Feb 2022-June
2022
BCA-252
Submitted To Submitted By:
Ms. Minal Maheshwari Name: Khush Bhatt
Assistant Professor - Enroll. No.: 02321402020
IT
JIMS, VASANT KUNJ BCA- 4E
INDEX
S. No. Name of the Practical Date Signature
1 Write down the steps to compile and run a
Java Program? Write a program to print
Hello World.
To study the basics of programming
a. Write a program to read name, age, phone
no, gender and CGPA from user and print
the value.
b. WAP to find the largest and smallest of 3
numbers using if else statement
2 c. Write a Java program that takes three
numbers as input to calculate and print the
average of the numbers.
d. WAP to calculate Factorial of a number
e. WAP to check whether a given number is
Armstrong or not
f. WAP to check leap year.
g. Write a Java program that checks whether
a given string is a palindrome or not.
3 WAP to display integer values of an array
using for each loop.
4 WAP to print String Elements in array using
for each loop
5 Write a Java Program to implement array of
objects.
6 WAP to read the array dynamically, sort the
array and display the sorted array.
7 Write a Java Program to demonstrate use of
nested class.
8 Program to find Area of circle, Rectangle
and Triangle using different methods
9 Java program to demonstrate example of
static variable and static method.
10 WAP to implement Single Inheritance
11 WAP to implement Multilevel and
Hierarchy Inheritance
12 WAP to create a package in java
13 WAP to illustrate the concept of Method
Overriding
14 WAP to demonstrate the concept of Abstract
Class
15 Write a program to demonstrate use of
implementing interfaces.
16 Write a program to demonstrate use of
extending interfaces
17 Write an program using try, catch, throw and
finally
18 Write a program to implement the concept of
Exception Handling using predefined exception
19 Write a program to implement the concept of
Exception Handling by creating user-defined
exceptions.
20 Write a program to demonstrate thread priority.
21 WAP to implement the concept of
Multithreading
22 WAP to demonstrate Method overloading
23 WAP to show constructor overloading
24 Write a program to implement all string
operations.
25 Write a program to implement all string
operations using String Buffer Methods.
26 WAP to read the contents from file and writing
contents into a file
27 Develop an applet that displays a simple
message.
28 Write a GUI Program to Add Two Numbers
Using AWT and event handling.
WAP to make a login GUI using TextField,
29
PasswordField and Login Button using Swings.
30 Write a java program that connects to a
database using JDBC and perform add, delete
and retrieve operations.
PRACTICAL-1
Write down the steps to compile and run a Java Program. Write a program to print Hello
World.
Steps to compile and run a Java Program-
Open command prompt and set the directory by typing “drive name:”, For example-
d: .
Then go to the folder where you saved the program,
type “cd foldername”.
Type “javac [Link]” to compile the code.
Now type “java programname” to run the program.
public class hlloworld
{
public static void main(String args[]){
[Link]("Khush Bhatt , 02321402020\n");
[Link]("Hello World");
}
}
PRACTICAL-2
A. Write a program to read name, age, phone no, gender and CGPA from user
and print the value.
import [Link];
public class userinput
{
public static void main(String args[]){
[Link]("Khush Bhatt 02321402020\n");
Scanner s= new Scanner([Link]);
String name,gender;
int phone,cgpa, age;
[Link]("Enter name: \n");
name = [Link]();
[Link]("Enter gender: \n");
gender=[Link]();
[Link]("Enter age: \n");
age = [Link]();
[Link]("Enter phone number: \n");
phone=[Link]();
[Link]("Enter cgpa: \n");
cgpa = [Link]();
[Link]("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 [Link];
public class greatest{
public static void main(String args[]){
[Link]("Khush bhatt 02321402020\n");
int num1,num2,num3;
Scanner s=new Scanner([Link]);
[Link]("Enter 1st number: \n");
num1=[Link]();
[Link]("Enter the 2nd number: \n");
num2=[Link]();
[Link]("Enter the 3rd Number: \n");
num3=[Link]();
if(num1>num2 && num1>num3){
[Link](num1+" "+"is the greatest");}
else if(num2>num1 && num2>num3){
[Link](num2+" "+"is the greatest");}
else{
[Link](num3+" "+"is the greatest");}
}
}
import [Link];
public class small{
public static void main(String args[]){
[Link]("Shubham Patel 05121402020\n");
Scanner s=new Scanner([Link]);
int num1,num2,num3;
[Link]("Enter 1st number: ");
num1=[Link]();
[Link]("Enter the 2nd number: ");
num2=[Link]();
[Link]("Enter the 3rd Number: ");
num3=[Link]();
if(num1<num2 && num1<num3){
[Link](num1+" "+"is the smallest");}
else if(num2<num1 && num2<num3){
[Link](num2+" "+"is the smallest");}
else{
[Link](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 [Link];
public class average{
public static void main(String args[]){
[Link]("Khush Bhatt 02321402020\n");
int num1,num2,num3;
double avg=0.0;
Scanner s=new Scanner([Link]);
[Link]("Enter 1st number: \n");
num1=[Link]();
[Link]("Enter the 2nd number: \n");
num2=[Link]();
[Link]("Enter the 3rd Number: \n");
num3=[Link]();
avg=(num1+num2+num3)/3;
[Link]("The average of 3 numbers is: "+avg);
}
}
D. WAP to calculate Factorial of a number
import [Link];
public class factorial{
public static void main(String args[]){
Scanner s=new Scanner([Link]);
[Link]("Khush Bhatt 02321402020\n");
int num,i,fact=1;
[Link]("Enter the number: \n");
num=[Link]();
for(i=1;i<=num;i++){
fact=fact*i;}
[Link]("Factorial of the number is: "+fact);
}
}
E. WAP to check whether a given number is Armstrong or not
import [Link];
public class armstrong
{
public static void main(String[] args)
{
[Link]("Khush Bhatt 02321402020\n");
int x=0,a,temp;
int n;
[Link]("Enter number");
Scanner s=new Scanner([Link]);
n=[Link]();
temp=n;
while(n>0)
{
a=n%10;
n=n/10;
x=x+(a*a*a);
}
if(temp==x)
[Link](temp+" is an armstrong number");
else
[Link](temp+" is not an armstrong number");
}
}
F. WAP to check leap year.
import [Link];
public class LeapYear {
public static void main(String[] args){
[Link]("Khush bhatt 05121402020");
int year;
[Link]("Enter an Year :: ");
Scanner sc = new Scanner([Link]);
year = [Link]();
if (((year % 4 == 0) && (year % 100!= 0)) || (year%400 == 0))
[Link]("Specified year is a leap year");
else
[Link]("Specified year is not a leap year");
}
}
G. Write a Java program that checks whether a given string is a palindrome or not.
import [Link];
class pl
{
public static void main(String args[])
{
[Link]("Khush bhatt 02321402020\n");
int l,i;
String str,rev="";
[Link]("Enter the string::");
Scanner s = new Scanner([Link]);
str=[Link]();
l=[Link]();
for(i=l-1;i>=0;i--)
{
rev=rev+[Link](i);
}
if([Link](rev))
{
[Link](str+" is a palindrome");
}
else
[Link](str+" is not a palindrome");
}
}
PRACTICAL-3
WAP to display integer and string values of an array using for each loop.
import [Link];
public class loop
{
public static void main(String args[])
{
[Link]("Khush Bhatt 02321402020");
int arr[]={2,3,4,5,6,7};
String arr1[]={"a","e","i","o","u"};
for(int i:arr)
{
[Link](i);
}
for(String j:arr1){
[Link](j);}
}
}
PRACTICAL-4
WAP to read the array dynamically, sort the array and display the sorted array.
import [Link];
import [Link];
public class arraydn
{
public static void main(String args[])
{
int size,i;
[Link]("enter the size of an array:");
Scanner s= new Scanner([Link]);
size=[Link]();
int[] a=new int[size];
[Link]("Enter elements in the array :");
for(i=0;i<size;i++)
{
a[i]=[Link]();
}
[Link]([Link](a));
}}
PRACTICAL-5
Write a Java Program to demonstrate use of nested class.
class outer1
{
void outmethod()
{
class inner
{
void inmethod()
{
[Link]("Inner class");
}
}
inner i=new inner();
[Link]();
}
public static void main(String args[])
{
outer c=new outer();
[Link]();
}
}
PRACTICAL-6
Java program to demonstrate example of static variable and static method.
import [Link];
class static1
{
static int a;
static int fact(int a)
{
int b=1;
for(int i=a;i>1;i--)
{
b=b*i;
}
[Link]("Factorial of "+a+" is : "+b);
return b;
}
public static void main(String args[])
{
Scanner sc=new Scanner([Link]);
[Link]("Enter number to find factorial");
a=[Link](); 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[]){
[Link]("Shubham Patel 05121402020");
programmer p=new programmer();
[Link]("Salary: "+[Link]);
[Link]("Bonus: "+[Link]);
}
}
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[]){
[Link]("Shubham Patel 05121402020\n");
rat r=new rat();
[Link]([Link]);
[Link](r.name1);
[Link](r.name2);
}
}
PRACTICAL-9
WAP to create a package in java
package pack2;
public class C{
public void msg(){
[Link]("Enter a number: ");
}
}
package pack3;
import pack2.*;
import [Link];
class D{
public static void main(String args[]){
C obj=new C();
[Link]();
int num;
Scanner s=new Scanner([Link]);
num=[Link]();
[Link]("The number entered by you is: "+num);
}
}
PRACTICAL-10
WAP to illustrate the concept of Method Overriding and constructor overriding
class Vehicle{
void run(){[Link]("Vehicle is running");}
}
public class Bike2 extends Vehicle{
void run()
{[Link]("Bike is running safely");}
public static void main(String args[]){
[Link]("Shubham Patel 05121402020\n");
Bike2 obj = new Bike2();//creating object
[Link]();//calling method
}
}
public class Student {
int id;
String name;
Student(){
[Link]("this a default constructor");
}
Student(int i, String n){
id = i;
name = n;
}
public static void main(String[] args) {
[Link]("Shubham Patel 05121402020\n");
Student s = new Student();
[Link]("\nDefault Constructor values: \n");
[Link]("Student Id : "+[Link] + "\nStudent Name : "+[Link]);
[Link]("\nParameterized Constructor values: \n");
Student student = new Student(10, "David");
[Link]("Student Id : "+[Link] + "\nStudent Name :
"+[Link]);
}
}
PRACTICAL-11
Write an program using try, catch, throw and finally
import [Link].*;
import [Link].*;
public class program1
{
static void validate(int age)
{
if(age<18)
throw new ArithmeticException("NOT VALID");
else
[Link]("YOU CAN VOTE");
}
public static void main(String args[])
{
Scanner sc=new Scanner([Link]);
[Link]("Enter age ");
try
{
int a=[Link]();
validate(a);
}
catch(Exception e)
{
[Link]("PLEASE USE NUMERIC");
}
finally
{
[Link]("This is finally block");
}
}
}
PRACTICAL-12
WAP to illustrate the concept of abstract class and abstract method
abstract class base
{
public base()
{
[Link]("Base constructor");
}
public void sayHello()
{
[Link]("Hello");
}
abstract public void greet();
}
class child extends base
{
public void greet()
{
[Link]("Good Morning");
}
}
public class Abstract
{
public static void main(String args[])
{
[Link]("\nName:- khush bhatt Enroll. no.:- 02321402020 \
n");
//base obj=new base();
//this line give error because we cannot create object of Abstract class
child obj=new child();
[Link]();
}
}
PRACTICAL-13
WAP to demonstrate the use of implementing interfaces
import [Link];
interface area
{
public void dimensions();
public void area();
}
public class Inter implements area
{
int length,breadth,area;
public void dimensions()
{
Scanner s=new Scanner([Link]);
[Link]("Enter length: ");
length=[Link]();
[Link]("Enter breadth: ");
breadth=[Link]();
}
public void area()
{
area=length*breadth;
[Link]("\nArea :"+area);
}
public static void main(String[] args)
{
[Link]("\nName:- khush bhatt Enroll. no.:-
02321402020 \n");
Inter obj=new Inter();
[Link]();
[Link]();
}
}
PRACTICAL-14
WAP to implement the concept of exception handling using predefined and user
define exceptions.
A. PREDINED EXCEPTION
import [Link];
class MyException extends Exception
{
public String tostring()
{
return "I am tostring()";
}
}
public class test
{
public static void main(String args[])
{
try
{
Scanner emp=new Scanner([Link]);
String name,dep;
int salary;
[Link]("\nEnter your Name:- ");
name=[Link]();
[Link]("\nEnter your Department Name:-");
dep=[Link]();
[Link]("\nEnter your Salary:-");
salary=[Link]();
[Link]("Your Name is: "+name);
[Link]("Your Department Name is: "+dep);
[Link]("Your Salary is: "+salary);
}
catch(Exception obj)
{
[Link]("Exception Occured:- " + obj);
}
}
}
B . CUSTOM EXCEPTION
// class representing custom exception
class InvalidAgeException extends Exception
{
public InvalidAgeException (String str)
{
// calling the constructor of parent Exception
super(str);
}
}
// class that uses custom exception InvalidAgeException
public class Custom
{
// method to check the age
static void validate (int age) throws InvalidAgeException{
if(age < 18){
// throw an object of user defined exception
throw new InvalidAgeException("age is not valid to vote");
}
else {
[Link]("welcome to vote");
}
}
// main method
public static void main(String args[])
{
try
{
// calling the method
validate(13);
}
catch (InvalidAgeException ex)
{
[Link]("Caught the exception");
// printing the message from InvalidAgeException object
[Link]("Exception occured: " + ex);
}
[Link]("rest of the code...");
}
}
}
}
PRACTICAL-15
WAP to demonstrate thread priority.
import [Link].*;
public class Test extends Thread
{
// Method 1
// Whenever the start() method is called by a thread
// the run() method is invoked
public void run()
{
// the print statement
[Link]("Inside the run() method");
}
// the main method
public static void main(String argvs[])
{
[Link]("\nName:- khush bhatt Enroll. no.:- 02321402020 \
n");
// Creating threads with the help of ThreadPriority class
ThreadPriority th1 = new ThreadPriority();
ThreadPriority th2 = new ThreadPriority();
ThreadPriority th3 = new ThreadPriority();
// We did not mention the priority of the thread.
// Therefore, the priorities of the thread is 5, the default value
// 1st Thread
// Displaying the priority of the thread
// using the getPriority() method
[Link]("Priority of the thread th1 is : " + [Link]());
// 2nd Thread
// Display the priority of the thread
[Link]("Priority of the thread th2 is : " + [Link]());
// 3rd Thread
// // Display the priority of the thread
[Link]("Priority of the thread th2 is : " + [Link]());
// Setting priorities of above threads by
// passing integer arguments
[Link](6);
[Link](3);
[Link](9);
// 6
[Link]("Priority of the thread th1 is : " + [Link]());
// 3
[Link]("Priority of the thread th2 is : " + [Link]());
// 9
[Link]("Priority of the thread th3 is : " + [Link]());
// Main thread
// Displaying name of the currently executing thread
[Link]("Currently Executing The Thread : " +
[Link]().getName());
[Link]("Priority of the main thread is : " +
[Link]().getPriority());
// Priority of the main thread is 10 now
[Link]().setPriority(10);
[Link]("Priority of the main thread is : " +
[Link]().getPriority());
}
}
PRACTICAL-16
WAP to Check the Thread Status using multithreading.
import [Link];
class MyThread implements Runnable
{
public void run()
{
try
{
[Link](1500);
}
catch (Exception e)
{
[Link](e);
}
}
}
public class Status
{
public static void main(String args[]) throws Exception
{
[Link]("\nName:- khush bhatt Enroll. no.:-
02321402020 \n");
for (int tn = 0; tn < 5; tn++)
{
Thread t = new Thread(new MyThread());
[Link]("MyThread:" + tn);
[Link]();
}
Set<Thread> threadSet = [Link]().keySet();
for (Thread t : threadSet)
{
[Link]("Thread :" + t + ":" + "Thread status : " + [Link]());
}
}
}
PRACTICAL-17
WAP to explain multithreading concept.
A. MULTITHREADING BY EXTEND THREAD
class MultithreadingDemo extends Thread {
public void run()
{
try {
// Displaying the thread that is running
[Link](
"Thread " + [Link]().getId()
+ " is running");
}
catch (Exception e) {
// Throwing an exception
[Link]("Exception is caught");
}
}
}
// Main Class
public class Multithread1 {
public static void main(String[] args)
{
[Link]("\nName:- khush bhatt Enroll. no.:-
023321402020 \n");
int n = 8; // Number of threads
for (int i = 0; i < n; i++) {
MultithreadingDemo object
= new MultithreadingDemo();
[Link]();
}
}
}
B. MULTITHREADING BY IMPLEMENTS RUNNABLE THREAD
class MultithreadingDemo implements Runnable {
public void run()
{
try {
// Displaying the thread that is running
[Link](
"Thread " + [Link]().getId()
+ " is running");
}
catch (Exception e) {
// Throwing an exception
[Link]("Exception is caught");
}
}
}
// Main Class
class Multithread2 {
public static void main(String[] args)
{
[Link]("\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());
[Link]();
} }
PRACTICAL-18
WAP to explain all the string operations
import [Link];
public class string
{
public static void main(String[] args)
{
Scanner obj=new Scanner([Link]);
String string1,string2,string3;
[Link]("\nName:- khush bhatt Enroll. no.:-
02321402020 \n");
// get the length of string
[Link]("***Length Operation***\n");
[Link]("Enter the value in First String: ");
string1=[Link]();
int length = [Link]();
[Link]("Length: " + length);
[Link]("\n***Concatination Operation***\n");
[Link]("Enter the value in Second String to concatenate: ");
string2=[Link]();
// join two strings
String joinedString = [Link](string2);
[Link]("Joined String: " + joinedString);
[Link]("\n***Comparision Operation***\n");
[Link]("Enter the value in Third String for Comparision: ");
string3=[Link]();
// compare first and second strings
boolean result1 = [Link](string2);
[Link]("Strings first and second are equal: " + result1);
// compare first and third strings
boolean result2 = [Link](string3);
[Link]("Strings first and third are equal: " + result2);
}
}
PRACTICAL-19
WAP to explain all the string operations using string buffer method.
import [Link];
public class stringBuffer
{
public static void main(String[] args)
{
[Link]("\nName:- khush bhatt Enroll. no.:- 02321402020 \
n");
[Link]("***append() method***\n");
//The append() method concatenates the given argument with this String.
StringBuffer sb=new StringBuffer("Hello ");
[Link]("Java");//now original string is changed
[Link](sb);//prints Hello Java
[Link]("\n*** insert() method ***\n");
//The insert() method inserts the given String with this string at the given
position.
StringBuffer x=new StringBuffer("Hello ");
[Link](1,"Java");//now original string is changed
[Link](x);//prints HJavaello
[Link]("\n***replace() method ***\n");
//The replace() method replaces the given String from the specified beginIndex
and endIndex.
StringBuffer y=new StringBuffer("Hello");
[Link](1,3,"Java");
[Link](y);//prints HJavalo
[Link]("\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");
[Link](1,3);
[Link](z);//prints Hlo
[Link]("\n***reverse() method ***\n");
//The reverse() method of the StringBuilder class reverses the current String.
StringBuffer b=new StringBuffer("Hello");
[Link]();
[Link](b);//prints olleH
}
}
PRACTICAL-20
WAP to read and write in a file
A. TO CREATE A FILE
import [Link]; // Import the File class
import [Link]; // Import the IOException class to handle errors
public class CreateFile {
public static void main(String[] args) {
try {
File myObj = new File("[Link]");
if ([Link]()) {
[Link]("File created: " + [Link]());
} else {
[Link]("File already exists.");
}
} catch (IOException e) {
[Link]("An error occurred.");
[Link]();
}
}
}
B. TO WRITE IN FILE
import [Link]; // Import the FileWriter class
import [Link]; // Import the IOException class to handle errors
public class WriteToFile {
public static void main(String[] args) {
try {
FileWriter myWriter = new FileWriter("[Link]");
[Link]("Files in Java might be tricky, but it is fun enough!");
[Link]();
[Link]("Successfully wrote to the file.");
} catch (IOException e) {
[Link]("An error occurred.");
[Link]();
}
}
}
C. TO READ THE FILE
import [Link]; // Import the File class
import [Link]; // Import this class to handle errors
import [Link]; // Import the Scanner class to read text files
public class ReadFile {
public static void main(String[] args) {
try {
File myObj = new File("[Link]");
Scanner myReader = new Scanner(myObj);
while ([Link]()) {
String data = [Link]();
[Link](data);
}
[Link]();
} catch (FileNotFoundException e) {
[Link]("An error occurred.");
[Link]();
}
}
}
PRACTICAL-21
WAP to display user registration form using applet.
import [Link].*;
public class Tes extends [Link]
{
public void init()
{
setLayout(new FlowLayout([Link]));
add(new Label("Name :"));
add(new TextField(10));
add(new Label("Address :"));
add(new TextField(10));
add(new Label("Birthday :"));
add(new TextField(10));
add(new Label("Gender :"));
Choice gender = new Choice();
[Link]("Man");
[Link]("Woman");
Component add = add(gender);
add(new Label("Job :"));
CheckboxGroup job = new CheckboxGroup();
add(new Checkbox("Student", job, false));
add(new Checkbox("Teacher", job, false));
add(new Button("Register"));
add(new Button("Exit"));
}}
HTML Code: -
<html>
<head><title>Register</title></head>
<body>
<applet code="[Link]" width=230 height=300></applet>
</body>
</html>
PRACTICAL-22
WAP to explain important fields of AWT.
public class trycatch
import [Link].*;
import [Link].*;
import [Link].*;
public class cont extends Applet implements ActionListener
{
String name=" ", gender=" ";
int age;
TextField n=new TextField(10);
CheckboxGroup g=new CheckboxGroup();
Checkbox m=new Checkbox("male",g,true);
Checkbox f=new Checkbox("female",g,false);
Choice c=new Choice();
Label l1=new Label("Enter name:");
Label l2=new Label("Selec Gender:");
Label l3=new Label("age:");
Button b=new Button("Button");
public void init()
{
add(l1);
add(n);
add(l2);
add(m);
add(f);
add(l3);
[Link]("10");
[Link]("15");
[Link]("20");
[Link]("25");
[Link]("30");
[Link]("35");
add(c);
add(b);
[Link](this);
}
public void paint(Graphics g)
{
[Link]("name: "+name,20,100);
[Link]("Gender: "+gender,20,120);
[Link]("age: "+age,20,140);
}
public void actionPerformed(ActionEvent e)
{
name=[Link]();
gender=[Link]().getLabel();
age=[Link]([Link]());
repaint();
}
}
HTML Code: -
<html>
<body>
<applet code="[Link]" height=1000 width=1000></applet>
</body>
</html>
PRACTICAL-23
WAP to explain important fields of event handling.
import [Link].*;
import [Link].*;
import [Link].*;
public class MouseExample extends MouseAdapter
{
Frame f;
MouseExample()
{
f=new Frame("Mouse Adapter");
[Link](this);
[Link](300,300);
[Link](null);
[Link](true);
}
public void mouseClicked(MouseEvent e)
{
Graphics g=[Link]();
[Link]([Link]);
[Link]([Link](),[Link](),30,30);
}
public static void main(String args[])
{
[Link]("\nName:- Sameer Enroll. no.:- 04321402020 \n");
new MouseExample();
}
}
PRACTICAL-24
Write a GUI Program to Add Two numbers Using AWT and event
handling
import [Link].*;
import [Link].*;
import [Link].*;
public class sum extends Applet implements ActionListener
{
TextField t1=new TextField(10);
TextField t2=new TextField(10);
TextField t3=new TextField(10);
Label l1=new Label("Enter First Number: ");
Label l2=new Label("Enter Second Number: ");
Label l3=new Label("Sum: ");
Button B=new Button("ADD");
public void init(){
add(l1);
add(t1);
add(l2);
add(t2);
add(l3);
add(t3);
add(B);
[Link](this);
}
public void actionPerformed(ActionEvent e)
{
if([Link]()==B)
{
int num1= [Link]([Link]());
int num2= [Link]([Link]());
[Link]("" +(num1+num2));
}
}
PRACTICAL-25
WAP to show database connection is [Link].
import [Link].*;
public class est
{
public static void main(String[] args)
{
Connection con = null;
try
{
[Link]("[Link]");
[Link]("Driver loaded");
// query="select * from student ";
con =[Link]("jdbc:mysql://localhost:3306/student"
, "root", "Sameer@000");
[Link]("Connection established");
}
catch (ClassNotFoundException | SQLException e)
{
[Link]("Exception caught " + [Link]());
}
}
}
PRACTICAL-26
WAP to make a login GUI using TextField, Password Field and Login Button using
Swings.
import [Link].*;
import [Link].*;
import [Link].*;
import [Link];
class CreateLoginForm extends JFrame implements ActionListener
{
JButton b1;
JPanel newPanel;
JLabel userLabel, passLabel;
final JTextField textField1,textField2;
CreateLoginForm()
{
userLabel = new JLabel();
[Link]("Username");
textField1 = new JTextField(15);
passLabel = new JLabel();
[Link]("Password");
textField2 = new JPasswordField(15);
b1 = new JButton("SUBMIT");
newPanel = new JPanel(new GridLayout(3, 1));
[Link](userLabel);
[Link](textField1);
[Link](passLabel);
[Link](textField2);
[Link](b1);
add(newPanel, [Link]);
[Link](this);
setTitle("LOGIN FORM");
}
public void actionPerformed(ActionEvent ae)
{
String userValue = [Link]();
String passValue = [Link]();
if ([Link]("sameer") && [Link]("NHLPS"))
{
[Link]("Login Succesfully");
}
else
{
[Link]("Please enter valid username and password");
}
}
}
public class LoginFormDemo
{
public static void main(String arg[])
{
try
{
CreateLoginForm form = new CreateLoginForm();
[Link](300, 100);
[Link](true);
}
catch (Exception e)
{
[Link](null, [Link]());
}
}
}