Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Template package lab10; import java.awt.BorderLayout; import java.awt.Font; impo

ID: 3939407 • Letter: T

Question

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

   }

}

Explanation / Answer

<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 ">
<INPUT TYPE="button" NAME="two" VALUE=" 2 ">
<INPUT TYPE="button" NAME="three" VALUE=" 3 ">
<INPUT TYPE="button" NAME="plus" VALUE=" + ">
<br>
<INPUT TYPE="button" NAME="four" VALUE=" 4 ">
<INPUT TYPE="button" NAME="five" VALUE=" 5 ">
<INPUT TYPE="button" NAME="six" VALUE=" 6 ">
<INPUT TYPE="button" NAME="minus" VALUE=" - ">
<br>
<INPUT TYPE="button" NAME="seven" VALUE=" 7 ">
<INPUT TYPE="button" NAME="eight" VALUE=" 8 ">
<INPUT TYPE="button" NAME="nine" VALUE=" 9 ">
<INPUT TYPE="button" NAME="times" VALUE=" x ">
<br>
<INPUT TYPE="button" NAME="clear" VALUE=" c ">
<INPUT TYPE="button" NAME="zero" VALUE=" 0 ">
<INPUT TYPE="button" NAME="DoIt" VALUE=" = ">
<INPUT TYPE="button" NAME="div" 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>

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote