Monday, 13 July 2015

Java Programs[Important]

  1. WRITE A JAVA PROGRAM that works as a simple calculator. Use a grid layout to arrange buttons for the digits and for the + - x / % operations. Add atext field to display the result. Program :
  2.  
  3. import   x.swing.*;
  4.  import   .awt.*; 
  5. import   .awt.event.*; 
  6. //<applet code=Calculator height=300 width=200></applet>
  7.  public class Calculator extends JApplet
  8.  { 
  9. public void init()
  10.  { 
  11. CalculatorPanel calc=new CalculatorPanel(); getContentPane().add(calc); 
  12. }
  13.  
  14. class CalculatorPanel extends JPanel implements ActionListener
  15.  { 
  16. JButton n1,n2,n3,n4,n5,n6,n7,n8,n9,n0,plus,minus,mul,div,dot,equal; static JTextField result=new JTextField("0",45);

  17. static String lastCommand=null;
  18.  JOptionPane p=new JOptionPane(); 
  19. double preRes=0,secVal=0,res;
  20.  
  21. private static void assign(String no)
  22.  { if((result.getText()).equals("0")) result.setText(no); else if(lastCommand=="=") { result.setText(no); lastCommand=null; } else result.setText(result.getText()+no); }
  23.  
  24. public CalculatorPanel() { setLayout(new BorderLayout()); result.setEditable(false); result.setSize(300,200); add(result,BorderLayout.NORTH);  

  25. JPanel panel=new JPanel(); panel.setLayout(new GridLayout(4,4));
  26.  
  27. n7=new JButton("7"); panel.add(n7); n7.addActionListener(this); n8=new JButton("8"); panel.add(n8); n8.addActionListener(this); n9=new JButton("9"); panel.add(n9); n9.addActionListener(this); div=new JButton("/"); panel.add(div); div.addActionListener(this);
  28.  
  29. n4=new JButton("4"); panel.add(n4); n4.addActionListener(this); n5=new JButton("5"); panel.add(n5); n5.addActionListener(this); 
  30. n6=new JButton("6"); panel.add(n6); n6.addActionListener(this); mul=new JButton("*"); panel.add(mul); mul.addActionListener(this);
  31.  
  32. n1=new JButton("1"); panel.add(n1); n1.addActionListener(this); n2=new JButton("2"); panel.add(n2); n2.addActionListener(this); n3=new JButton("3"); panel.add(n3); n3.addActionListener(this); minus=new JButton("-"); panel.add(minus); minus.addActionListener(this);
  33.  
  34. dot=new JButton(".");
  35.  panel.add(dot);  
  36. dot.addActionListener(this);
  37.  n0=new JButton("0");
  38.  panel.add(n0); 
  39. n0.addActionListener(this);
  40.  equal=new JButton("=");
  41.  panel.add(equal); 
  42. equal.addActionListener(this);
  43.  plus=new JButton("+"); 
  44. panel.add(plus);
  45.  plus.addActionListener(this); 
  46. add(panel,BorderLayout.CENTER); 
  47. }
  48.  public void actionPerformed(ActionEvent ae) { if(ae.getSource()==n1) 
  49. assign("1"); 
  50. else if(ae.getSource()==n2) 
  51. assign("2"); 
  52. else if(ae.getSource()==n3)
  53.  assign("3"); 
  54. else if(ae.getSource()==n4)
  55.  assign("4"); 
  56. else if(ae.getSource()==n5)
  57.  assign("5");
  58.  else if(ae.getSource()==n6)
  59.  assign("6");
  60.  else if(ae.getSource()==n7)
  61.  assign("7"); 
  62. else if(ae.getSource()==n8)
  63.  assign("8");   

  64. else if(ae.getSource()==n9)
  65.  assign("9"); 
  66. else if(ae.getSource()==n0)
  67.  assign("0");
  68.  else if(ae.getSource()==dot)
  69.  { 
  70. if(((result.getText()).indexOf("."))==-1) result.setText(result.getText()+"."); 
  71. else if(ae.getSource()==minus)
  72.  {
  73.  preRes=Double.parseDouble(result.getText());
  74.  lastCommand="-";
  75.  result.setText("0");
  76.  } 
  77. else if(ae.getSource()==div)
  78.  { 
  79. preRes=Double.parseDouble(result.getText());
  80.  lastCommand="/"; result.setText("0");
  81.  } 
  82. else if(ae.getSource()==equal)
  83.  { 
  84. secVal=Double.parseDouble(result.getText());   
  85.  
  86. if(lastCommand.equals("/")) 
  87. res=preRes/secVal; 
  88. else if(lastCommand.equals("*")) 
  89. res=preRes*secVal; 
  90. else if(lastCommand.equals("-"))
  91.  res=preRes-secVal;
  92.  else if(lastCommand.equals("+"))
  93.  res=preRes+secVal;
  94.  result.setText(" "+res); 
  95. lastCommand="="; 
  96. }
  97.  else if(ae.getSource()==mul
  98. ) { preRes=Double.parseDouble(result.getText()); 
  99. lastCommand="*"; result.setText("0"); 
  100. else if(ae.getSource()==plus) { preRes=Double.parseDouble(result.getText());
  101.  lastCommand="+";
  102.  result.setText("0"); 

  103. } } }