import java.awt.FlowLayout; import java.awt.BorderLayout; import javax.swing.*; import javax.swing.border.*; import java.awt.event.*; import java.security.AccessControlException; /* * The only code that is missing is in the "code" method below. * Everything else should work fine, so if you implement the * "code" method your pogram will work properly. */ public class Shifter extends JFrame implements ActionListener { private static final int ENCODE = 1; private static final int DECODE = 2; private static final int MIN_CODE = 32; private static final int MAX_CODE = 126; private static final int CODE_RANGE = MAX_CODE - MIN_CODE; private int key = -1; private JTextField keyField = new JTextField(10); private JTextArea plainText = new JTextArea(7, 50); private JTextArea codedText = new JTextArea(7, 50); private JButton encodeButton = new JButton("Encode"); private JButton decodeButton = new JButton("Decode"); private class WindowCloser extends WindowAdapter { public void windowClosing(WindowEvent we) { try { setVisible(false); System.exit(0); } catch(AccessControlException err) { } catch(Exception ex) { } catch(Error err) { } } } public Shifter() { super("Shift Coder"); plainText.setBorder(new TitledBorder(new EtchedBorder(), "Plain Text")); codedText.setBorder(new TitledBorder(new EtchedBorder(), "Coded Text")); JPanel buttonPanel = new JPanel(new FlowLayout()); buttonPanel.add(encodeButton); buttonPanel.add(decodeButton); JPanel keyPanel = new JPanel(new BorderLayout()); keyPanel.add("Center", keyField); keyPanel.add("West", new JLabel("Key = ")); JPanel actionPanel = new JPanel(new FlowLayout()); actionPanel.add(keyPanel); actionPanel.add(buttonPanel); JSplitPane textPanel = new JSplitPane(JSplitPane.VERTICAL_SPLIT); textPanel.setTopComponent(new JScrollPane(plainText)); textPanel.setBottomComponent(new JScrollPane(codedText)); getContentPane().setLayout(new BorderLayout()); getContentPane().add("Center", textPanel); getContentPane().add("South", actionPanel); encodeButton.addActionListener(this); decodeButton.addActionListener(this); addWindowListener(new WindowCloser()); validate(); pack(); } private void setKey(int mode) { try { key = Integer.parseInt(keyField.getText().trim()); if (key <= 0) throw new NumberFormatException("Number must be positive"); if (key >= CODE_RANGE) throw new NumberFormatException("Number must be less than " + CODE_RANGE); if (mode == ENCODE) encode(); else if (mode == DECODE) decode(); } catch(NumberFormatException nfe) { JOptionPane.showMessageDialog(this, "Error: the key must be a positive integer less than " + MAX_CODE); } } /* * CHANGE THIS METHOD OTHERWISE NOTHING HAPPENS */ private String code(String text, int key) { StringBuffer output = new StringBuffer(); for (int i = 0; i < text.length(); i++) { // this is the loop you need to complete. The varialbe "text" // contains the text to encode, and "key" contains the // number by which to shift every character. } return output.toString(); } private void encode() { String text = plainText.getText(); if (text.equals("")) JOptionPane.showMessageDialog(this, "No text to encode"); else codedText.setText(code(text, key)); } private void decode() { String text = codedText.getText(); if (text.equals("")) JOptionPane.showMessageDialog(this, "No text to decode"); else plainText.setText(code(text, -key)); } public void actionPerformed(ActionEvent ae) { if (ae.getSource() == encodeButton) setKey(ENCODE); else if (ae.getSource() == decodeButton) setKey(DECODE); } public static void main(String args[]) { Shifter s = new Shifter(); s.setVisible(true); } }