Template package lab10 import javaawtBorderLayout import jav

Template

package lab10;

import java.awt.BorderLayout;

import java.awt.Font;

import java.awt.GridLayout;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import javax.swing.JButton;

import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.JPanel;

import javax.swing.SwingConstants;

@SuppressWarnings(\"serial\")

public class Lab10 extends JFrame {

   Lab10() {

       this.setTitle(\"Keypad\");

       this.setSize(300, 300);

       this.setLayout(new BorderLayout());

       this.setDefaultCloseOperation(EXIT_ON_CLOSE);

   }

  

   public static void main(String[] args) {

       JFrame frame = new Lab10();

      

       // label is used to display the digits (right aligned) on the keypad

       JLabel label = new JLabel(\"0\");

       label.setFont(new Font(\"Arial\", Font.PLAIN, 40));

       label.setHorizontalAlignment(SwingConstants.RIGHT);

      

       // panel is used to hold the buttons on the keypad

       JPanel panel = new JPanel();

       panel.setLayout(new GridLayout(4, 3, 4, 4));

      

       // buttons array store the digit buttons

       JButton[] buttons = new JButton[10];

       JButton dot = new JButton(\".\"), clear = new JButton(\"C\");

      

       Keypad pad = new Keypad();

       ActionListener l = new ActionListener(){

           // use event source to find out which button is clicked

           // call method on \"pad\" to change the state of the keypad

           // then use the \"print\" method to get the text to be displayed

           // and set the text of the label

           @Override

           public void actionPerformed(ActionEvent e) {

               Object src = e.getSource();

               // TODO

           }

          

       };

       // add the listener to all buttons

       // TODO

      

       // add the buttons to the panel

       // TODO

      

       // add the label (north) and the panel (center) to the frame

       // TODO

       frame.setVisible(true);

   }

}

// This class tracks the state of the keypad when digits and dot are added

// It also resets the keypad state through clear method and generates the string to be displayed

class Keypad {

   String integer = \"0\", fraction = \"\";

   boolean hasDot = false;

  

   // reset the keypad state so that it displays 0

   void clear() {

       // TODO

   }

   // return the string to be displayed

   String print() {

       // TODO

   }

   // change the keypad state to indicate dot is added (so it can have fractions)

   void addDot() {

       // TODO

   }

   // change the keypad state to include a digit

   // must not allow leading 0 unless it is a fraction

   void addDigit(int i) {

       // TODO

   }

}

Solution

<FORM NAME=\"Calc\">
<TABLE BORDER=4>
<TR>
<TD>
<INPUT TYPE=\"text\" NAME=\"Input\" Size=\"16\">
<br>
</TD>
</TR>
<TR>
<TD>
<INPUT TYPE=\"button\" NAME=\"one\" VALUE=\" 1 \" OnClick=\"Calc.Input.value += \'1\'\">
<INPUT TYPE=\"button\" NAME=\"two\" VALUE=\" 2 \" OnCLick=\"Calc.Input.value += \'2\'\">
<INPUT TYPE=\"button\" NAME=\"three\" VALUE=\" 3 \" OnClick=\"Calc.Input.value += \'3\'\">
<INPUT TYPE=\"button\" NAME=\"plus\" VALUE=\" + \" OnClick=\"Calc.Input.value += \' + \'\">
<br>
<INPUT TYPE=\"button\" NAME=\"four\" VALUE=\" 4 \" OnClick=\"Calc.Input.value += \'4\'\">
<INPUT TYPE=\"button\" NAME=\"five\" VALUE=\" 5 \" OnCLick=\"Calc.Input.value += \'5\'\">
<INPUT TYPE=\"button\" NAME=\"six\" VALUE=\" 6 \" OnClick=\"Calc.Input.value += \'6\'\">
<INPUT TYPE=\"button\" NAME=\"minus\" VALUE=\" - \" OnClick=\"Calc.Input.value += \' - \'\">
<br>
<INPUT TYPE=\"button\" NAME=\"seven\" VALUE=\" 7 \" OnClick=\"Calc.Input.value += \'7\'\">
<INPUT TYPE=\"button\" NAME=\"eight\" VALUE=\" 8 \" OnCLick=\"Calc.Input.value += \'8\'\">
<INPUT TYPE=\"button\" NAME=\"nine\" VALUE=\" 9 \" OnClick=\"Calc.Input.value += \'9\'\">
<INPUT TYPE=\"button\" NAME=\"times\" VALUE=\" x \" OnClick=\"Calc.Input.value += \' * \'\">
<br>
<INPUT TYPE=\"button\" NAME=\"clear\" VALUE=\" c \" OnClick=\"Calc.Input.value = \'\'\">
<INPUT TYPE=\"button\" NAME=\"zero\" VALUE=\" 0 \" OnClick=\"Calc.Input.value += \'0\'\">
<INPUT TYPE=\"button\" NAME=\"DoIt\" VALUE=\" = \" OnClick=\"Calc.Input.value = eval(Calc.Input.value)\">
<INPUT TYPE=\"button\" NAME=\"div\" VALUE=\" / \" OnClick=\"Calc.Input.value += \' / \'\">
<br>
</TD>
</TR>
</TABLE>
</FORM>

<p align=\"center\"><font face=\"arial\" size=\"-2\">This free script provided by</font><br>
<font face=\"arial, helvetica\" size=\"-2\"><a href=\"http://javascriptkit.com\">JavaScript
Kit</a></font></p>

Template package lab10; import java.awt.BorderLayout; import java.awt.Font; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event
Template package lab10; import java.awt.BorderLayout; import java.awt.Font; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event
Template package lab10; import java.awt.BorderLayout; import java.awt.Font; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event
Template package lab10; import java.awt.BorderLayout; import java.awt.Font; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site