// AddBigNumbers // // This program adds two binary numbers together. // // The only part of this that is directly relevant to the class is // the first function, AddBigNumbers. The rest, you can ignore if you'd // like. (I'm not stopping you from looking over it, too, if you're // interested, but it's not necessary.) import java.awt.*; public class AddBigNumbers extends Frame { protected Label header; protected TextField[] num = new TextField[2]; protected Button dotest; protected Button doquit; protected int unfocused; // AddBigNumbers // // calculates the sum of two binary numbers and , // represented as arrays of ints whose entries are only 0's and // 1's, with the least significant bit (the right-hand side when // written) in the first slot. // // The sum is placed into . // // The function returns 0 if successful and -1 if the sum doesn't // fit into . public static int AddIntegers(int[] result, int[] a, int[] b) { return -1; } // AddBigNumbers // // creates the dialog box public AddBigNumbers() { super("Add numbers"); Panel p; GridBagConstraints c; unfocused = 0; c = new GridBagConstraints(); c.gridx = 0; c.gridwidth = 1; c.gridheight = 1; c.ipady = 5; c.anchor = GridBagConstraints.CENTER; c.weightx = 1.0; // create the label giving output to user this.setLayout(new GridBagLayout()); header = new Label("Hello, world! Enter the numbers to add."); c.gridy = 0; c.weighty = 0.0; c.fill = GridBagConstraints.HORIZONTAL; ((GridBagLayout) this.getLayout()).setConstraints(header, c); this.add(header); // create text field to get first bit string from user num[0] = new TextField("1010101"); c.gridy = 1; c.weighty = 1.0; c.fill = GridBagConstraints.HORIZONTAL; ((GridBagLayout) this.getLayout()).setConstraints(num[0], c); this.add(num[0]); // create text field to get second bit string from user num[1] = new TextField("0101010"); c.gridy = 2; c.weighty = 1.0; c.fill = GridBagConstraints.HORIZONTAL; ((GridBagLayout) this.getLayout()).setConstraints(num[1], c); this.add(num[1]); // create a Panel to hold the buttons. p = new Panel(); p.setLayout(new FlowLayout(FlowLayout.CENTER, 15, 15)); // add the ``Test'' button dotest = new Button("Test"); p.add(dotest); // add the ``Quit'' button doquit = new Button("Quit"); p.add(doquit); // put Panel in dialog c.gridy = 3; c.weighty = 0.0; c.fill = GridBagConstraints.NONE; ((GridBagLayout) this.getLayout()).setConstraints(p, c); this.add(p); this.pack(); } // action // // handle button clicks public boolean action(Event e, Object arg) { String input; String output; int[][] a = new int[2][]; int[] result = new int[8]; int err; int which; int i; if(e.target == num[0]) { num[1].requestFocus(); return true; } else if(e.target == dotest || e.target == num[1]) { // find the sum // get the first number for(which = 0; which < 2; which++) { input = num[which].getText(); a[which] = new int[input.length()]; for(i = 0; i < a[which].length; i = i + 1) { if(input.charAt(i) == '0') { a[which][a[which].length - 1 - i] = 0; } else if(input.charAt(i) == '1') { a[which][a[which].length - 1 - i] = 1; } else { header.setText("The numbers contain a non-bit " + "character ('" + input.charAt(i) + "')."); return true; } } } // get result err = AddIntegers(result, a[0], a[1]); if(err != 0) { header.setText("The sum did not fit in " + result.length + " bits."); return true; } // put output in the header label output = "The sum is "; for(i = result.length - 1; i >= 0; i = i - 1) { if(result[i] == 0) { output = output + "0"; } else if(result[i] == 1) { output = output + "1"; } else { header.setText("The output contains the invalid " + "bit " + result[i] + "."); return true; } } header.setText(output + "."); return true; } else if(e.target == doquit) { this.dispose(); return true; } else { return false; } } // gotFocus // // sets the default focus location public boolean gotFocus(Event e, Object arg) { if(e.target == num[1]) { num[1].requestFocus(); } else if(e.target == num[0] || unfocused == 1) { unfocused = 0; num[0].requestFocus(); } return true; } // main // // this is where the program begins public static void main(String[] args) { Frame f; AddBigNumbers dialog; // create the dialog box f = new AddBigNumbers(); f.resize(300, 150); f.show(); } }