/* Add === Author Chris Pollett Course: CSCI 102 Clark University Date: Feb. 8 Purpose: This applet allows the user to enter two arbitrarily long ======== numbers in textfields and see their sum printed out after a submit button is pressed. How to Use: =========== javac Add.java appletviewer Add.class Methods: ======== public void init() -- overrides Applet init() to initialise background, textfields, buttons, and listeners. public void actionPerformed(ActionEvent e) -- handles events generated by submit button. Adds two textfields private Vector rev(String s) -- takes a strings and enters it character by character in reverse order into a vector */ import java.util.*; import java.applet.Applet; import java.awt.*; import java.awt.event.*; import java.awt.Font; public class Add extends Applet implements ActionListener { private Font font; private Label l1,l2, blank, output,name; private TextField t1,t2; private Button b; public void init() {//post: background colour set and listeners and GUI components added // //set background and layout // setBackground(new Color(00,255,255)); //background is cyan font = new Font ("SansSerif",Font.BOLD,18); setLayout( new GridLayout( 4,2)); // //now we add the Label's and TextField's // name= new Label("Add Applet"); name.setFont(font); add(name); blank = new Label(""); //blank label to keep GridLayout add(blank); //happy l1 = new Label("Number 1:"); add(l1); t1 = new TextField("0",20); add(t1); l2 = new Label("Number 2:"); add(l2); t2 = new TextField("0",20); add(t2); b = new Button("Click for the sum:"); b.addActionListener(this); add(b); output = new Label("0"); add(output); } public void actionPerformed(ActionEvent e) { //post: sum of two inputs printed to applet Vector v1 = rev(t1.getText()); //convert text in t1 to a Vector Vector v2 = rev(t2.getText()); //convert text in t2 to a Vector int carry=0; int max,i, e1,e2; Vector vsum; StringBuffer b; max = Math.max(v1.size(),v2.size()); vsum =new Vector(); //add the two vectors for(i=0; i=0; i--) {b.append(vsum.elementAt(i));} //output the result output.setText(b.toString()); } private Vector rev (String s) { //post: add each character of s in reverse order into a vector which is then returned Vector v; v =new Vector(); for (int i=s.length()-1; i >=0 ; i--) {v.addElement(new Integer(Character.digit(s.charAt(i),10)));} return v; } }