Skip to main content

Assignment No: 5

Assignment No: 5 


Set A:

a)Write a java program that works as a simple calculator. Use a grid layout to arrange buttons for the digits and for the +, -, *, /, % operations. Add a text filed to display the result.

CODE:

👇

  1. import java.awt.*;
  2. import java.awt.event.*;
  3. import javax.swing.*;

  4. public class calc  extends JFrame implements ActionListener
  5. {
  6. String msg=" ";
  7. int v1,v2,result;
  8. JTextField t;
  9. JButton b[]=new JButton[10];
  10. JButton add,sub,mul,div,clear,equals;
  11. char choice;
  12. JPanel p,p1;
  13. calc()
  14. {
  15. setLayout(new BorderLayout());
  16. p =new JPanel();
  17. t=new JTextField(20);
  18. p.add(t);
  19. p1=new JPanel();
  20. p1.setLayout(new GridLayout(5,4));
  21. for(int i=0;i<10;i++)
  22. {
  23. b[i]=new JButton(""+i);
  24. }
  25. equals=new JButton("=");
  26. add=new JButton("+");
  27. sub=new JButton("-");
  28. mul=new JButton("*");
  29. div=new JButton("/");
  30. clear=new JButton("C");
  31. for(int i=0;i<10;i++)
  32. {
  33. p1.add(b[i]);
  34. }
  35. p1.add(equals);
  36. p1.add(add);
  37. p1.add(sub);
  38. p1.add(mul);
  39. p1.add(div);
  40. p1.add(clear);
  41. for(int i=0;i<10;i++)
  42. {
  43. b[i].addActionListener(this);
  44. }
  45. add.addActionListener(this);
  46. sub.addActionListener(this);
  47. mul.addActionListener(this);
  48. div.addActionListener(this);
  49. clear.addActionListener(this);
  50. equals.addActionListener(this);
  51. add(p,BorderLayout.NORTH);
  52. add(p1);
  53. setVisible(true);
  54. setSize(500,500);
  55. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  56. }
  57. public void actionPerformed(ActionEvent ae)
  58. {
  59. String str = ae.getActionCommand();
  60. char ch = str.charAt(0);
  61. if ( Character.isDigit(ch))
  62. t.setText(t.getText()+str);
  63. else if(str.equals("+"))
  64. {
  65. v1=Integer.parseInt(t.getText());
  66. choice='+';
  67. t.setText("");
  68. }
  69. else if(str.equals("-"))
  70. {
  71. v1=Integer.parseInt(t.getText());
  72. choice='-';
  73. t.setText("");
  74. }
  75. else if(str.equals("*"))
  76. {
  77. v1=Integer.parseInt(t.getText());
  78. choice='*';
  79. t.setText("");
  80. }
  81. else if(str.equals("/"))
  82. {
  83. v1=Integer.parseInt(t.getText());
  84. choice='/';
  85. t.setText("");
  86. }
  87. if(str.equals("="))
  88. {
  89. v2=Integer.parseInt(t.getText());
  90. if(choice=='+')
  91. result=v1+v2;
  92. else if(choice=='-')
  93. result=v1-v2;
  94. else if(choice=='*')
  95. result=v1*v2;
  96. else if(choice=='/')
  97. result=v1/v2;
  98. t.setText(""+result);
  99. }
  100. if(str.equals("C"))
  101. {
  102. t.setText("");
  103. }
  104. }
  105. public static void main(String a[])
  106. {
  107. calc ob = new calc();
  108. }
  109. }

b)Design a screen to handle the Mouse Event such as MOUSE_MOVED and MOUSE_CLICK and display the position of the Mouse_Click in a TextField.

CODE:

👇

  1. import java.awt.event.*;
  2. import java.awt.*;
  3. import javax.swing.*;
  4. public class MouseEvents extends JFrame implements MouseListener,MouseMotionListener
  5. {
  6.    JTextField txt1;
  7.    MouseEvents()
  8.    {
  9.       setSize(500,500);
  10.       setLayout(new FlowLayout());
  11.       setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  12.       txt1=new JTextField(30);
  13.       add(txt1);
  14.       addMouseListener(this);
  15.       addMouseMotionListener(this);
  16.       setVisible(true);
  17.    }

  18.    @Override
  19.    public void mouseDragged(MouseEvent e)
  20.    {}

  21.    @Override
  22.    public void mouseMoved(MouseEvent e)
  23.    {
  24.       txt1.setText("Mouse Moved"+e.getX()+","+e.getY());
  25.    }

  26.    @Override
  27.    public void mouseClicked(MouseEvent e)
  28.    {
  29.       txt1.setText("Mouse Clicked"+e.getX()+","+e.getY());
  30.    }
  31.    
  32.    @Override
  33.    public void mousePressed(MouseEvent e)
  34.    {}

  35.    @Override
  36.    public void mouseReleased(MouseEvent e)
  37.    {}

  38.    @Override
  39.    public void mouseEntered(MouseEvent e)
  40.    {}

  41.    @Override
  42.    public void mouseExited(MouseEvent e)
  43.    {}

  44.    public static void main(String[]args)
  45.    {
  46.       MouseEvents mouseEvents = new MouseEvents();
  47.    }
  48. }


Set B:

a) Create the following GUI screen using appropriate layout managers. Accept the name, class,

hobbies of the user and apply the changes and the selected options in a text box.

CODE:

👇

  1. import javax.swing.*;
  2. import java.awt.*;
  3. import java.awt.event.*;

  4. class form extends JFrame implements ActionListener
  5. {
  6.  JLabel l1,l2,l3;
  7.  JButton b;
  8.  JRadioButton r1,r2,r3;
  9.  JCheckBox c1,c2,c3;
  10.  JTextField t1,t2;
  11.  ButtonGroup b1;
  12.  JPanel p1,p2;
  13.  String s;
  14.  static int cnt;
  15.  private StringBuffer s1=new StringBuffer();
  16.  
  17.  form()
  18.  {
  19.   b1=new ButtonGroup();
  20.         p1=new JPanel();
  21.         p2=new JPanel();
  22.         b=new JButton("clear");
  23.         b.addActionListener(this);
  24.   
  25.   r1=new JRadioButton("FY");
  26.    r2=new JRadioButton("SY");
  27.    r3=new JRadioButton("TY");
  28.    
  29.    b1.add(r1);
  30.    b1.add(r2);
  31.    b1.add(r3);
  32.    
  33.    r1.addActionListener(this);
  34.    r2.addActionListener(this);
  35.    r3.addActionListener(this);
  36.     
  37.    c1=new JCheckBox("Music");
  38.    c2=new JCheckBox("Dance");
  39.    c3=new JCheckBox("Sports");
  40.    
  41.     c1.addActionListener(this);
  42.     c2.addActionListener(this);
  43.     c3.addActionListener(this);
  44.     
  45.     l1=new JLabel("Your Name");
  46.     l2=new JLabel("Your Class");
  47.     l3=new JLabel("Your Hobbies");
  48.     
  49.     t1=new JTextField(20);
  50.     t2=new JTextField(30);
  51.    
  52.    p1.setLayout(new GridLayout(5,2));
  53.    p1.add(l1); p1.add(t1);
  54.    p1.add(l2); p1.add(l3);
  55.    p1.add(r1); p1.add(c1);
  56.    p1.add(r2); p1.add(c2);
  57.    p1.add(r3); p1.add(c3);
  58.     
  59.     p2.setLayout(new FlowLayout());
  60.     p2.add(b);
  61.     p2.add(t2);
  62.     
  63.     setLayout(new BorderLayout());
  64.     add(p1,BorderLayout.NORTH);
  65.     add(p2,BorderLayout.CENTER);
  66.     setSize(600,300);
  67.     setVisible(true);
  68.     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  69.    }
  70.    
  71.    public void actionPerformed(ActionEvent e)
  72.    {
  73.      if(e.getSource()==r1)
  74.      {
  75.        cnt++;
  76.        if(cnt==1)
  77.         {
  78.          s=t1.getText();
  79.          s1.append("Name =");
  80.          s1.append(s);
  81.         }
  82.         s1.append("Class=FY");
  83.      }
  84.   
  85.     else if(e.getSource()==r2)
  86.     {
  87.      cnt++;
  88.      if(cnt==1)
  89.       {
  90.         String s= t1.getText();
  91.          s1.append("Name =");
  92.          s1.append(s);    
  93.         }
  94.      s1.append("Class=SY");     
  95.     } 
  96.      else if(e.getSource()==r3)
  97.     {
  98.      cnt++;
  99.      if(cnt==1)
  100.       {
  101.         String s= t1.getText();
  102.          s1.append("Name =");
  103.          s1.append(s);    
  104.         }
  105.      s1.append("Class=TY");   
  106.    }
  107.    
  108.    else if(e.getSource()==c1)
  109.     {
  110.      s1.append("Hobbies=Music");   
  111.     }
  112.      
  113.    else if(e.getSource()==c2)
  114.     {
  115.      s1.append("Hobbies=Dance");   
  116.     }
  117.     
  118.     else if(e.getSource()==c3)
  119.     {
  120.      s1.append("Hobbies=Sports");   
  121.     }
  122.     
  123.     t2.setText(new String(s1));
  124.     if(e.getSource()==b)
  125.      {
  126.       t2.setText(" ");
  127.       t1.setText(" ");
  128.     }
  129.  }
  130.  public static void main(String arg[])
  131.  {
  132.   form f=new form();
  133.  }

       

   


b) Write a java program to design a screen using Awt that will take a username and password. If the username and password are not the same, raise an Exception with the appropriate message. User can have 3 login chance only. Use clear button to clear the TextFields.

CODE:

👇