Ok First we are going to Develop a call calculator use JAVA.......
we wont to make a Two Java Class
like this
class cal{
cal(){
}
}
class createCal{
public static void main(String arges[]){
cla cd=new cal();
}
}
Now get to work
You import a
java.awt.*;
javx.swing.*;
java.awt.event.*;
Next "implements ActionListener" to cal class like this
class cal implements ActionListener {
}
Now we call a "public void actionPerformed(ActionEvent e)" method on cal call scope like this
class cal implements ActionListener {
cla(){
}
public void actionPerformed(ActionEvent e){
}
}
REMEMBER DON'T WRITE THIS METHOD ON CLA CONSTRUCTOR"cal(){}" SCOPE
Make a 16 button variable , 1 text variable , 3 double variable and 1 string variable use cal class scope
TextField tf1;
JButton b1,b2,b3,b4,b5,b6,b7,b8,b9,b10,b11,b12,b13,b14,b15,b16;
double num1,num2,tot;
String ca;
Now we learn to how to make a jbutton. You import a java swing class then you can call a jbutton object like this
b1=new JButton("");
You can make a text fields, panels forms ext.
JFrame ff=new JFrame("");
Panel p1=new Panel();
tf1=new TextField();
Now we learn how to connect an Action Listener on jbutton
b1.addActionListener(this);
Ok. Now can make a java cal use this source code Try it.....
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class cal implements ActionListener {
TextField tf1;
JButton b1,b2,b3,b4,b5,b6,b7,b8,b9,b10,b11,b12,b13,b14,b15,b16;
double num1,num2,tot;
String ca;
cal(){
JFrame ff=new JFrame("java_app4u");
ff.setVisible(true);
ff.setSize(300,300);
Panel p1=new Panel();
Panel p2=new Panel();
BorderLayout bor=new BorderLayout();
ff.add(p1,bor.NORTH);
ff.add(p2,bor.CENTER);
tf1=new TextField(35);
p1.add(tf1);
GridLayout gd1=new GridLayout(4,4,2,2);
p2.setLayout(gd1);
b1=new JButton("1");
b2=new JButton("2");
b3=new JButton("3");
b4=new JButton("+");
b5=new JButton("4");
b6=new JButton("5");
b7=new JButton("6");
b8=new JButton("-");
b9=new JButton("7");
b10=new JButton("8");
b11=new JButton("9");
b12=new JButton("*");
b13=new JButton("/");
b14=new JButton("0");
b15=new JButton("Sqrt");
b16=new JButton("=");
p2.add(b1);
p2.add(b2);
p2.add(b3);
p2.add(b4);
p2.add(b5);
p2.add(b6);
p2.add(b7);
p2.add(b8);
p2.add(b9);
p2.add(b10);
p2.add(b11);
p2.add(b12);
p2.add(b13);
p2.add(b14);
p2.add(b15);
p2.add(b16);
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
b4.addActionListener(this);
b5.addActionListener(this);
b6.addActionListener(this);
b7.addActionListener(this);
b8.addActionListener(this);
b9.addActionListener(this);
b10.addActionListener(this);
b11.addActionListener(this);
b12.addActionListener(this);
b13.addActionListener(this);
b14.addActionListener(this);
b15.addActionListener(this);
b16.addActionListener(this);
}
public void actionPerformed(ActionEvent e){
if(e.getSource().equals(b1)){
tf1.setText(tf1.getText()+e.getActionCommand());
}else if(e.getSource().equals(b2)){
tf1.setText(tf1.getText()+e.getActionCommand());
}else if(e.getSource().equals(b3)){
tf1.setText(tf1.getText()+e.getActionCommand());
}else if(e.getSource().equals(b4)){
String s=tf1.getText();
num1=Double.parseDouble(s);
tf1.setText(null);
ca=e.getActionCommand();
}else if(e.getSource().equals(b5)){
tf1.setText(tf1.getText()+e.getActionCommand());
}else if(e.getSource().equals(b6)){
tf1.setText(tf1.getText()+e.getActionCommand());
}else if(e.getSource().equals(b7)){
tf1.setText(tf1.getText()+e.getActionCommand());
}else if(e.getSource().equals(b8)){
String s=tf1.getText();
num1=Double.parseDouble(s);
tf1.setText(null);
ca=e.getActionCommand();
}else if(e.getSource().equals(b9)){
tf1.setText(tf1.getText()+e.getActionCommand());
}else if(e.getSource().equals(b10)){
tf1.setText(tf1.getText()+e.getActionCommand());
}else if(e.getSource().equals(b11)){
tf1.setText(tf1.getText()+e.getActionCommand());
}else if(e.getSource().equals(b12)){
String s=tf1.getText();
num1=Double.parseDouble(s);
tf1.setText(null);
ca=e.getActionCommand();
}else if(e.getSource().equals(b13)){
String s=tf1.getText();
num1=Double.parseDouble(s);
tf1.setText(null);
ca=e.getActionCommand();
}else if(e.getSource().equals(b14)){
tf1.setText(tf1.getText()+e.getActionCommand());
}else if(e.getSource().equals(b15)){
String s=tf1.getText();
tot=Double.parseDouble(s);
tf1.setText(""+Math.sqrt(tot));
}else if(e.getSource().equals(b16)){
String s=tf1.getText();
num2=Double.parseDouble(s);
if(ca.equals("+")){
tot=num1+num2;
}else if(ca.equals("-")){
tot=num1-num2;
}else if(ca.equals("*")){
tot=num1*num2;
}else if(ca.equals("/")){
tot=num1/num2;
}
tf1.setText(tot+"");
}
}
}
class ccal{
public static void main(String args[]){
cal css=new cal();
}
}
Thank You....