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

   }

}

Lab 10 November 11, 2016 1 Overview In this lab, you will be creating a small program that looks like the keypad of a calculator. You should try to recreate the screenshot on the next page. For reference, the frame is 300-by-300 pixels and the number (and dot) buttons are in a separate panel. Think carefully about the types of layout managers to use for each section of the frame. The accumulator label should begin with the text \"0\". For the accumulator label, I use the following code to make the font larger and right-aligned label.setFont (new Font (\"Arial\", Font.PLAIN, 40)); label.setHorizontalAlignment (SwingConstants.RIGHT); Functional Requirements You also have a number of functional requirements. Whenever you press a key, you should append the label of that key onto the accumulator label. This allows you to type numbers. However, you should be aware of several things » Leading zeroes should be ignored. If your first click is the \"2\" button, the . You should only allow one dot to be entered. A second press of the button » If you press \"C\" button, the number should be cleared and be replaced accumulator should read \"2\", not \"02\" should have no observable effect with \"O\" It is up to you to figure out how to elegantly handle these cases

Solution

Answer:

Modified Code:

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

              //check if number button is pressed

              for(int kk=0;kk<10;kk++)

              {

                   if(src==buttons[kk])

                   {

                        pad.addDigit(kk);

                        label.setText(pad.print());

                        break;

                   }

              }

              //if dot button is pressed

              if(src==dot)

              {

                   pad.addDot();

                   label.setText(pad.print());

                                                                                                                                                                       

              }

              //if clear button is pressed

              if(src==clear)

              {

                   pad.clear();

                   label.setText(pad.print());

                                                                                                                                                                       

              }   

              }

         

              };

          // add the listener to all buttons

          // TODO

          for(int kk=0;kk<10;kk++)

          {

              buttons[kk]=new JButton(String.valueOf(kk));

              buttons[kk].addActionListener(l);

          }

          dot.addActionListener(l);

          clear.addActionListener(l);

         

     

          // add the buttons to the panel

          // TODO

          panel.add(buttons[7]);

          panel.add(buttons[8]);

          panel.add(buttons[9]);

          panel.add(buttons[4]);

          panel.add(buttons[5]);

          panel.add(buttons[6]);

          panel.add(buttons[1]);

          panel.add(buttons[2]);

          panel.add(buttons[3]);

          panel.add(clear);

          panel.add(buttons[0]);

          panel.add(dot);

         

         

     

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

          // TODO

          frame.add(label, BorderLayout.NORTH);

          frame.add(panel, BorderLayout.CENTER);

          frame.pack();

              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

          integer=\"0\";

          fraction=\"\";

          hasDot=false;

   }

   // return the string to be displayed

   String print()

     {

              // TODO

          if(hasDot)

              return integer+\".\"+fraction;

          return integer;

   }

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

   void addDot()

     {

              // TODO

          hasDot=true;

         

   }

   // change the keypad state to include a digit

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

   void addDigit(int i)

     {

              // TODO

          if(hasDot)

              fraction=fraction+(Integer.toString(i));

          else

          {

              if(integer.equals(\"0\"))

              {

                   integer=Integer.toString(i);

              }

              else

                   integer = integer+Integer.toString(i);

          }

     }

         

  

}

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
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