CS 1401 Assignment #6

Date Assigned: Monday, October 8, or Tuesday, October 9, 2007.

Due Date: Monday, October 15, or Tuesday, October 16, 2007, before the beginning of your lab section.

Objective: The main objective of this assignment is to learn to use GUI.

Programming assignment: design and implement a GUI program that asks a student for a positive integer and transforms it into the binary code.

The following algorithm can be used to convert a decimal positive integer n into the binary form (as a string): you divide n by 2, keep a remainder. Divide the result by 2, keep a remainder, etc. until we get 0. The remainders, when read bottom to top, form the desired binary number.

Example of a binary code: for n = 13, we have

13 / 2 = 6 rem 1
 6 / 2 = 3 rem 0
 3 / 2 = 1 rem 1
 1 / 2 = 0 rem 1
When we read the remainders from bottom to top, we get 1101, which is exactly the binary representation of 13 -- since 1 * 8 + 1 * 4 + 0 * 2 + 1 * 1 = 8 + 4 + 1 = 13.

Hint: make a loop in which a string is gradually increased. Originally, the result-so-far is the input integer (e.g., 13), the string-so-far is empty. Within the loop, you divide the result-so-far by 2, compute the remainder, and add the corresponding one-character string ("0" or "1") to the left of the string-so-far, etc.


Solution:

public class Assignment6{

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

  public class Binary extends JFrame{
    private static final int WIDTH = 400;
    private static final int HEIGHT = 400;
    private JLabel decimalL, binaryL;
    private JTextField decimalTF, binaryTF;
    private JButton computeB, exitB;

    private ComputeButtonHandler cbHandler;
    private ExitButtonHandler ebHandler;

    public GPA(){
      //describe title
      setTitle("Conversion to binary");

      //create labels
      decimalL = new JLabel("Enter a decimal number: ", SwingConstants.RIGHT);
      binaryL = new JLabel("Its binary form is: ", SwingConstants.RIGHT);

      //create text fields
      decimalTF = new JTextField(20);
      binaryTF  = new JTextField(20);

      //create compute button
      computeB = new JButton("Compute");
      cbHandler = new ComputeButtonHandler();
      computeB.addActionListener(cbHandler);

      //create exit button
      exitB = new JButton("Exit");
      ebHandler = new ExitButtonHandler();
      exitB.addActionListener(ebHandler);

      //create the window
      Container window = getContentPane();

      //set the layout
      window.setLayout(new GridLayout(3,2));

      //place components of GUI into the pane
      window.add(decimalL);    window.add(decimalTF);
      window.add(binaryL);     window.add(binaryTF);
      window.add(computeB);    window.add(exitB);

      //set the size of the window and display it
      setSize(WIDTH, HEIGHT);
      setVisible(true);
      setDefaultCloseOperation(EXIT_ON_CLOSE);
    }

    private class ComputeButtonHandler implements ActionListener {
      public void actionPerformed(ActionEvent e){
        int resultSoFar;
        int remainder;
        String stringSoFar = "";
        String frontSymbol;

        //reading the decimal number
        resultSoFar = Int.parseInt(decimalTF.getText());

        //translating to binary
        while (resultSoFar > 0){
          //dividing by 2 and computing the remainder
          remainder = resultSoFar % 2;
          resultSoFar /= 2;

          //adding a character to the string
          if (remainder == 0)
            {frontSymbol = "0";}
          else
            {frontSymbol = "1";}
          stringSoFar = frontSymbol + stringSoFar;
        }

        binaryTF.setText(stringSoFar);
      }
    }
    private class ExitButtonHandler implements ActionListener{
      public void actionPerformed(ActionEvent e){
        System.exit(0);
      }
    }
    public static void main(String [] args){
      Binary myBinary = new Binary();
    }
}

Homework assignment: on a separate sheet of paper, solve Ex. 8, 16, and 18 at the end of Chapter 6.

Deliverables: as instructed by your TA.