CIS 260 Assignment: Calculator I

The objective for this assignment is to be able to construct working GUI components following specifications. These components will be used to construct your next project. I have test program listings at the bottom of this page which can do some superficial testing of your components to see if they are working. Please note that the tests are not thorough and your code may still have problems even if it manages to pass the tests. You are expected to test and debug your own code.

Keypad Project (Keypad.java)

You have two major tasks for this assignment. The first is to create the following keypad GUI:

Here are the requirements for the keypad:

  1. it must extend a container (JPanel)
  2. you will need 23 buttons total (see picture for text inside buttons and layout of buttons)
  3. the constructor should:
    1. set the proper layout
    2. create the containers (JPanels) you will need
    3. create the appropriate buttons and add them to the containers (JPanels)
    4. set the proper colors for the text inside the buttons
    5. add the containers (JPanels) to the keypad
    6. Note: It would be easiest if the number buttons are referenced by an array so you don't have to deal with 23 names
  4. there should be an addActionListener routine
    1. JPanels don't normally have them (but your's will)
    2. accept an ActionListener as an argument
    3. add the listener to each button on the keypad (this is easier if all the buttons are just elements of an array)

When you are done with your keypad, you can test it with the driver listed below. It should display the text from the buttons you click in the console window.

/*
   TestKeypad.java
   CIS 260 Calculator 1 Assignmnet
*/

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class TestKeypad extends JFrame implements ActionListener {
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                new TestKeypad();
            }
        });
    }

    public TestKeypad() {
        init();
    }

    public void init() {
        try {
            UIManager.setLookAndFeel(
               "com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
        } catch (Exception e) {
            e.printStackTrace(System.err);
        }
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        Keypad k = new Keypad();
        Container c = getContentPane();
        c.setLayout(new BorderLayout());
        c.add(k);
        k.addActionListener(this);
        setSize(220,200);
        setVisible(true);
    }

    public void actionPerformed(ActionEvent e) {
        final ActionEvent event = e;
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                JButton b = (JButton) event.getSource();
                System.out.println(b.getActionCommand());
            }
        });
    }
}

Number Field Project (NumField.java)

The second part of this project is to create a text field which will display numbers. Think of it as a display for a calculator. The tricky part is to only allow valid numbers.

Here are the requirements for the number field:

  1. base your class on a JTextField
  2. you should have a boolean variable to tell you whether the field currently contains a decimal point
  3. the constructor should:
  4. you should have the following methods:

When you are done with your number field, you can test it with the driver listed below. It should display testing information in the console window.

/*
   TestNumField.java
   CIS 260 Calculator 1 assignment
*/

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class TestNumField extends JFrame {
    NumField nf;

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                new TestNumField();
            }
        });
    }

    public TestNumField() {
        init();
    }

    public void init() {
        try {
            UIManager.setLookAndFeel(
               "com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
        } catch (Exception e) {
            e.printStackTrace(System.err);
        }
        Container c = getContentPane();
        c.setLayout(new BorderLayout());
        nf = new NumField();
        c.add(nf);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setSize(200,60);
        setVisible(true);
        start();
    }

    public void start() {
        System.out.println("Testing Number Field...");

        nf.setValue(123.89);
        if (nf.getText().equals("123.89"))
            System.out.println("...passed setting value test");
        else
            System.out.println("...failed setting value test");

        double d = nf.getValue();
        if (d == 123.89)
            System.out.println("...passed getting value test");
        else
            System.out.println("...failed getting value test");

        nf.append('1');
        System.out.println(nf.getText());
        System.out.println("The line above should read: 123.891");

        nf.append(".7");
        System.out.println(nf.getText());
        System.out.println("The line above should read: 123.8917");

        nf.clear();
        System.out.println(nf.getText());
        System.out.println("The line above should be blank");

        nf.append("124.43");
        System.out.println(nf.getText());
        System.out.println("The line above should read: 124.43");
        System.out.println("Your GUI should also read: 124.43");
    }
}

Your test output should look like this:

Testing Number Field...
...passed setting value test
...passed getting value test
123.891
The line above should read: 123.891
123.8917
The line above should read: 123.8917

The line above should be blank
124.43
The line above should read: 124.43
Your GUI should also read: 124.43