Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Question: - For the code below, need help to modify code in order to use menus i

ID: 3530423 • Letter: Q

Question

Question: - For the code below, need help to modify code in order to use menus instead of buttons for open,save, and clear - Therefore atm code uses open, save and clear buutons, need to modify code to use menus instead ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// Tester \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ package q1; import javax.swing.JFrame; import q1.LetterFrequencyGUI; public class tester{ /** * Start me up with no command line arguments */ public static void main(String[] args) { JFrame frame = new LetterFrequencyGUI(); frame.pack(); frame.setVisible(true); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } } /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// class /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// package q1; import java.io.*; import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.border.LineBorder; import java.util.Scanner; /** * *Graphical User Interface for *calculating and computing letter frequency data for selcted txt file. *shows results in a display region and it also has the ability to save to a file * */ public class LetterFrequencyGUI extends JFrame implements ActionListener { private JButton ChooseFileKey; private JButton savekey; private JButton ClearKey; private JTextArea output; private JFileChooser chooser; /** * * Construct a GUI for the letter frequencies */ public LetterFrequencyGUI() { chooser = new JFileChooser(); setTitle("Letters Frequency"); ChooseFileKey = new JButton("Open Document"); savekey = new JButton("Save in File"); ClearKey = new JButton("Clear the Display"); // Output area for displaying frequency table output = new JTextArea(45,50); output.setEditable(false); //Top panel containing the buttons JPanel buttonPanel = new JPanel(); buttonPanel.setLayout(new FlowLayout()); buttonPanel.add(ChooseFileKey); buttonPanel.add(savekey); buttonPanel.add(ClearKey); //Add button panel in north and display area in center Container cp = getContentPane(); cp.setLayout(new BorderLayout()); cp.add(buttonPanel, BorderLayout.NORTH); cp.add(new JScrollPane(output), BorderLayout.CENTER); //Button handlers will be inner classes which implement the //ActionListener interface ChooseFileKey.addActionListener(this); savekey.addActionListener(this); ClearKey.addActionListener(this); } public void actionPerformed(ActionEvent e) { if(e.getSource() == ChooseFileKey) calculateFrequencies(); else if (e.getSource() == savekey) saveOutput(); else clearOutput(); } /** * Get an input file and calculate and display its frequency table */ public void calculateFrequencies() { // NOTE: We must catch any exceptions since actionPerformed cannot // have a throws clause. chooser.showOpenDialog(null); File inFile = chooser.getSelectedFile(); //String name = inFile.getAbsolutePath(); // Create array for 26 letter frequencies. // 'A' to 'Z' correspond to indices 0 to 25 so the //index of upper case letter c is c - 'A' int[] frequency = new int[26]; //Initialize frequency array and letter counter to zero int totalLetters = 0; for (int k = 0; k < frequency.length; k++) { frequency[k] = 0; } //Read the input file a line at a time Scanner in = null; try { in = new Scanner(inFile); while(in.hasNextLine()) { String line = in.nextLine(); line = line.toUpperCase(); for (int k = 0; k < line.length(); k++) { char c = line.charAt(k); if ('A' <= c && c <= 'Z') { frequency[c - 'A']++; totalLetters++; } } } in.close(); } catch (IOException e1) { System.out.println(e1.getMessage()); } // Display results in output text area output.append("Total number of letters: " + totalLetters + " in " + inFile.getAbsoluteFile() + ". "); output.append("Letter: Occurences: Precentage: "); for (int k = 0; k < frequency.length; k++) { char c = (char)(k + 'A'); // maps 0-25 to 'A'-'Z' double percent = 100.0*(frequency[k] / (double) totalLetters); output.append(c + " " + frequency[k] + " " + percent + " "); } } /** * Clear the output display area, status field, and reset title */ public void clearOutput() { output.setText(""); setTitle("Letter Frequencies"); } /** * Save contents of output display area in a file */ public void saveOutput() { chooser.showSaveDialog(null); File outFile = chooser.getSelectedFile(); PrintWriter out = null; try { out = new PrintWriter(outFile); out.print(output.getText()); out.close(); } catch (IOException e) { } } } //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

Explanation / Answer

Repost the question using advanced editor.

unable to read

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote