Exercises - Applets
  1. Write a program that displays 10 JButtons on the bottom side, a JTextArea in the middle, and a JTextField at the top. Above the JTextArea there should be a label saying 'Output:'. Above the JTextField there should be a label saying 'Input:'. The 10 JButtons should be labeled with the digits from '0' to '9'. Use a for loop to add the JButtons. Use String.valueOf(...) to convert an integer to a string.

  2. Further develop the applet in the above exercise to add the following features:
    1. Set the font size of the JTextArea to 22 pt.
    2. When '0' is clicked, "No little piggies went to market" is appended to the JTextArea on a new line. When '1' is clicked, "1 little piggy went to market" is appended on a new line. For the other buttons, "X little piggies went to market" is appended where 'X' is the number on the JButton that is clicked.
    3. The last line appended to the JTextArea is always displayed.
    Use an array of JButtons to reference them. For example, the following defines an array of references to JButtons
       JButton button[] = new JButton[10];
    and the following actually installs the buttons into the array and adds an ActionListener to each
       for (int i=0 ; i < 10 ; i++) {
          button[i] = new JButton(String.valueOf(i));
    Similarly, use a for loop in actionPerformed to recognize buttons 2 to 9.

  3. Modify the applet of the previous exercise to create a number in the JTextField by clicking on the digit buttons: the click of a digit button should insert the digit labeling that button to the right of all the digits currently in the JTextField. If '0' is clicked when no digits appear in the JTextField then a '0' is not inserted. Set the font of the JTextField to 22 pt. Use
    to set justify the digits of the JTextField on the right.

  4. Add a second row of JButtons below the digit buttons. Label then '*', '-', '/', and '+'. These buttons will be used to perform simple arithmetic operations involving the JTextField with the results being appended to the JTextArea. Specifically, when an arithmetic operator button is clicked, the number in the JTextField is added to, subtracted from, multiplied by or divided into the last number appended to the JTextArea and the result is appended to the JTextArea. For simplicity, and since we are using only integers here, suppose the division is 'integer division' (that is, the remainder is lost).

    Try using BigIntegers (see the API for details).

    Use an 'accumulator' variable to hold the last value appended to the JTextArea instead of trying to read the number from it. That will save a lot of effort.