INDEX
SNo Name of the program Page no Date Signature
a) Write a java program to find the Fibonacci series
using recursive and non recursive functions.
1. b) Write a java program to multiply two given matrices.
a) Write a java program for Method overloading and Constructor
overloading.
b) Write a java program to display the employee details using
2. Scanner class.
c) Write a java program that checks whether a given string is
palindrome or not.
a) Write a java program to represent Abstract class with example.
3. b) Write a java program to implement Interface using extends
keyword.
Write a java program to create user defined package.
4.
a) Write a java program to create inner classes.
5.
b) Write a java program for creating multiple catch blocks.
a) Write a java program for producer and consumer problem
using Threads.
6.
b) Write a Java program that implements a multi-thread
application that has three threads.
a) Write a java program to display File class properties.
7.
b) Write a java program to represent ArrayList class.
Write a Java program loads phone no, name from a text file using
8.
hash table.
a) Write an applet program that displays a simple message.
9. b) Write a Java program compute factorial value using Applet.
c) Write a program for passing parameters using Applet.
10. Write a java program for handling Mouse events and Key events
a) Write a java program that connects to a database using JDBC
b)Write a java program to connect to database using
11. JDBC &insert values into table
c)Write a java program to connect to a database using
JDBC and delete values from table.
Write a java program that works as a simple calculator.
Use a Grid Layout to arrange Buttons for digits and for
12. the + - * % operations. Add a text field to display the
result.
JAVA PROGRAMMING LAB
DATE:
A) Write a java program to find the Fibonacci series using recursive and non recursive functions
/*Non Recursive Solution*/
import [Link];
class Fib {
public static void main(String args[ ]) {
Scanner input=new Scanner([Link]);
int i,a=0,b=1,c=0,t;
[Link]("Enter value of t:");
t=[Link]();
[Link](a);
[Link](" "+b);
for(i=0;i<t-2;i++) {
c=a+b;
a=b;
b=c;
[Link](" "+c);
}
[Link]();
[Link](t+"th value of the series is: "+c);
}
}
/* Recursive Solution*/
import [Link].*;
import [Link].*;
class Demo {
int fib(int n) {
if(n==1)
return (1);
else if(n==2)
return (1);
else
return (fib(n-1)+fib(n-2));
}
}
class RecFibDemo {
public static void main(String args[])throws IOException
{ InputStreamReader obj=new InputStreamReader([Link]);
BufferedReader br=new BufferedReader(obj);
[Link]("enter last number");
int n=[Link]([Link]());
JAVA PROGRAMMING LAB
Demo ob=new Demo();
[Link]("fibonacci series is as follows");
int res=0;
for(int i=1;i<=n;i++) {
res=[Link](i);
[Link](" "+res);
}
[Link]();
[Link](n+"th value of the series is "+res);
}
}
JAVA PROGRAMMING LAB
B) Write a java program to multiply two given matrices.
import [Link];
class Matrixmul
{
public static void main(String args[])
{
int m, n, p, q, sum = 0, i, j, k;
Scanner in = new Scanner([Link]);
[Link]("Enter the number of rows and columns of first matrix");
m = [Link]();
n = [Link]();
int first[][] = new int[m][n];
[Link]("Enter elements of first matrix");
for (i = 0; i < m; i++)
for (j = 0; j < n; j++)
first[i][j] = [Link]();
[Link]("Enter the number of rows and columns of second matrix");
p = [Link]();
q = [Link]();
if (n != p)
[Link]("The matrices can't be multiplied with each other.");
else
{
int second[][] = new int[p][q];
int multiply[][] = new int[m][q];
[Link]("Enter elements of second matrix");
for (i = 0; i < p; i++)
for (j = 0; j < q; j++)
second[i][j] = [Link]();
for (i = 0; i < m; i++)
{ for (j = 0; j < q; j++)
{ for (k = 0; k < p; k++)
sum = sum + first[i][k]*second[k][j];
multiply[i][j] = sum;
sum = 0;
}
}
[Link]("Product of the matrices:");
for (i = 0; i < m; i++) {
for (j = 0; j < q; j++)
[Link](multiply[i][j]+"\t");
[Link]("\n");
}
}
}
}
JAVA PROGRAMMING LAB
A) Write a java program for Method overloading and Constructor overloading.
// Method overloading in Java.
public class Sum {
// Overloaded sum(). This sum takes two int parameters
public int sum(int x, int y)
{
return (x + y);
}
// Overloaded sum(). This sum takes three int parameters
public int sum(int x, int y, int z)
{
return (x + y + z);
}
// Overloaded sum(). This sum takes two double parameters
public double sum(double x, double y)
{
return (x + y);
}
// Driver code
public static void main(String args[])
{
Sum s = new Sum();
[Link]([Link](10, 20));
[Link]([Link](10, 20, 30));
[Link]([Link](10.5, 20.5));
}
}
//Constructor overloading in Java.
class StudentData
{
private int stuID;
private String stuName;
private int stuAge;
StudentData()
{
//Default constructor
stuID = 100;
stuName = "New Student";
stuAge = 18;
}
JAVA PROGRAMMING LAB
StudentData(int num1, String str, int num2)
{
//Parameterized constructor
stuID = num1;
stuName = str;
stuAge = num2;
}
//Getter and setter methods
public int getStuID() {
return stuID;
}
public void setStuID(int stuID)
{ [Link] = stuID;
}
public String getStuName()
{ return stuName;
}
public void setStuName(String stuName)
{ [Link] = stuName;
}
public int getStuAge()
{ return stuAge;
}
public void setStuAge(int stuAge)
{ [Link] = stuAge;
}
public static void main(String args[])
{
//This object creation would call the default constructor
StudentData myobj = new StudentData();
[Link]("Student Name is: "+[Link]());
[Link]("Student Age is: "+[Link]());
[Link]("Student ID is: "+[Link]());
/*This object creation would call the parameterized constructor StudentData(int, String, int)*/
StudentData myobj2 = new StudentData(555, "Chaitanya", 25);
[Link]("Student Name is: "+[Link]());
[Link]("Student Age is: "+[Link]());
[Link]("Student ID is: "+[Link]());
}
}
JAVA PROGRAMMING LAB
B) Write a java program to display the employee details using Scanner class.
import [Link];
class Employee
{
int Id;
String Name;
int Age;
long Salary;
void GetData() // Defining GetData()
{
Scanner sc = new Scanner([Link]);
[Link]("\n\tEnter Employee Id : ");
Id = [Link]([Link]());
[Link]("\n\tEnter Employee Name : ");
Name = [Link]();
[Link]("\n\tEnter Employee Age : ");
Age = [Link]([Link]());
[Link]("\n\tEnter Employee Salary : ");
Salary = [Link]([Link]());
void PutData() // Defining PutData()
{
[Link]("\n\t" + Id + "\t" +Name + "\t" +Age + "\t" +Salary);
}
public static void main(String args[])
{
Employee[] Emp = new Employee[3];
int i;
for(i=0;i<3;i++)
Emp[i] = new Employee(); // Allocating memory to each object
for(i=0;i<3;i++)
{
[Link]("\nEnter details of "+ (i+1) +" Employee\n");
Emp[i].GetData();
}
[Link]("\nDetails of Employees\n");
for(i=0;i<3;i++)
JAVA PROGRAMMING LAB
Emp[i].PutData();
}
}
JAVA PROGRAMMING LAB
C) Write a java program that checks whether a given string is palindrome or not.
import [Link];
class ChkPalindrome
{
public static void main(String args[])
{
String str, rev = "";
Scanner sc = new Scanner([Link]);
[Link]("Enter a string:");
str = [Link]();
int length = [Link]();
for ( int i = length - 1; i >= 0; i-- )
rev = rev + [Link](i);
if ([Link](rev))
[Link](str+" is a palindrome");
else
[Link](str+" is not a palindrome");
}
}
JAVA PROGRAMMING LAB
A) Write a java program to represent Abstract class with example.
// Abstract class that contains abstract method.
abstract class Shape
{
abstract void numberOfSides();
}
// Classes that illustrates the abstract method.
class Trapezoid
{
void numberOfSides()
{
[Link]("The no. of side's in trapezoidal are6");
}
}
class Triangle
{
void numberOfSides()
{
[Link]("The no. of side's in triangle are:3 ");
}
}
class Hexagon
{
void numberOfSides()
{ [Link]("The no. of side's in hexagon are:6 ");
}
}
// Class that create objects and call the method.
class ShapeDemo
{
public static void main(String args[])
{
Trapezoid obj1 = new Trapezoid();
Triangle obj2 = new Triangle();
Hexagon obj3 = new Hexagon();
[Link]();
[Link]();
[Link](); }
}
JAVA PROGRAMMING LAB
B) Write a java program to implement Interface using extends keyword.
interface Inf1{
public void method1();
}
interface Inf2 extends Inf1
{ public void method2();
}
public class Demo implements Inf2{
/* Even though this class is only implementing the interface Inf2, it has to implement all
the methods of Inf1 as well because the interface Inf2 extends Inf1 */
public void
method1(){ [Link]("method1");
}
public void
method2(){ [Link]("method2");
}
public static void main(String
args[]){ Inf2 obj = new Demo();
obj.method2();
}
}
JAVA PROGRAMMING LAB
Write a java program to implement Interface using extends keyword.
interface Inf1{
public void method1();
}
interface Inf2 extends Inf1
{ public void method2();
}
public class Demo implements Inf2{
/* Even though this class is only implementing the interface Inf2, it has to implement all
the methods of Inf1 as well because the interface Inf2 extends Inf1 */
public void
method1(){ [Link]("method1");
}
public void
method2(){ [Link]("method2");
}
public static void main(String
args[]){ Inf2 obj = new Demo();
obj.method2();
}
}
JAVA PROGRAMMING LAB
A) Write a java program to create inner classes.
class Outer_Demo {
int num;
// inner class
private class Inner_Demo
{ public void print() {
[Link]("This is an inner class");
}
}
// Accessing he inner class from the method within
void display_Inner() {
Inner_Demo inner = new Inner_Demo();
[Link]();
}
}
public class My_class {
public static void main(String args[]) {
// Instantiating the outer class
Outer_Demo outer = new Outer_Demo();
// Accessing the display_Inner() method.
outer.display_Inner();
}
}
JAVA PROGRAMMING LAB
B) Write a java program for creating multiple catch blocks.
class MultipleExceptionHandling{
public static void main(String
args[]){ try{
[Link]("Begin: try block");
String name="abc";
int nameLength = [Link]();
int res= 10/0;
[Link]("End: try block");
}
catch(ArithmeticException e){
[Link]("Division is not allowed with zero
denominator");
}
catch(NullPointerException
e){ [Link]("Name should not be
Null");
}
catch(Exception
e){ [Link]("General
Exception");
}
[Link]("End: try-catch block");
}
}
JAVA PROGRAMMING LAB
A) Write a java program for producer and consumer problem using Threads.
// Java program to implement solution of producer consumer problem.
import [Link];
public class Threadexample {
public static void main(String[] args)
throws InterruptedException
{
// Object of a class that has both produce() and consume() methods
final PC pc = new PC();
// Create producer thread
Thread t1 = new Thread(new Runnable()
{ @Override
public void run()
{
try {
[Link]();
}
catch (InterruptedException e)
{ [Link]();
}
}
});
// Create consumer thread
Thread t2 = new Thread(new Runnable()
{ @Override
public void run()
{
try {
[Link]();
}
catch (InterruptedException e)
{ [Link]();
}
}
});
// Start both threads
[Link]();
[Link]();
JAVA PROGRAMMING LAB
// t1 finishes before t2
[Link]();
[Link]();
}
// This class has a list, producer (adds items to list and consumer (removes items).
public static class PC {
// Create a list shared by producer and consumer
// Size of list is 2.
LinkedList<Integer> list = new LinkedList<>();
int capacity = 2;
// Function called by producer thread
public void produce() throws InterruptedException
{
int value = 0;
while (true) {
synchronized (this)
{
// producer thread waits while list is full
while ([Link]() == capacity)
wait();
[Link]("Producer produced-"+ value);
// to insert the jobs in the list
[Link](value++);
// notifies the consumer thread that
// now it can start consuming
notify();
// makes the working of program easier
// to understand
[Link](1000);
}
}
}
// Function called by consumer thread
public void consume() throws InterruptedException
{
while (true) {
synchronized (this)
JAVA PROGRAMMING LAB
{
// consumer thread waits while list
// is empty
while ([Link]() == 0)
wait();
// to retrive the ifrst job in the list
int val = [Link]();
[Link]("Consumer consumed-" + val);
// Wake up producer thread
notify();
// and sleep
[Link](1000);
}
}
}
}
}
JAVA PROGRAMMING LAB
B) Write a Java program that implements a multi-thread application that has three threads.
/* The first thread displays "Good Morning" for every one second, the second thread displays "Hello"
for every two seconds and third thread displays "Welcome" for every three seconds */
class GoodMorning extends Thread
{ synchronized public void run() {
try {
int i=0;
while (i<5) {
sleep(1000);
[Link]("Good morning ");
i++;
}
} catch (Exception e) {
}
}
}
class Hello extends Thread
{ synchronized public void run() {
try {
int i=0;
while (i<5) {
sleep(2000);
[Link]("hello");
i++;
}
} catch (Exception e) {
}
}
}
class Welcome extends Thread
{ synchronized public void run() {
try {
int i=0;
while (i<5) {
sleep(3000);
[Link]("welcome");
i++;
}
} catch (Exception e) {
}
}
}
class MultithreadDemo {
public static void main(String args[])
{ GoodMorning t1 = new
GoodMorning(); Hello t2 = new
Hello();
JAVA PROGRAMMING LAB
Welcome t3 = new Welcome();
[Link]();
[Link]();
[Link]();
}
}
JAVA PROGRAMMING LAB
A) Write a java program to display File class properties.
// Program to check if a file or directory physically exist or not.
/* Accept a file or directory name from command line arguments. Then the program will check if
that file or directory physically exist or not and it displays the property of that file or directory.*/
*import [Link];
// Displaying file property
class fileProperty
public static void main(String[] args) {
//accept file name or directory name through command line args
String fname =args[0];
//pass the filename or directory name to File object
File f = new File(fname);
//apply File class methods on File object
[Link]("File name :"+[Link]());
[Link]("Path: "+[Link]());
[Link]("Absolute path:" +[Link]());
[Link]("Parent:"+[Link]());
[Link]("Exists :"+[Link]());
if([Link]())
{
[Link]("Is writeable:"+[Link]());
[Link]("Is readable"+[Link]());
[Link]("Is a directory:"+[Link]());
[Link]("File Size in bytes "+[Link]());
}
}
JAVA PROGRAMMING LAB
B) Write a java program to represent ArrayList class.
// Java program to demonstrate working of ArrayList in Java
import [Link].*;
import [Link].*;
class arrayli
{
public static void main(String[] args) throws IOException
{
// size of ArrayList
int n = 5;
//declaring ArrayList with initial size n
ArrayList<Integer> arrli = new ArrayList<Integer>(n);
// Appending the new element at the end of the list
for (int i=1; i<=n; i++)
[Link](i);
// Printing elements
[Link](arrli);
// Remove element at index 3
[Link](3);
// Displaying ArrayList after deletion
[Link](arrli);
// Printing elements one by one
for (int i=0; i<[Link](); i++)
[Link]([Link](i)+" ");
}
}
JAVA PROGRAMMING LAB
Write a Java program loads phone no, name from a text file using hash table.
/*Create a text file [Link] with name followed by a tab and number
akhil 123
srujan 345
edrf 567
Then create a java file [Link] */
import [Link].*;
import [Link].*;
public class Phonebook
{
public static void main(String args[])
{
try
{
FileInputStream fis=new FileInputStream("[Link]");
Scanner sc=new Scanner(fis).useDelimiter("\t");
Hashtable<String,String> ht=new Hashtable<String,String> ();
String[] strarray;
String a,str;
while([Link]())
{
a=[Link]();
strarray=[Link]("\t");
[Link](strarray[0],strarray[1]);
[Link]("hash table values are"+strarray[0]+":"+strarray[1]);
}
Scanner s=new Scanner([Link]);
[Link]("enter the name as given in the phone book");
str=[Link]();
if([Link](str))
{
[Link]("phone no is"+[Link](str));
}
else
{
[Link]("name is not matched");
}
}
catch(Exception e)
{
[Link](e);
}
}
}
JAVA PROGRAMMING LAB
A) Write an applet program that displays a simple message
[Link]:
// Import the packages to access the classes and methods in awt and applet classes.
import [Link].*;
import [Link].*;
public class Applet1 extends Applet
{
// Paint method to display the message. public void paint(Graphics g)
{
[Link]("HELLO WORLD",20,20);
}
[Link]:
/* <applet code="Applet1" width=200 height=300> </applet>*/
JAVA PROGRAMMING LAB
B) Write a Java program to compute factorial value using Applet.
import [Link].*;
import [Link].*;
import [Link].*;
/*<applet code="Factorial" width=500 height=500></applet> */
public class FactorialApplet extends Applet implements ActionListener
{
Label l1,l2;
TextField t1,t2;
Button b1,b2;
public void init()
{
l1=new Label("Enter a value: ");
l2=new Label("Result:");
t1=new TextField(10);
t2=new TextField(10);
b1=new Button("Calculate");
b2=new Button("Clear");
add(l1);
add(t1);
add(b1);
add(b2);
add(l2);
add(t2);
[Link](this);
[Link](this);
}
public void actionPerformed(ActionEvent ae)
{
int n=[Link]([Link]());
int fact=1;
if([Link]()==b1)
{
if(n==0||n==1)
{
fact=1;
[Link]([Link](fact));
}
else
{
JAVA PROGRAMMING LAB
for(int i=1;i<=n;i++)
fact=fact*i;
}
[Link]([Link](fact));
}
else if([Link]()==b2)
{
[Link]("");
[Link]("");
}
}
}
JAVA PROGRAMMING LAB
C) Write a program for passing parameters using Applet.
import [Link].*;
import [Link].*;
public class MyApplet extends Applet
{
String n;
String a;
public void init()
{
n = getParameter("name");
a = getParameter("age");
}
public void paint(Graphics g)
{
[Link]("Name is: " + n, 20, 20);
[Link]("Age is: " + a, 20, 40);
}
}
/*
<applet code="MyApplet" height="300" width="500">
<param name="name" value="Ramesh" />
<param name="age" value="25" />
</applet>
*/
JAVA PROGRAMMING LAB
Write a java program for handling Mouse events and Key events
//Program to implement mouse events
import [Link].*;
import [Link].*;
import [Link].*;
public class Mouseevents extends Applet implements MouseListener,MouseMotionListener
{
/*<applet code="Mouseevents" width=500 height=300></applet>*/
String msg="";
int mousex=0,mousey=0;
public void init()
{
addMouseListener(this);
addMouseMotionListener(this);
}
public void mouseClicked(MouseEvent me)
{
mousex=0; mousey=10;
msg="mouse clicked";
repaint();
}
public void mouseEntered(MouseEvent me)
{
mousex=0;
mousey=10;
msg="mouse entered";
repaint();
}
public void mouseExited(MouseEvent me)
{
mousex=0;
mousey=10;
msg="mouse exited";
repaint();
}
public void mousePressed(MouseEvent me)
{
mousex=[Link]();
JAVA PROGRAMMING LAB
mousey=[Link]();
msg="down";
repaint();
}
public void mouseReleased(MouseEvent me)
{
mousex=[Link]();
mousey=[Link]();
msg="up";
repaint();
}
public void mouseDragged(MouseEvent me)
{
mousex=[Link]();
mousey=[Link]();
msg="*";
showStatus("Dragging mouse at "+mousex+" , "+mousey);
repaint();
}
public void mouseMoved(MouseEvent me)
{
showStatus("mouse moving at "+[Link]()+" , "+[Link]());
}
public void paint(Graphics g)
{
[Link](msg,mousex,mousey);
}
}
JAVA PROGRAMMING LAB
//Program to implement key events
import [Link].*;
import [Link].*;
import [Link].*;
/*
<applet code="Key" width=300 height=400>
</applet>
*/
public class Key extends Applet implements KeyListener
{
int X=20,Y=30;
String msg="KeyEvents--->";
public void init()
{
addKeyListener(this);
requestFocus();
setBackground([Link]);
setForeground([Link]);
}
public void keyPressed(KeyEvent k)
{
showStatus("KeyDown");
int key=[Link]();
switch(key)
{
case KeyEvent.VK_UP:
showStatus("Move to Up");
break;
case KeyEvent.VK_DOWN:
showStatus("Move to Down");
break;
case KeyEvent.VK_LEFT:
showStatus("Move to Left");
break;
case KeyEvent.VK_RIGHT:
showStatus("Move to Right");
break;
}
repaint();
}
JAVA PROGRAMMING LAB
public void keyReleased(KeyEvent k)
{
showStatus("Key Up");
public void keyTyped(KeyEvent k)
{
msg+=[Link]();
repaint();
}
public void paint(Graphics g)
{
[Link](msg,X,Y);
}
}
JAVA PROGRAMMING LAB
A) Write a java program that connects to a database using JDBC program:
import [Link];
import [Link];
public class PostgreSQLJDBC
{
public static void main(String args[])
{
Connection c = null;
try {
[Link]("[Link]");
c = [Link]("jdbc:postgresql://localhost:5432/testdb",
"postgres", "123");
} catch (Exception e)
{ [Link]();
[Link]([Link]().getName()+": "+[Link]());
[Link](0);
[Link]("Opened database successfully");
}
}
JAVA PROGRAMMING LAB
B) Write a java program to connect to database using JDBC & insert values into
table
import [Link].*;
public class insert1
{
public static void main(String args[])
{
String id = "id1";
String pwd = "pwd1";
String fullname = "MRCET";
String email = "MRCET@[Link]";
try
{
[Link]("[Link]");
Connection con = [Link]("
jdbc:oracle:thin:@localhost:1521:orcl", "login1", "pwd1");
Statement stmt = [Link]();
// Inserting data in database
String q1 = "insert into userid values('" +id+ "', '" +pwd+
"', '" +fullname+ "', '" +email+ "')";
int x = [Link](q1);
if (x > 0)
[Link]("Successfully Inserted");
else
[Link]("Insert Failed");
[Link]();
}
catch(Exception e)
{
[Link](e);
}
}
}
JAVA PROGRAMMING LAB
C) Write a java program to connect to a database using JDBC and delete values from
table.
import [Link].*;
public class delete
{
public static void main(String args[])
{
String id = "id2";
String pwd = "pwd2";
try
{
[Link]("[Link]");
Connection con = [Link]("
jdbc:oracle:thin:@localhost:1521:orcl", "login1", "pwd1");
Statement stmt = [Link]();
// Deleting from database
String q1 = "DELETE from userid WHERE id = '" +
id + "' AND pwd = '" + pwd + "'";
int x = [Link](q1);
if (x > 0)
[Link]("One User Successfully Deleted");
else
[Link]("ERROR OCCURED :(");
[Link]();
}
catch(Exception e)
{
[Link](e);
}
}
}
JAVA PROGRAMMING LAB
[Link] a java program that works as a simple calculator. Use a Grid Layout to arrange Buttons for
digits and for the + - * % operations. Add a text field to display the result.
Program:
import [Link].*;
import [Link]; import [Link].*;
import [Link].*;
// Class that initialize the applet and create calculator.
public class Calculator extends JApplet
{
public void init()
{
CalculatorPanel calc=new CalculatorPanel(); getContentPane().add(calc);
}
}
// Class that creates the calculator panel .
class CalculatorPanel extends JPanel implements ActionListener
{
// Creation of JButton.
JButton n1,n2,n3,n4,n5,n6,n7,n8,n9,n0,plus,minus,mul,div,dot,equal;
static JTextField result=new JTextField("0",45); static String lastCommand=null;
// Create the JObjectPane.
JOptionPane p=new JOptionPane(); double preRes=0,secVal=0,res;
private static void assign(String no)
{
if(([Link]()).equals("0"))
[Link](no); else
if(lastCommand=="=")
{
[Link](no); lastCommand=null; }
else
[Link]([Link]()+no);
}
// Creation of control panel of calculator and adding buttons using
GridLayout. public CalculatorPanel()
{
setLayout(new GridLayout());
[Link](false);
[Link](300,200);
add(result);
JAVA PROGRAMMING LAB
JPanel panel=new JPanel();
[Link](new GridLayout(5,5));
n7=new JButton("7");
[Link](n7);
[Link](this);
n8=new JButton("8");
[Link](n8);
[Link](this);
n9=new JButton("9");
[Link](n9);
[Link](this);
div=new JButton("/");
[Link](div);
[Link](this);
n4=new JButton("4");
[Link](n4);
[Link](this);
n5=new JButton("5");
[Link](n5);
[Link](this);
n6=new JButton("6");
[Link](n6);
[Link](this);
mul=new JButton("*");
[Link](mul);
[Link](this);
n1=new JButton("1");
[Link](n1);
[Link](this);
n2=new JButton("2");
[Link](n2);
[Link](this);
n3=new JButton("3");
[Link](n3);
[Link](this);
minus=new JButton("-");
[Link](minus);
[Link](this);
dot=new JButton(".");
[Link](dot);
[Link](this);
n0=new JButton("0");
[Link](n0); [Link](this);
equal=new JButton("=");
JAVA PROGRAMMING LAB 2018-2019
[Link](equal);
[Link](this); plus=new JButton("+"); [Link](plus);
[Link](this); add(panel);
}
// Implementing method in ActionListener.
public void actionPerformed(ActionEvent
ae)
{
if([Link]()==n1)
assign("1");
else if([Link]()==n2)
assign("2");
else if([Link]()==n3)
assign("3");
else if([Link]()==n4)
assign("4");
else if([Link]()==n5)
assign("5");
else if([Link]()==n6)
assign("6");
else if([Link]()==n7)
assign("7");
else if([Link]()==n8)
assign("8");
else if([Link]()==n9)
assign("9");
else if([Link]()==n0)
assign("0");
else if([Link]()==dot)
{
if((([Link]()).indexOf("."))==-1)
[Link]([Link]()+"."); } else if([Link]()==minus)
{
preRes=[Link]([Link]()); lastCommand="-";
[Link]("0");
}
else if([Link]()==div)
{
preRes=[Link]([Link]());
lastCommand="/";
[Link]("0");
}
JAVA PROGRAMMING LAB
else if([Link]()==equal)
{
secVal=[Link]([Link]());
if([Link]("/"))
res=preRes/secVal;
else if([Link]("*"))
res=preRes*secVal;
else if([Link]("-
")) res=preRes-secVal;
else if([Link]("+"))
res=preRes+secVal;
[Link](" "+res); lastCommand="=";
}
else if([Link]()==mul)
{
preRes=[Link]([Link]());
lastCommand="*";
[Link]("0");
}
else if([Link]()==plus)
{
preRes=[Link]([Link]());
lastCommand="+";
[Link]("0");
}
}
}
[Link]:
<applet code="Calculator" width=200 height=300> </applet>