Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and beginning April 20th, 2021 (Eastern Time) the Yahoo Answers website will be in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.

How would I design this Java program?

I want to design a special Java program that works like a code machine. I have already made the window layout for the program in a class called KeyTranslator. This program cannot do anything right now because it is missing the syntax that will process the user's input.

This is how it will work:

I have already made the layout of the program in BreezySwing, so I would like to try and keep to the BreezySwing syntax for the rest of the program. The window is arranged with two JTextArea objects and one JButton object. Each text area is at the top and bottom of the window with the button in between. The user will key whatever they want into the upper text area until the text is just as they want it. Then they will click the "Translate" button in the middle. Then the program must be able to evaluate each letter, number, or symbol that the user has keyed into the upper text area. It must then display what the user has just keyed into the bottom text area; only this time in a special code. I already know how the code is associated with different letters, numbers, and symbols.

The code that is displayed must be in the same order and position as the text that the user has entered.

What would the syntax for this program include?

Update:

Please note:

I have no experience with traditional GUI (AWT, Swing), so it would be highly advisable to use BreezySwing for the main part of this program. If that is not possible, lean more towards Swing and not towards complicated sets of classes.

Here is an example of how the program could work:

If the user input is 'helloworld', the program could somehow evaluate each letter in the string.

I thought that it might be able to use the length() method to determine the length of the string, and then somehow read each character in specific positions in the string. I have no clue if this is possible, but it would seperate the string into this:

h e l l o w o r l d

Each letter would have its own position in the string (h is 1; w is 6; d is 10) so that a set of if-else statements could determine what they are and construct a string of code to return to the user.

1 Answer

Relevance
  • Anonymous
    1 decade ago
    Favorite Answer

    A naive attempt:

    Make the class implement ActionListener

    Add the class to the button as a listener.

    Button.addActionListener(this);

    now add this method to the class:

    public void actionPerformed(ActionEvent e)

    {

    String plainText = upperTextArea.getText();

    String encoded = myEncryptionMethod(plainText);

    lowerTextArea.setText(encoded);

    }

    That will handle the wiring up of the events. Of course, I left the hard work up to you, that is the actual writing of the method to handle the encryption.

    Also, this violates a general principle: NEVER do any real work in the listener. You should actual send the work off to be done in the dispatch thread, but that's a much more complicated way of doing things.

Still have questions? Get your answers by asking now.