Вы находитесь на странице: 1из 40

PSN ENGINEERING COLLEGE

Melathediyoor-Tirunelveli

Sub Title: Java Lab


Prepared by P.Murugan.AP.CSE Dept of CSE P.S.N Engineering College.

Simple Java Programs Ex. No: 1

A) DISPLAY A STRING

Aim: To implement a java program to display a string. Algorithm: Step 1: Start the program by creating a class. Step 2: Add main function. Step 3: Print the string by using print statement. Step 4: End the program by closing the class. Program: // Save as simple1.java class simple1 { public static void main(String args[]) { System.out.println("Welcome to java world"); } }

Output: Welcome to java world

B) ADD TWO NUMBERS


Aim: To implement a java program to add two numbers. Algorithm: Step 1: Start the program by creating a class. Step 2: Add main function. Step 3: Assign the values for two variables. Step 4: Add two variables and the result stored in another variable. Step 5: Display the result. Step 6: End the program by closing the class. Program: class add { public static void main(String args[]) { int a=10; int b=40; int c=a+b; System.out.println("The value is:"+c); } } Output: The value is: 50

C) AREA OF SQUARE
Aim: To implement a java program to calculate area of square. Algorithm: Step 1: Start the program by adding header files and creating a class. Step 2: Add main function. Step 3: Initialize the values for variable. Step 4: Get the side of the square from the user as input. Step 5: Calculate area of square. Step 6: Display the result. Step 7: End the program by closing the class. Program: import java.io.*; class area { public static void main(String args[]) throws Exception { float area=0; System.out.println("Enter the side of square:"); DataInputStream ds=new DataInputStream(System.in); int a=Integer.parseInt(ds.readLine()); area=a*a; System.out.println("The area of square is:"+area); } } Output: Enter the side of square: 5 The area of square is: 25

D) STUDENT DETAILS

Aim: To implement a java program to display the student details. Algorithm: Step 1: Start the program by adding header files and creating a class. Step 2: Add main function. Step 3: Declare the variables. Step 4: Get the student details as input from the user. Step 5: Display the student details. Step 6: End the program by closing the class. Program: import java.io.*; class student { public static void main(String args[])throws Exception { String name; int m1,m2,m3,m4,regno,total,avg; System.out.println("Enter the details"); DataInputStream ds=new DataInputStream(System.in); System.out.println("Enter the name"); name=ds.readLine(); System.out.println("Enter the regno"); regno=Integer.parseInt(ds.readLine()); System.out.println("Enter the mark1"); m1=Integer.parseInt(ds.readLine()); System.out.println("Enter the mark2"); m2=Integer.parseInt(ds.readLine()); System.out.println("Enter the mark3"); m3=Integer.parseInt(ds.readLine()); System.out.println("Enter the mark4"); m4=Integer.parseInt(ds.readLine()); total=m1+m2+m3+m4; avg=total/4; System.out.println("Student mark sheet"); System.out.println("student name:"+name); System.out.println("regno:"+regno); System.out.println("mark1:"+m1); System.out.println("mark2:"+m2); System.out.println("mark3:"+m3); System.out.println("mark4:"+m4); System.out.println("Total="+total);

System.out.println("Average="+avg); } } Output: Enter the Enter the Raja Enter the 03IT04 Enter the 85 Enter the 95 Enter the 80 Enter the 90 details name reg no mark1 mark2 mark3 mark4

Student mark sheet Student name: Raja Reg no: 03IT04 mark1:85 mark2:95 mark3:80 mark4:90 Total =350 Average =87.5

E) TO PRINT N NUMBERS
Aim:

To implement a java program to print N numbers. Algorithm: Step 1: Start the program by adding header files and creating a class. Step 2: Add main function. Step 3: Get the n value from the user. Step 4: Print all the numbers from 0 to n. Step 5: End the program by closing the class. Program: import java.io.*; class element { public static void main(String args[]) throws Exception { System.out.println("Enter the element:"); DataInputStream ds= new DataInputStream(System.in); int n=Integer.parseInt(ds.readLine()); for(int i=0;i<n;i++) { System.out.println(+i); } } } Output: Enter the element: 5 0 1 2 3 4

Result: Thus the all simple java programs are implemented and executed successfully. Ex. No: 2 Aim:

OPERATIONS ON COMPLEX NUMBERS

To implement a java program to perform simple operations on complex numbers. Algorithm: Step 1: Start the program by creating a class. Step 2: Declare the variables for imaginary part and real part of complex numbers. Step 3: Initialize the values for variables by creating constructor. Step 4: Create first method to add two complex numbers. Step 5: Create second method to subtract two complex numbers. Step 6: Display the result of addition and subtraction by creating another method. Step 7: End the program by closing the class. Program: class complex { double re; double im; complex(double real,double imag) { re=real; im=imag; } public complex plus(complex b) { double real=re+b.re; double imag=im+b.im; return new complex(real,imag); } public complex minus(complex b) { double real=re-b.re; double imag=im-b.im; return new complex(real,imag); } public void display() { if(im<0) System.out.println(re+"-"+(-im)+"i"); else System.out.println(re+"+"+(im)+"i"); } public static void main(String args[]) { complex a=new complex(5.0,6.0); complex b=new complex(3.0,8.0); System.out.print("a=");

a.display(); System.out.print("b="); b.display(); complex c=a.plus(b); complex d=a.minus(b); System.out.print("Addition Result:"); c.display(); System.out.print("Subtraction Result:"); d.display(); } } Output: a=5.0+6.0i b=3.0+8.0i Addition Result: 8.0+14.0i Subtraction Result 2.0-2.0i

Result: Thus java program for performing operations on complex numbers was verified and executed successfully.

STACK IMPLEMENTATION AND DOCUMENTATION


Ex. No: 3 Aim:

To develop a java program to implement the stack using package and generate document by using java Doc comments. Algorithm: Step 1: Start the program by creating a directory and a package in that directory with same name. Step 2: Create a class with in the same package. Step 3: Declare the variables for stack size as 10 and stack pointer. Step 4: Initialize the value for pointer variable by creating constructor. Step 4: Create a method to PUSH operation. Step 5: To perform PUSH operation check this (top of stack==9) condition. Step 6: Create another method to POP operation. Step 7: To perform POP operation check this (top of stack==0) condition. Step 8: Create another method to display the stack elements. Step 9: Close the class. Step 10: Adding header files and create a main class with main method outside of the directory. Step 11: Adding menu as 1.PUSH 2.POP 3. DISPLAY 4. EXIT. Step 12: Get the choice from the user to perform specified stack operation. Step 13: End the program by closing the class. Step 14: Generate the document for stack. (javadoc filename.java) Program: //Create one directory Mypack // save as stack.java package Mypack; public class stack { int stck[]=new int[10]; int tos; // intialize stack public stack() { tos=-1; } //push an item on to the stack public void push(int item) { if(tos==9) System.out.println("Stack is full"); else stck[++tos]=item; }

//Pop an item from stack public int pop() { if(tos<0) System.out.println("Stack is empty"); else return stck[tos--]; return 0; } public void display() { for(int i=0;i<=tos;i++) System.out.println(stck[i]); } } // save as pstack.java import java.io.*; import Mypack.stack; /** * Protocol for stacks. * @author III IT Students */ class pstack { public static void main(String args[]) throws Exception { int ch,i; stack a=new stack(); DataInputStream ds=new DataInputStream(System.in); do { System.out.print("Enter your Choice\n 1.PUSH\n 2.POP \n 3. DISPLAY \n 4.EXIT\n"); ch=Integer.parseInt(ds.readLine()); switch(ch) { case 1: System.out.println("Enter the number:"); i=Integer.parseInt(ds.readLine()); a.push(i); break; /** * Insert a new item into the stack. * @param i the item to insert. */

case 2: int b=a.pop(); System.out.println("Popped Element "+b); break; case 3: a.display(); break; case 4: System.exit(0); } }while(ch!=4); /** * Remove the most recently inserted item from the stack. */ } } Output: Enter your Choice 1. PUSH 2. POP 3. DISPLAY 4. EXIT 1 Enter the element: 22 Enter your Choice 1. PUSH 2. POP 3. DISPLAY 4. EXIT 1 Enter the element: 23 Enter your Choice 1. PUSH 2. POP 3. DISPLAY 4. EXIT 1

Enter the element 21 Enter your Choice 1. PUSH 2. POP 3. DISPLAY 4. EXIT 3 The stack elements are: 22 The stack elements are: 23 The stack elements are: 21 Enter your Choice 1. PUSH 2. POP 3. DISPLAY 4. EXIT 2 Popped Element: 21 Enter your Choice 1. PUSH 2. POP 3. DISPLAY 4. EXIT 3 The stack elements are: 22 The stack elements are: 23 Enter your Choice 1. PUSH 2. POP 3. DISPLAY 4. EXIT 4 Documentation of stack Loading source file stack.java Constructing java doc information Building tree for all the packages and classes. Building index for all the packages and classes. Generating over view tree.html.. Generating index all.html.

Generating deprecated list.html Building index for all classes.. Generating all classes frame.html.. Generating index.html Generating packages.html Generating stack.html. Generating serialized form.html.. Generating package list.html. Generating help.doc.html.. Generating stylesheet.css

Result: Thus the java program for implementation and documentation of the stack was verified and executed successfully. Ex. No: 4 QUEUE IMPLEMENTATION AND

DOCUMENTATION

Aim: To develop a java program to implement the Queue and generate document by using java Doc comments. Algorithm: Step 1: Start the program by creating a directory and a package in that directory with same name. Step 2: Create a class with in the same package. Step 3: Declare the variables for queue size as 10 and pointers. Step 4: Initialize the value for pointer variables by creating constructor. Step 4: Create a method to INSERT operation. Step 5: To perform INSERT operation check this (rear==9) condition. Step 6: Create another method to DELETE operation. Step 7: To perform DELETE operation check this (front==rear) condition. Step 8: Create another method to display the queue elements. Step 9: Close the class. Step 10: Adding header files and create a main class with main method outside of the directory. Step 11: Adding menu as 1.INSERT 2.DELETE 3.DISPLAY 4.EXIT. Step 12: Get the choice from the user to perform specified queue operation. Step 13: End the program by closing the class. Step 14: Generate the document for queue. (javadoc filename.java)

Program: // create one directory Mypack // save as queue.java package Mypack; public class queue { int que[]=new int[10]; int rear; int front; // intialize Queue public queue() { front=-1; rear=-1; } //push an item on to the queue public void insert(int item) { if(rear==9) System.out.println("Queue is full");

else que[++rear]=item; } //Pop an item from queue public int delete() { if(front==rear) System.out.println("Queue is empty"); else return que[++front]; return 0; } public void display() { System.out.println("The Queue elements are"); for(int i=front+1;i<=rear;i++) System.out.println(que[i]); } } //Save as pqueue.java import java.io.*; import Mypack.queue; /** * Protocol for Queues. * @author III IT Students */ public class pqueue { public static void main(String args[]) throws Exception { int ch,i; queue a=new queue(); DataInputStream ds=new DataInputStream(System.in); do { System.out.print("Enter your Choice\n 1.INSERT \n 2.DELETE \n 3.DISPLAY \n 4.EXIT \n"); ch=Integer.parseInt(ds.readLine()); switch(ch) { case 1: System.out.println("Enter the number:"); i=Integer.parseInt(ds.readLine()); a.insert(i); break; /**

* Insert a new item into the Queue. * @param i the item to insert. */ case 2: int b=a.delete(); System.out.println("Deleted Elements are "+b); break; case 3: a.display(); break; case 4: System.exit(0); } }while(ch!=4); /** * Remove the most recently inserted item from the Queue. */ } }

Output: Enter your Choice 1. INSERT 2. DELETE 3. DISPLAY 4. EXIT 1 Enter the element: 22 Enter your Choice 1. INSERT 2. DELETE 3. DISPLAY 4. EXIT 1 Enter the element: 23 Enter your Choice 1. INSERT 2. DELETE

3. DISPLAY 4. EXIT 1 Enter the element 21 Enter your Choice 1. INSERT 2. DELETE 3. DISPLAY 4. EXIT 3 The queue elements are: 22 The queue elements are: 23 The queue elements are: 21 Enter your Choice 1. INSERT 2. DELETE 3. DISPLAY 4. EXIT 2 Popped Element: 22 Enter your Choice 1. INSERT 2. DELETE 3. DISPLAY 4. EXIT 3 The queue elements are: 23 The queue elements are: 21 Enter your Choice 1. INSERT 2. DELETE 3. DISPLAY 4. EXIT 4 Documentation of Queue Loading source file queue.java

Constructing java doc information Building tree for all the packages and classes. Building index for all the packages and classes. Generating over view tree.html.. Generating index all.html. Generating deprecated list.html Building index for all classes.. Generating all classes frame.html.. Generating index.html Generating packages.html Generating queue.html. Generating serialized form.html.. Generating package list.html. Generating help.doc.html.. Generating stylesheet.css

Result: Thus the java program for implementation and documentation of the queue was verified and executed successfully.

Ex. No: 5

INTERFACE USING STACK

Aim: To implement a java program to develop the stack using interface concept. Algorithm: Step 1: Start the program by creating an interface. Step 2: Declaring the methods to perform PUSH and POP operations of stack. Step 3: Create a class to implement the interface. Step 4: Declare the variables for stack size as 10 and stack pointer. Step 5: Initialize the value for pointer variable by creating constructor. Step 6: Create a method to PUSH operation. Step 7: To perform PUSH operation check this (top of stack==9) condition. Step 8: Create another method to POP operation. Step 9: To perform POP operation check this (top of stack==0) condition. Step 10: Create another method to display the stack elements. Step 11: Close the class. Step 12: Adding header files and create a main class with main method. Step 13: Adding menu as 1.PUSH 2.POP 3. DISPLAY 4. EXIT. Step 14: Get the choice from the user to perform specified stack operation. Step 15: End the program by closing the class. Program: import java.io.*; interface intstack { void push(int item); int pop(); } class arraystack implements intstack { int stck[]=new int[10]; int tos; // intialize stack arraystack() { tos=-1; } //push an item on to the stack public void push(int item) { if(tos==9) System.out.println("Stack is full"); else stck[++tos]=item;

} //Pop an item from stack public int pop() { if(tos<0) System.out.println("Stack is empty"); else return stck[tos--]; return 0; } public void display() { System.out.println("The stack elements are"); for(int i=0;i<=tos;i++) System.out.println(stck[i]); } } class intfinal { public static void main(String args[]) throws Exception { int ch,i; arraystack a = new arraystack(); DataInputStream ds=new DataInputStream(System.in); do { System.out.print("Enter your Choice \n 1.PUSH\n 2.POP \n 3.Display \n 4.Exit\n"); ch=Integer.parseInt(ds.readLine()); switch(ch) { case 1: System.out.println("Enter the element:"); i=Integer.parseInt(ds.readLine()); a.push(i); break; case 2: int b=a.pop(); System.out.println("Popped Element "+b); break; case 3: a.display(); break; case 4: System.exit(0);

} } while(ch!=4); } } Output: Enter your Choice 1. PUSH 2. POP 3. DISPLAY 4. EXIT 1 Enter the element: 22 Enter your Choice 1. PUSH 2. POP 3. DISPLAY 4. EXIT 1 Enter the element: 23 Enter your Choice 1. PUSH 2. POP 3. DISPLAY 4. EXIT 1 Enter the element 21 Enter your Choice 1. PUSH 2. POP 3. DISPLAY 4. EXIT 3 The stack elements are: 22 The stack elements are: 23 The stack elements are: 21

Enter your Choice 1. PUSH 2. POP 3. DISPLAY 4. EXIT 2 Popped Element: 21 Enter your Choice 1. PUSH 2. POP 3. DISPLAY 4. EXIT 3 The stack elements are: 22 The stack elements are: 23 Enter your Choice 1. PUSH 2. POP 3. DISPLAY 4. EXIT 4

Result: Thus the java program for implementation of the stack using interface concept was verified and executed successfully. Ex. No: 6 Aim:

DYNAMIC POLYMORPHISM

To develop a java program to implement the dynamic polymorphism concept. Algorithm: Step 1: Start the program by adding header files and creating a base class (shape). Step 2: Define a method (public void area ()) within that same class and close that base class. Step 3: Create a subclass (rectangle) which extends the base class properties. Step 4: Declare the variables and initialize those variables through constructor of this sub class. Step 5: Calculate the area of rectangle by acquiring base class method. Step 6: Close this sub class (rectangle). Step 7: Create another subclass (square) which extends the base class properties. Step 8: Declare the variables and initialize those variables through constructor of this sub class. Step 9: Calculate the area of square by acquiring base class method. Step 10: Close this sub class (square). Step 11: Create another subclass (circle) which extends the base class properties. Step 12: Declare the variables and initialize those variables through constructor of this sub class. Step 13: Calculate the area of circle by acquiring base class method. Step 14: Close this sub class (circle). Step 15: Create a subclass (triangle) which extends the base class properties. Step 16: Declare the variables and initialize those variables through constructor of this sub class. Step 17: Calculate the area of triangle by acquiring base class method. Step 18: Close this sub class (triangle). Step 19: Create a main class. Step 20: Create objects for all the classes. Step 21: Assign each subclass object to base class object (At run time only one object perform different tasks). Step 22: End the program by closing the class. Program: import java.io.*; class shape { void area() { System.out.println("\nBase method is called"); } } // Area of Rectangle class rectangle extends shape { int x,y; rectangle(int a,int b) { x=a; y=b; }

void area() { int area1=x*y; System.out.println("\nThe area of Rectangle is:"+area1); } } // Area of square class square extends shape { int a; square(int x) { a=x; } void area() { int area2=a*a; System.out.println("\nThe area of square is:"+area2); } } // Area of circle class circle extends shape { int r; circle(int a) { r=a; } void area() { double area3=3.142*r; System.out.println("\nThe area of circle is:"+area3); } } //Area of Triangle class triangle extends shape { int b,h; triangle(int a,int b1) { b=a; h=b1; } void area() {

double area4=0.5*b*h; System.out.println("\nThe area of Triangle is:"+area4); } } // main class class dynamicpoly { public static void main(String args[]) { shape t; shape s=new shape(); rectangle obj1=new rectangle(10,5); square obj2=new square(10); circle obj3=new circle(5); triangle obj4=new triangle(5,2); System.out.println("\n***********Dynamic polymorphism**********"); s.area(); System.out.println("\nBase object is point to Rectangle object"); t=obj1; t.area(); System.out.println("\nBase object is point to square object"); t=obj2; t.area(); System.out.println("\nBase object is point to circle object"); t=obj3; t.area(); System.out.println("\nBase object is point to triangle object"); t=obj4; t.area(); } } Output: ***********Dynamic polymorphism********** Base object is point to Rectangle object The area of Rectangle is: 50 Base object is point to square object The area of square is: 100 Base object is point to circle object The area of circle is: 15.71 Base object is point to triangle object The area of Triangle is:5.0

Result: Thus the java program for implementation of the dynamic polymorphism concept was verified and executed successfully. Ex. No: 7 IMPLEMENTATION OF PRODUCER AND

CONSUMER

Aim:

To develop a java program to implement the producer consumer problem using multithreading. Algorithm: Step 1: Start the program by adding header files and creating a base class (shape). Step 2: Define a method (public void area ()) within that same class and close that base class. Step 3: Create a subclass (rectangle) which extends the base class properties. Step 4: Declare the variables and initialize those variables through constructor of this sub class. Step 5: Calculate the area of rectangle by acquiring base class method. Step 6: Close this sub class (rectangle). Step 7: Create another subclass (square) which extends the base class properties

Program: //Save as pcfinal.java class Q { int n; boolean valueset=false; synchronized int get() { if(!valueset) try { wait(); } catch(Exception e) { System.out.println("Exception caught"); } System.out.println("Got:"+n); valueset=false; notify(); return n; } synchronized void put(int n) { if(valueset) try { wait(); }

catch(Exception e) { System.out.println("Exception caught"); } this.n=n; valueset=true; System.out.println("Put:"+n); notify(); } } class producer implements Runnable { Q q; producer(Q q) { this.q=q; new Thread(this,"Producer").start(); } public void run() { int i=0; while(true) { q.put(i++); } } } class consumer implements Runnable { Q q; consumer(Q q) { this.q=q; new Thread(this,"consumer").start(); } public void run() { while(true) { q.get(); } } } class pcfinal {

public static void main(String args[]) { Q q =new Q(); new producer(q); new consumer(q); System.out.println("Control-c to stop"); } } Output: Put: 0 Control-c to stop Got: 0 Put: 1 Got: 1 Put: 2 Got: 2 Put: 3 Got: 3 Put: 4 Got: 4 Put: 5 Got: 5

Result: Thus the java program for implementation of producer and consumer concept was verified and executed successfully. Ex. No: 8

SCIENTIFIC CALCULATOR

Aim: To implement a java program to develop a scientific calculator. Algorithm:

Program: import java.awt.*; import java.awt.event.*; import java.util.*; import javax.swing.*; import java.lang.*; public class Calculator extends JFrame implements ActionListener { JLabel lbl_firstNo; JTextField txt_firstNo; JLabel lbl_secondNo; JTextField txt_secondNo; JLabel lbl_result; JTextField txt_result; JButton cmd_add; JButton cmd_sub; JButton cmd_mul; JButton cmd_div; JButton cmd_clear; JButton cmd_exit; JButton cmd_sin; JButton cmd_cos; JButton cmd_tan; public Calculator() { Container c; c=getContentPane(); c.setLayout(null);

Color cr=new Color(50,180,220); c.setBackground(cr); Font lbl_f=new Font("Times New Roman",Font.BOLD,16); Font txt_f=new Font("Times New Roman", Font.PLAIN,16); Font cmd_f=new Font("Arial Black",Font.BOLD,22); //Color cn=new Color(180,80,100); lbl_firstNo=new JLabel("First No:"); c.add(lbl_firstNo); lbl_firstNo.setBounds(150,30,100,30); lbl_firstNo.setForeground(Color.blue); lbl_firstNo.setFont(lbl_f); txt_firstNo=new JTextField(""); txt_firstNo.setText("0"); c.add(txt_firstNo); txt_firstNo.setBounds(270,30,145,30); txt_firstNo.setFont(txt_f); lbl_secondNo=new JLabel("Second No:"); c.add(lbl_secondNo); lbl_secondNo.setBounds(150,70,100,30); lbl_secondNo.setForeground(Color.blue); lbl_secondNo.setFont(lbl_f); txt_secondNo=new JTextField(""); txt_secondNo.setText("0"); c.add(txt_secondNo); txt_secondNo.setBounds(270,70,145,30); txt_secondNo.setFont(txt_f); lbl_result=new JLabel("Result:"); c.add(lbl_result); lbl_result.setBounds(150,110,100,30); lbl_result.setForeground(Color.blue); lbl_result.setFont(lbl_f); txt_result=new JTextField(""); c.add(txt_result); txt_result.setBounds(270,110,145,30); txt_result.setFont(txt_f); cmd_add=new JButton("+"); c.add(cmd_add); cmd_add.setBounds(40,170,50,30); cmd_add.setFont(cmd_f); cmd_sub=new JButton("-"); c.add(cmd_sub); cmd_sub.setBounds(85,170,50,30); cmd_sub.setFont(cmd_f); cmd_mul=new JButton("*"); c.add(cmd_mul); cmd_mul.setBounds(135,170,50,30);

cmd_mul.setFont(cmd_f); cmd_div=new JButton("/"); c.add(cmd_div); cmd_div.setBounds(185,170,50,30); cmd_div.setFont(cmd_f); cmd_sin=new JButton("sin"); c.add(cmd_sin); cmd_sin.setBounds(260,170,60,30); cmd_sin.setForeground(Color.red); cmd_cos=new JButton("cos"); c.add(cmd_cos); cmd_cos.setBounds(320,170,60,30); cmd_cos.setForeground(Color.red); cmd_tan=new JButton("tan"); c.add(cmd_tan); cmd_tan.setBounds(380,170,60,30); cmd_tan.setForeground(Color.red); cmd_clear=new JButton("Clear"); c.add(cmd_clear); cmd_clear.setBounds(120,240,90,30); cmd_clear.setForeground(Color.red); cmd_exit=new JButton("Exit"); c.add(cmd_exit); cmd_exit.setBounds(270,240,90,30); cmd_exit.setForeground(Color.red); cmd_add.addActionListener(this); cmd_sub.addActionListener(this); cmd_mul.addActionListener(this); cmd_div.addActionListener(this); cmd_sin.addActionListener(this); cmd_cos.addActionListener(this); cmd_tan.addActionListener(this); cmd_clear.addActionListener(this); cmd_exit.addActionListener(this); setVisible(true); setTitle("Scientific Calculator"); setSize(500,350); setLocation(320,250); } public void actionPerformed(ActionEvent e) { try { String fNo=txt_firstNo.getText(); String sNo=txt_secondNo.getText(); int a=Integer.parseInt(fNo);

int b=Integer.parseInt(sNo); if(e.getSource()==cmd_add) { int sum=a+b; txt_result.setText(""+sum); } else if(e.getSource()==cmd_sub) { int sub=a-b; txt_result.setText(""+sub); } else if(e.getSource()==cmd_mul) { int mul=a*b; txt_result.setText(""+mul); } else if(e.getSource()==cmd_div) { float div=a/b; txt_result.setText(""+div); } else if(e.getSource()==cmd_sin) { double s=Math.sin((a*3.14)/180); txt_result.setText(""+s); } else if(e.getSource()==cmd_cos) { double c1=Math.cos((a*3.14)/180); txt_result.setText(""+c1); } else if(e.getSource()==cmd_tan) { double t=Math.tan((a*3.14)/180); txt_result.setText(""+t); } } catch(Exception e1) { System.out.println("Error Occur");} if(e.getSource()==cmd_clear) { txt_firstNo.setText(""); txt_secondNo.setText(""); txt_result.setText(""); }

if(e.getSource()==cmd_exit) { System.exit(0); } } public static void main(String args[]) { Calculator cal=new Calculator(); } } Output:

Result: Thus the java program for implementation of scientific calculator was verified and executed successfully.

Ex. No: 8

GUI APPLICATION

Aim: To implement a java program to develop a GUI application.

Algorithm:

Program: import java.awt.*; import java.awt.event.*; import java.util.*; import javax.swing.*; public class GUI extends JFrame implements ActionListener { JLabel lbl_firstNo; JTextField txt_firstNo; JLabel lbl_secondNo; JTextField txt_secondNo; JLabel lbl_result; JTextField txt_result; JButton cmd_concatenate; JButton cmd_clear; JButton cmd_exit; public GUI() { Container c; c=getContentPane(); c.setLayout(null); Color cr=new Color(50,180,220); c.setBackground(cr); Font lbl_f=new Font("Times New Roman",Font.BOLD,16); Font txt_f=new Font("Times New Roman", Font.PLAIN,16); Font cmd_f=new Font("Arial Black",Font.BOLD,22); //Color cn=new Color(180,80,100); lbl_firstNo=new JLabel("Enter First String"); c.add(lbl_firstNo); lbl_firstNo.setBounds(150,30,100,30); lbl_firstNo.setForeground(Color.blue);

lbl_firstNo.setFont(lbl_f); txt_firstNo=new JTextField(""); c.add(txt_firstNo); txt_firstNo.setBounds(270,30,145,30); txt_firstNo.setFont(txt_f); lbl_secondNo=new JLabel("Enter Second String:"); c.add(lbl_secondNo); lbl_secondNo.setBounds(150,70,100,30); lbl_secondNo.setForeground(Color.blue); lbl_secondNo.setFont(lbl_f); txt_secondNo=new JTextField(""); c.add(txt_secondNo); txt_secondNo.setBounds(270,70,145,30); txt_secondNo.setFont(txt_f); lbl_result=new JLabel("Result:"); c.add(lbl_result); lbl_result.setBounds(150,110,100,30); lbl_result.setForeground(Color.blue); lbl_result.setFont(lbl_f); txt_result=new JTextField(""); c.add(txt_result); txt_result.setBounds(270,110,145,30); txt_result.setFont(txt_f); cmd_add=new JButton("+"); c.add(cmd_add); cmd_add.setBounds(40,170,50,30); cmd_add.setFont(cmd_f); cmd_clear=new JButton("Clear"); c.add(cmd_clear); cmd_clear.setBounds(120,240,90,30); cmd_clear.setForeground(Color.red); cmd_exit=new JButton("Exit"); c.add(cmd_exit); cmd_exit.setBounds(270,240,90,30); cmd_exit.setForeground(Color.red); cmd_add.addActionListener(this); cmd_clear.addActionListener(this); cmd_exit.addActionListener(this); setVisible(true); setTitle(" GUI application"); setSize(500,350); setLocation(320,250); } public void actionPerformed(ActionEvent e) { try

{ String fNo=txt_firstNo.getText(); String sNo=txt_secondNo.getText(); String a=fNo; String b=sNo; if(e.getSource()==cmd_concatenate) { String sum=a+b; txt_result.setText(""+sum); } } catch(Exception e1){System.out.println("Error Occur");} if(e.getSource()==cmd_clear) { txt_firstNo.setText(""); txt_secondNo.setText(""); txt_result.setText(""); } if(e.getSource()==cmd_exit) { System.exit(0); } } public static void main(String args[]) { GUI ca=new GUI(); } } Output:

Result: Thus the java program for the implementation of GUI application was verified and executed successfully. Ex. No: 9 Implementation of Date Format

Aim: To develop a java program to implement Date format.

Algorithm:

Program: import java.text.*; import java.util.*; public class DateFormatDemo { public static void main(String args[]) { Date date=new Date(); DateFormat df; df=DateFormat.getDateInstance(DateFormat.SHORT,Locale.JAPAN); System.out.println("Japan:"+df.format(date)); df=DateFormat.getDateInstance(DateFormat.MEDIUM,Locale.KOREA); System.out.println("Korea:"+df.format(date)); df=DateFormat.getDateInstance(DateFormat.LONG,Locale.UK); System.out.println("United Kingdom:"+df.format(date)); df=DateFormat.getDateInstance(DateFormat.FULL,Locale.US); System.out.println("United States:"+df.format(date)); } } Output: Japan : 10/10/7 Korea : 2010-10-7 United Kingdom: 7 October 2010 United States : Thursday October 7,2010 Result: Thus the java program for the implementation of Date format was verified and executed successfully. Ex. No: 9 System time Using Date Class

Aim: To develop a java program to get system time using Date class. Algorithm:

Program: import java.util.*; class systemtime{ public static void main(String args[]) { Date d1 = new Date(); System.out.println("The current time is :"+d1); } } Output: The current time is : Thu Oct 7 15:33:37 IS7 2010

Result: Thus the java program for getting the system time was verified and executed successfully.

Вам также может понравиться