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.  
  15. class CalculatorPanel extends JPanel implements ActionListener
  16.  { 
  17. JButton n1,n2,n3,n4,n5,n6,n7,n8,n9,n0,plus,minus,mul,div,dot,equal; static JTextField result=new JTextField("0",45);

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

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

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

  106. } } }