import java.awt.*; import java.awt.event.*; import awb.*; /* * CompSci 1.___________ * author: dr * date: 9/17/04 * assignment: __________________ */ public class Couples extends java.applet.Applet implements ActionListener { TextField m1, m2; Button b1; String folks[] = {"Smith", "Jones", "Smith", "Adams", "Baker", "Jones", "Astor"}; public void init(){ m1 = new TextField(60); m2 = new TextField(60); b1 = new Button("Count Them"); m1.setText("Nested Loop Project"); add(m1); add(b1); add(m2); b1.addActionListener(this); } public void actionPerformed(ActionEvent event) { int n, sz = folks.length; Object cause = event.getSource(); if (cause == b1) { // can't really be anything else n = countCouples(folks, sz); m2.setText("There are " + n + " couples in " + sz + " names."); } } public int countCouples(String[] names, int size){ int count = 0; int k = 0; while (k < size){ int j = k + 1; while (j < size){ if (names[k].equals(names[j])) count = count + 1; j = j + 1; } k = k + 1; } return count; } }