1151cs302 - Java Prograaming Lab Manual
1151cs302 - Java Prograaming Lab Manual
LAB MANUAL
3. Inheritance
4. Package creation.
9. JDBC-To connect Oracle/MS Access for Table creation and Data Manipulation.
G. Learning Resources
i. Text Books
1. H.M.Deitel and P.J.Deitel –“Java How to Program” Pearson Prentice Hall 6th Edition, 2011.
1. docs.oracle.com/javaee/6/tutorial/doc/girgm.html
2. www.webreference.com/programming/java.html
3. www.apl.jhu.edu/~hall/java/Documentation.html
INDEX
2 Control Statement
3(a) Constructors
4(a) Inheritance
5 Package
6 Interface
7(a) Threading
7(b) Multithreading
9 DeadLock Detection
12 JDBC
13 Digital Clock
Exp_No: 1(a) Employee Details
Aim:
To write a simple java application program for student details for understanding reference to an
instance of a class (object), methods.
Algorithm
Step1: Start the program.
Step2: Declare all data members in Student class.
Step3: Define all member functions in Student class.
Step4: Create object for Student class.
Step5: call all the member functions and data members using object.
Step6: Display the result.
Step7: Stop the program.
Program
/*PROGARM FOR CREATE A EMPLOYEE DETAILS*/
import java.lang.*;
import java.io.*;
class Employee
{
int empno;
String name;
Employee()
{
try
{
DataInputStream d=new DataInputStream(System.in);
System.out.println("name of the employee");
name=d.readLine();
System.out.println("Employy no:");
empno=Integer.parseInt(d.readLine());
}
catch(IOException e)
{
System.out.println("ioerror");
}
}
void Display()
{
}
}
class manager extends Employee
{
String desig;
int bp;
int hra;
int ta=100,da,inc;
manager()
{
try
{
DataInputStream d=new DataInputStream(System.in);
System.out.println("salary is");
bp=Integer.parseInt(d.readLine());
System.out.println("desig of the employee");
desig=d.readLine();
hra=(bp*10)/100;
da=(bp*5)/100;
inc=(bp*15)/100;
}
catch(IOException e1)
{
System.out.println("error");
}
}
void Display()
{
}
}
class Typist extends Employee
{
int bp;
String desig;
int hra;
int ta=0,da,inc;
Typist()
{
try
{
DataInputStream d=new DataInputStream(System.in);
System.out.println("salary is");
bp=Integer.parseInt(d.readLine());
System.out.println("desig of the employee");
desig=d.readLine();
hra=(bp*8)/100;
da=(bp*4)/100;
inc=(bp*10)/100;
}
catch(IOException e1)
{
System.out.println("error");
}
}
void Display()
{
System.out.println(name+" "+empno+" "+desig+" "+" "+(bp+hra+da+ta+inc));
}
}
}
}
Output:
EMPLOYEE DETAILS
~~~~~~~~~~~~~~~~
Name Code Designation Salary
~~~~ ~~~~ ~~~~~~~~~~ ~~~~~~
BalaChandar 1212 Manager 54000
Kumar 1312 Typist 15000
Conclusion
Thus the java program was successfully complied and executed and result also verified.
Exp_No: 1(b) Handling String Function
Aim:
To write a simple java application program for student details for handling String functions.
Algorithm
Step1: Start the program.
Step2. Import the lang package.
Step3. Get input string.
Step4. And do the String manipulation operation such as modification, search and etc., using
appropriate methods.
Step5. Display the results.
Step6. Stop the program.
Program
class StringExample
{
public static void main (String[] args)
{
String str1 = "Seize the day";
String str2 = new String();
String str3 = new String(str1);
String str4 = "Day of the seize";
String str5 = "Seize the day";
System.out.println("str1: " + str1);
System.out.println("str2: " + str2);
System.out.println("str3: " + str3);
System.out.println("str4: " + str4);
System.out.println("str5: " + str5);
System.out.println();
System.out.println("length of str1 is " + str1.length());
System.out.println("length of str2 is " + str2.length());
System.out.println();
System.out.println("Index of 'e' in str4: "
+ str4.indexOf('e'));
System.out.println("Char at pos 3 in str1: "
+ str1.charAt(3));
System.out.println("Substring 6 to 8 of str1: "
+ str1.substring(6,8));
if (str1==str5)
System.out.println("str1 and str5 refer to the “
+ “same object");
if (str1 != str3)
System.out.println("str1 and str3 don't refer to “
+ “the same object");
if (str1.equals(str3))
System.out.println("str1 and str3 contain the “
+ ”same chars");
System.out.println();
str2 = str1.toUpperCase();
System.out.println("str2 now is: " + str2);
str5 = str1.replace('e','X');
System.out.println("str5 is now: " + str5);
length of str1 is 13
length of str2 is 0
Index of 'e' in str4: 9
Char at pos 3 in str1: z
Substring 6 to 8 of str1: th
str1 and str5 refer to the same object
str1 and str3 don't refer to the same object
str1 and str3 contain the same chars
Conclusion
Thus the java program was successfully complied and executed and result also verified.
Exp_No: 2 Control Statement
Aim:
To write simple java application program for generating the folling
a) Generate Prime Numbers
b) Generate Fibonacci series
c) Decimal To Binary Conversion
Using various control statements.
Algorithm
Step1: Start the program.
Step2: Declare all data members in ControlStatement class.
Step3: Define all member functions in ControlStatement class.
Step4: Create object for Student class.
Step5: Get the input for N.
Step5. Choose the option
Step6: call the member functions using object.
Step7: Display the result.
Step8. Until the option is exit, do Step5,Step6 and Step7
Step9: Stop the program.
Program
import java.io.*;
class ABC
{
int n1;
ABC(int n)
{
n1=n;
}
void prime()
{
int i,j,r,c=0;
System.out.print(n1+" The prime numbers are :");
for(i=1;i<=n1;i++)
{
for(j=2;j<i;j++)
{
r=i%j;
if(r==0)
{
c=c+1;
break;
}
}
if(c==0)
System.out.print(" "+i);
c=0;
}
}
void fibo()
{
int a=0,b=1,c=0,i;
System.out.print("The Fibonacci series :"+a+" "+b);
for(i=1;i<=n1;i++)
{
c=a+b;
System.out.print(" "+c);
a=b;
b=c;
}
}
void decimalToBinary ()
{
int j,i=0,n2;
int a1[]=new int[20];
n2=n1;
System.out.print(n2+" The Binary Number is :");
while(n2>0)
{
a1[i]=n2%2;
i++;
n2=n2/2;
//System.out.print(a1[i]);
}
//System.out.println("i :"+i);
for(j=i-1;j>=0;j--)
System.out.print(a1[j]);
}
}
class ControlStatement
{
public static void main(String arg[])throws Exception
{
int n,i;
DataInputStream dis=new DataInputStream(System.in);
System.out.print("Enter the value of n");
n=Integer.parseInt(dis.readLine());
ABC obj=new ABC(n);
do
{
System.out.println("");
System.out.println(" 1. List Prime ");
System.out.println(" 2. Fibonacci Series");
System.out.println(" 3. Binary Conversion");
System.out.println(" 4. Exit");
System.out.print("Enter your choice :");
i=Integer.parseInt(dis.readLine());
System.out.println("");
switch(i)
{
case 1: obj.prime();
break;
case 2: obj.fibo();
break;
case 3: obj. decimalToBinary ();
break;
default: break;
}
}while(i<4);
}
}
Output
D:\raj>java ControlStatement
Enter the value of n13
1. List Prime
2. Fibonacci Series
3. Binary Conversion
4. Exit
Enter your choice :1
Conclusion
Thus the java program was successfully complied and executed and result also verified.
Exp_No: 3 Constructor
Aim:
To write a simple java application program for the following concept
a) Constructor over Loading
b) Using super and this keywords
Procedure
Step1: Open the notepad.
Step2. Then type the program in the notepad and save in our own folder <mainclassname>.java
Step3. Set the path: set path=” C:\Program Files\Java\jdk1.6.0_20\bin”;
Step4. Set the classpath: set classpath=”C:\Program Files\Java\jdk1.6.0_20\lib”;
Step4. Compile the source progam using javac <mainclassname.java>
Step5. Run the program using java <mainclassname>
Program-(a)
class Square
{
int height;
int width;
Square()
{
height = 0;
width = 0;
}
Square(int side)
{
height = width = side;
}
class ImplSquare
{
public static void main(String args[])
{
Square sObj1 = new Square();
Square sObj2 = new Square(5);
Square sObj3 = new Square(2,3);
Output (a)
Program-(b)
class constructor1
{
int a=10;
constructor1(int a)
{
System.out.println("Super class constructor a:"+a);
a=this.a;
System.out.println("Super class constructor a:"+a);
}
void display()
{
System.out.println("a="+a);
}
}
class constructor2 extends constructor1
{
int t;
constructor2(int c)
{
super(c);
t=c;
System.out.println("Sub class constructor :"+c);
}
void display()
{
super.display();
System.out.println("a= "+a+" t= "+t);
}
}
class SuperAndThis
{
public static void main(String ag[])
{
constructor2 o=new constructor2(30);
o.display();
}
}
Output-(b)
D:\raj>javac SuperAndThis.java
D:\raj>java SuperAndThis
Super class constructor a:30
Super class constructor a:10
Sub class constructor :30
a=10
a= 10 t= 30
Conclusion
Thus the java program was successfully complied and executed and result also verified.
Exp_No: 4 Inheritance
Aim:
To write a simple java application program for the following concept.
a) Multilevel Inheritance
b) Inheritance with overloading and over riding methods
Procedure
Step1: Open the notepad.
Step2. Then type the program in the notepad and save in our own folder <mainclassname>.java
Step3. Set the path: set path=” C:\Program Files\Java\jdk1.6.0_20\bin”;
Step4. Set the classpath: set classpath=”C:\Program Files\Java\jdk1.6.0_20\lib”;
Step4. Compile the source progam using javac <mainclassname.java>
Step5. Run the program using java <mainclassname>
Program-(a)
class students
{
private int sno;
private String sname;
public void setstud(int no,String name)
{
sno = no;
sname = name;
}
public void putstud()
{
System.out.println("Student No : " + sno);
System.out.println("Student Name : " + sname);
}
}
class marks extends students
{
protected int mark1,mark2;
public void setmarks(int m1,int m2)
{
mark1 = m1;
mark2 = m2;
}
public void putmarks()
{
System.out.println("Mark1 : " + mark1);
System.out.println("Mark2 : " + mark2);
}
}
class finaltot extends marks
{
private int total;
public void calc()
{
total = mark1 + mark2;
}
public void puttotal()
{
System.out.println("Total : " + total);
}
public static void main(String args[])
{
finaltot f = new finaltot();
f.setstud(100,"Nithya");
f.setmarks(78,89);
f.calc();
f.putstud();
f.putmarks();
f.puttotal();
}
}
Output
D:\raj>java finaltot
Student No : 100
Student Name : Nithya
Mark1 : 78
Mark2 : 89
Total : 167
Program-(b)
class A
{
double area1,x,y;
A(double a, double b)
{
x=a;
y=b;
}
double area(double r)
{
area1=3.14*r*r;
return(area1);
}
double area(double b, double l) //Over loading method
{
area1=b*l;
return(area1);
}
void display()
{
System.out.println(“Area of the Circle is: “+area(x));
System.out.println(“Area of the Rectangle is: “+area(x,y));
}
}
class B extends A
{
double area1,x,y;
B(double a, double b)
{
Super(a,b);
x=a;
y=b;
}
double area(double l, double h)
{
area1= .5*l*h;
return(area1);
}
Void display() //Over riding method
{
System.out.println(“Area of the Triangle is: “+area(x,y));
}
}
class OverLoadOverRide
{
Public static void main(String arg[])throws Exception
{
Double a,b;
DataInputStream dis=new DataInputStream(System.in);
System.out.println(“Enter the value of a:”);
a=Integer.parseInt(dis.readLine());
System.out.println(“Enter the value of b:”);
b=Integer.parseInt(dis.readLine());
B obj=new B(a,b);
Obj.display();
}
}
Output
D:\raj>javac OverLoadOverRide.java
D:\raj>java OverLoadOverRide
Area of the Circle is:
Area of the Rectangle is:
Area of the Triangle is:
Conclusion
Thus the java program was successfully complied and executed and result also verified.
Exp-No: 5 Package
Aim
To write a simple java application program for creating calculator for Arithmetic operation using
package.
Procedure
Step1. Create new folder as a calculator in existing folder
Step2: Open the notepad.
Step3. Type the package program in notepad and save <classname>.java
Step4. Then type the main program in the notepad and save <mainclassname>.java
Step5. Compile the package program : javac foldername/classname.java
Step6. Compile the source progam using javac <mainclassname.java>
Step7. Run the program using java <mainclassname>
Program for creating Package
package calculator;
public class Arithmetic
{
public void add(double a,double b)
{
double c;
c=a+b;
System.out.println("Result: "+a+"+"+b+"="+c);
}
if(b==0)
System.out.println("Factorial of "+b+" ="+c);
else
{
for(i=1;i<=b;i++)
c=c*a;
System.out.println("Factorial of "+b+" ="+c);
}
}
}
Program for implementing the package
import java.io.*;
import calculator.*;
class SamplePackage
{
public static void main(String ar[])throws Exception
{
double a,b;
int c;
Arithmetic obj=new Arithmetic();
DataInputStream d=new DataInputStream(System.in);
do
{
System.out.println(" Arithmetic Operation");
System.out.println(" 1.Addition");
System.out.println(" 2.Subtraction");
System.out.println(" 3.Multiplication");
System.out.println(" 4.Division");
System.out.println(" 5.Power");
System.out.println(" 6.Exit");
System.out.print("Enter your choice:");
c=Integer.parseInt(d.readLine());
switch(c)
{
case 1:
System.out.println("Enter the value of a");
a=Double.parseDouble(d.readLine());
System.out.println("Enter the value of b");
b=Double.parseDouble(d.readLine());
obj.add(a,b);
break;
case 2:
System.out.println("Enter the value of a");
a=Double.parseDouble(d.readLine());
System.out.println("Enter the value of b");
b=Double.parseDouble(d.readLine());
obj.sub(a,b);
break;
case 3:
System.out.println("Enter the value of a");
a=Double.parseDouble(d.readLine());
System.out.println("Enter the value of b");
b=Double.parseDouble(d.readLine());
obj.mul(a,b);
break;
case 4:
System.out.println("Enter the value of a");
a=Double.parseDouble(d.readLine());
System.out.println("Enter the value of b");
b=Double.parseDouble(d.readLine());
obj.div(a,b);
break;
case 5:
System.out.println("Enter the value of a");
a=Double.parseDouble(d.readLine());
System.out.println("Enter the value of b");
b=Double.parseDouble(d.readLine());
obj.pow(a,b);
break;
default:
break;
}
}while(c!=6);
}
}
Output
D:\raj>javac calculator\Arithmetic.java
D:\raj>javac SamplePackage.java
Note: SamplePackage.java uses or overrides a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
D:\raj>java SamplePackage
Arithmetic Operation
1.Addition
2.Subtraction
3.Multiplication
4.Division
5.Power
6.Exit
Enter your choice:1
Enter the value of a
40
Enter the value of b
20
Result: 40.0+20.0=60.0
Arithmetic Operation
1.Addition
2.Subtraction
3.Multiplication
4.Division
5.Power
6.Exit
Enter your choice:2
Enter the value of a
50
Enter the value of b
30
Result: 50.0-30.0=20.0
Arithmetic Operation
1.Addition
2.Subtraction
3.Multiplication
4.Division
5.Power
6.Exit
Enter your choice:3
Enter the value of a
5
Enter the value of b
4
Result: 5.0*4.0=20.0
Arithmetic Operation
1.Addition
2.Subtraction
3.Multiplication
4.Division
5.Power
6.Exit
Enter your choice:4
Enter the value of a
55
Enter the value of b
11
Result: 55.0/11.0=5.0
Arithmetic Operation
1.Addition
2.Subtraction
3.Multiplication
4.Division
5.Power
6.Exit
Enter your choice:5
Enter the value of a
5
Enter the value of b
5
Factorial of 5.0 =3125.0
Arithmetic Operation
1.Addition
2.Subtraction
3.Multiplication
4.Division
5.Power
6.Exit
Enter your choice:6
Conclusion
Thus the java program was successfully complied and executed and result also verified.
Exp-No: 6 Interfaces
Aim:
To write java program for developing user-defined interfaces and implementation.
Procedure
Step1: Open the notepad.
Step2. Then type the program in the notepad and save in our own folder <mainclassname>.java
Step3. Set the path: set path=” C:\Program Files\Java\jdk1.6.0_20\bin”;
Step4. Set the classpath: set classpath=”C:\Program Files\Java\jdk1.6.0_20\lib”;
Step4. Compile the source progam using javac <mainclassname.java>
Step5. Run the program using java <mainclassname>
Program
interface ProductsInterface
{
public String getName();
public int getAvailableCount();
public String getKind();
public double getCost();
}
class Inherit
{
public static void main(String[] args)
{
ProductsInterface[] arr = new ProductsInterface[3];
arr[0] = new Book("My Michael - Amos Oz", 10, 56.50);
arr[1] = new IsraelDisk("Moon - Shlomo Artzi", 5, 87.90);
arr[2] = new AmericanDisk("Frozen - Madonna",
17, 21.23, 4.25);
Conclusion
Thus the java program was successfully complied and executed and result also verified.
Exp-No: 7 Threading
Aim:
To write a java program for developing a chatting application(Client and Server) using Thread.
Procedure
Step1: Open the notepad.
Step2. Then type the program in the notepad and save in our own folder <mainclassname>.java
Step3. Set the path: set path=” C:\Program Files\Java\jdk1.6.0_20\bin”;
Step4. Set the classpath: set classpath=”C:\Program Files\Java\jdk1.6.0_20\lib”;
Step4. Compile the source progam using javac <mainclassname.java>
Step5. Run the program using java <mainclassname>
Program-(a)
import java.io.*;
import java.net.*;
Chat1.java
class Chat1
{
static Socket s;
public static void main(String arg[])throws Exception
{
String in;
ServerSocket ss=new ServerSocket(2500);
s=ss.accept();
new Thread(new SendServerData()).start();
InputStreamReader isr=new InputStreamReader(s.getInputStream());
BufferedReader br=new BufferedReader(isr);
do
{
in=br.readLine();
System.out.println("person2:"+in);
}while(in!="end");
s.close();
ss.close();
}
}
class SendServerData extends Chat1 implements Runnable
{
String out;
public void run()
{
try
{
PrintStream ps=new PrintStream(s.getOutputStream());
//InputStreamReader isr=new InputStreamReader(System.in);
//BufferedReader br=new BufferedReader(isr);
DataInputStream br=new DataInputStream(System.in);
do
{
out=br.readLine();
ps.println(out);
}while(!out.equals("end"));
System.out.println("Stop the conversion");
}catch(IOException e){}
}
}
Chat2.java
import java.io.*;
import java.net.*;
class Chat2
{
static Socket s;
public static void main(String arg[])throws Exception
{
String in;
s=new Socket("localhost",2500);
//InputStreamReader isr=new InputStreamReader(s.getInputStream());
new Thread(new SendClientData()).start();
//BufferedReader br=new BufferedReader(isr);
DataInputStream br=new DataInputStream(s.getInputStream());
do
{
in=br.readLine();
System.out.println("Person1:"+in);
}while(in!="end");
s.close();
}
}
class SendClientData extends Chat2 implements Runnable
{
}catch(IOException e){}
}
}
Output
D:\raj>javac Chat1.java
D:\raj>java Chat1
person2:Hello Anand
Hello Raj
person2:How are you man?
ya fina da...
what abot you da......
person2:fine
bye
person2:bye
D:\raj>javac Chat2.java
D:\raj>java Chat2
Hello Anand
Person1:Hello Raj
How are you man?
Person1:ya fina da...
Person1:what abot you da......
fine
Person1:bye
bye
Program-B
class A extends Thread
{
int i;
public void run()
{
System.out.println(".........Thread1 Started..............");
for(i=1;i<=10;i++)
{
if(i==5)
stop();
else
System.out.println("Thread1 value :"+i);
}
System.out.println("Thread1 Stopped");
}
}
class B extends Thread
{
int j;
public void run()
{
System.out.println(".........Thread2 Started..............");
try{
for(j=1;j<=10;j++)
{
if(j==3)
sleep(500);
else
System.out.println("Thread2 value :"+j);
}
System.out.println("Thread2 Stopped");
}
catch(Exception e)
{
System.out.println(e);
}
}
}
class C extends Thread
{
int k;
public void run()
{
System.out.println(".........Thread3 Started..............");
try{
for(k=1;k<=10;k++)
{
if(k==5)
sleep(500);
else
System.out.println("Thread3 value :"+k);
}
System.out.println("Thread3 Stopped");
}
catch(Exception e)
{
System.out.println(e);
}
}
}
class MultiThread
{
public static void main(String arg[])throws Exception
{
A t1=new A();
B t2=new B();
C t3=new C();
t1.start();
t2.start();
t3.start();
}
}
Output
.........Thread1 Started..............
.........Thread2 Started..............
Thread1 value :1
.........Thread3 Started..............
Thread1 value :2
Thread2 value :1
Thread1 value :3
Thread3 value :1
Thread1 value :4
Thread2 value :2
Thread3 value :2
Thread3 value :3
Thread3 value :4
Thread2 value :4
Thread3 value :6
Thread2 value :5
Thread3 value :7
Thread2 value :6
Thread3 value :8
Thread2 value :7
Thread3 value :9
Thread2 value :8
Thread3 value :10
Thread2 value :9
Thread3 Stopped
Thread2 value :10
Thread2 Stopped
Press any key to continue...
Conclusion
Thus the java program was successfully complied and executed and result also verified.
Exp-No: 8 Exception Handling
Aim
To write java program for Handling pre-defined exceptions and User-defined Exception.
Procedure
Step1: Open the notepad.
Step2. Then type the program in the notepad and save in our own folder <mainclassname>.java
Step3. Set the path: set path=” C:\Program Files\Java\jdk1.6.0_20\bin”;
Step4. Set the classpath: set classpath=”C:\Program Files\Java\jdk1.6.0_20\lib”;
Step4. Compile the source progam using javac <mainclassname.java>
Step5. Run the program using java <mainclassname>
Program-(a)
import java.io.*;
class PrefinedException
{
public static void main(String arg[])
{
try{
int a1[]=new int[5];
int a,b,i,N;
double c;
DataInputStream d=new DataInputStream(System.in);
System.out.println("Enter the value of N");
N=Integer.parseInt(d.readLine());
System.out.println("Enter the array elements");
for(i=0;i<N;i++)
a1[i]=Integer.parseInt(d.readLine());
a=a1[0];
b=a1[1];
c=a/b;
System.out.println("c="+c);
}
catch(ArrayIndexOutOfBoundsException e)
{
System.out.println("Error is "+e);
}
catch(ArithmeticException e)
{
System.out.println("Error is "+e);
}
catch(NumberFormatException e)
{
System.out.println("Error is "+e);
}
catch(IOException e)
{
System.out.println("Error is "+e);
}
}
}
Output
D:\raj>javac PrefinedException.java
Note: PrefinedException.java uses or overrides a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
D:\raj>java PrefinedException
Enter the value of N
3
Enter the array elements
10
11
12
Sum of N elements=33
c=0.9090909090909091
D:\raj>
D:\raj>java PrefinedException
Enter the value of N
6
Enter the array elements
1
2
3
4
5
6
Error is java.lang.ArrayIndexOutOfBoundsException: 5
D:\raj>java PrefinedException
Enter the value of N
3
Enter the array elements
11
0
13
Sum of N elements=24
Error is java.lang.ArithmeticException: Divide by zero
Program(b)
NegativeAgeException.java
public class NegativeAgeException extends Exception
{
private int age;
public NegativeAgeException(int age)
{
this.age = age;
}
CustomExceptionTest.java
public class CustomExceptionTest
{
D:\raj>java CustomExceptionTest
Age entered is 4
D:\raj>javac CustomExceptionTest.java
D:\raj>java CustomExceptionTest
Exception in thread "main" Age cannot be negative -4
at CustomExceptionTest.main(CustomExceptionTest.java:9)
Conclusion
Thus the java program was successfully complied and executed and result also verified.
Ex:11 AWT Components
Procedure
Step1: Open the notepad.
Step2. Then type the program in the notepad and save in our own folder <mainclassname>.java
Step3. Set the path: set path=” C:\Program Files\Java\jdk1.6.0_20\bin”;
Step4. Set the classpath: set classpath=”C:\Program Files\Java\jdk1.6.0_20\lib”;
Step4. Compile the source progam using javac <mainclassname.java>
Step5. Run the program using applet viewer or Internet Explorer <mainclassname>
Program
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
result = 0;
lastCommand = "=";
start = true;
addButton("7", insert);
addButton("8", insert);
addButton("9", insert);
addButton("/", command);
addButton("4", insert);
addButton("5", insert);
addButton("6", insert);
addButton("*", command);
addButton("1", insert);
addButton("2", insert);
addButton("3", insert);
addButton("-", command);
addButton("0", insert);
addButton(".", insert);
addButton("=", command);
addButton("+", command);
add(panel, BorderLayout.CENTER);
}
/*
Adds a button to the center panel.
@param label the button label
@param listener the button listener
*/
/*
This action inserts the button action string to the
end of the display text.
*/
private class InsertAction implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
String input = event.getActionCommand();
if (start)
{
display.setText("");
start = false;
}
display.setText(display.getText() + input);
}
}
/*
This action executes the command that the button
action string denotes.
*/
private class CommandAction implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
String command = event.getActionCommand();
if (start)
{
if (command.equals("-"))
{
display.setText(command);
start = false;
}
else
lastCommand = command;
}
else
{
calculate(Double.parseDouble(display.getText()));
lastCommand = command;
start = true;
}
}
}
/*
Carries out the pending calculation.
@param x the value to be accumulated with the prior result.
*/
public void calculate(double x)
{
if (lastCommand.equals("+")) result += x;
else if (lastCommand.equals("-")) result -= x;
else if (lastCommand.equals("*")) result *= x;
else if (lastCommand.equals("/")) result /= x;
else if (lastCommand.equals("=")) result = x;
display.setText("" + result);
}
Conclusion
Thus the java program was successfully complied and executed and result also verified.
Exno: 11 JDBC
Aim :
Write a simple for connect Oracle for Data Manipulation.
Procedure
Step1: Open the notepad.
Step2. Then type the program in the notepad and save in our own folder <mainclassname>.java
Step3. Set the path: set path=” C:\Program Files\Java\jdk1.6.0_20\bin”;
Step4. Set the classpath: set classpath=”C:\Program Files\Java\jdk1.6.0_20\lib”;
Step4. Compile the source progam using javac <mainclassname.java>
Step5. Run the program using applet viewer or Internet Explorer <mainclassname>
Program
import java.sql.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class addcus extends JFrame implements ActionListener
{
JTextField id;
JTextField name;
JTextField addr;
JTextField pno;
JTextField job;
JButton next;
JButton addnew;
JPanel p;
static Result res;
static Connection conn;
static Statement stat;
public addcus()
{
Super("our application");
Container c=getContentPane();
c.setLayout(new GridLayout(8,1));
id=new JTextField(20);
name=new JTextField(20);
addr=new JTextField(25);
pno=new JTextField(10);
job=new JTextField(15);
next=new JButton("Next");
p=new JPanel();
c.add(new JLabel("customer id",JLabel.CENTER));
c.add(id);
c.add(new JLabel("Customer name",JLabel.CENTER));
c.add(name);
c.add(new JLabel("Customer address",JLabel.CENTER));
c.add(addr);
c.add(new JLabel(" phone num",JLabel.CENTER));
c.add(pno);
c.add(new JLabel("Job",JLabel.CENTER));
c.add(job);
c.add(p);
p.add(next);
next.addActionListener(this);
setSize(500,500);
setVisible(true);
addWindowListener(new WIN());
}
public static void mian(string args[])
{
addcus c=new addcus();
try
{
Class.forName("sun.jdbc.odbc.jdbcodbcDriver");
conn=DriverManager.getConnection("jdbc:odbc:data1");
stat=conn.CreateStatement();
res=stat.executeQuery("select *from customer");
res.next();
}
catch(Exception e)
{
System.out.println("Error"+e);
}
c.showRecord(res);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==next)
{
try
{
res.next();
}
catch(Exception ee)
{}
showRecord(res0;
}
}
public void showRecord(ResultSet res)
{
try
{
id.setText(res.getString(1));
name.setText(res.getString(2));
addr.setText(res.getString(3));
pno.setText(res.getString(4));
job.setText(res.getString(5));
}
catch(Exception e)
{}
}
class WIN extends WindowAdapter
{
public void windowClosing(windowEvent w)
{
JOptionPane jop=new JOptionPane();
jop.showMessageDialog(null,"Database","thanks",JOptionPane.QUESTION_MESSAGE);
}
}
}
Output:
Conclusion
Thus the java program was successfully complied and executed and result also verified.