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

I need an expert to help me sort through words in descending order? which is bes

ID: 3659517 • Letter: I

Question

I need an expert to help me sort through words in descending order? which is best bubble sort or arraylist sort? give me an example with my program in java format plz. how to add a second hash table? import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.filechooser.*; import java.util.*; import java.io.*; public class BuildWords extends JFrame{ private JButton chooser, start; private JLabel wordLabel; private JTextField wordField; private JPanel inputPanel; private JTextArea results; private File inFile; private String inputFileName; public static void main () { BuildWords myFrame = new BuildWords(); myFrame.setSize(500,500); myFrame.setTitle("Concordance"); myFrame.createGUI(); myFrame.setVisible(true); } private void createGUI (){ setDefaultCloseOperation(EXIT_ON_CLOSE); Container window = getContentPane(); window.setLayout(new FlowLayout()); inputPanel = new JPanel(); inputPanel.setLayout(new BoxLayout (inputPanel, BoxLayout.X_AXIS)); inputPanel.add(Box.createRigidArea (new Dimension(20,40))); chooser = new JButton("Select File"); chooser.addActionListener(new ChooserAction()); inputPanel.add(chooser); inputPanel.add(Box.createRigidArea (new Dimension(20,40))); wordLabel = new JLabel("Word "); inputPanel.add(wordLabel); wordField = new JTextField(12); inputPanel.add(wordField); inputPanel.add(Box.createRigidArea(new Dimension(20,40))); start = new JButton("Start"); start.addActionListener(new StartAction()); inputPanel.add(start); window.add(inputPanel); results = new JTextArea(25,30); JScrollPane scrollPane = new JScrollPane(results); window.add(scrollPane); } private class ChooserAction implements ActionListener{ public void actionPerformed (ActionEvent e){ JFileChooser myChooser = new JFileChooser(); int returnValue = myChooser.showOpenDialog(Concordance.this); inputFileName = myChooser.getSelectedFile().getName(); inFile = myChooser.getSelectedFile(); } } private class StartAction implements ActionListener{ public void actionPerformed (ActionEvent e){ if (inputFileName != null){ buildConcordance(); }else{ JOptionPane.showMessageDialog(null,"Please select a file first", "Error",JOptionPane.ERROR_MESSAGE); } } } public void BuildWords(){ //sorting the word that occurs ??? //diplay order of words that occurs in descending order } // Add a second hash table that /// will hold large words. }

Explanation / Answer

This is from a post on the bbj-developers list that might be helpful: The easiest way to sort objects is to use a TreeMap. It is similar in it's methods to a Hashtable but provides a sorted capability. You use the .put(key!,object!) method to add objects to the table and the .get(key!) to get them out by key. When you want to retrieve the items in key sequence you can use the .keySet() method of the TreeMap. This gives you a set of keys in the proper sequence. You can then load these into a BBj Vector using the addAll() method of the java List/Collections interfaces, which BBjVectors implement. Here is a code snip that reads a comma delimited file with US Zip (Postal) Codes, stores the information in a TreeMap, then retrieves the list of keys and adds them to a BBjVector. bbj!=bbjapi() zipTree!=new java.util.TreeMap() dim zip$:"zipCode:c(1*=44),city:c(1*=44),state:c(1*=44),county:c(1*=)" zip=unt;open(zip)"zipCode" while 1 read(zip,err=*break)zip$ zipTree!.put(zip.zipCode$,zip$) wend rem create a BBj Vector rem fill vector from the set of keys in the TreeMap zipVector! = bbj!.makeVector() zipVector!.addAll(zipTree!.keySet()) The resulting vector is sorted in "Zip code" sequence. You can also read the data from the file and put it into a vector, then sort the vector. bbj!=bbjapi() zipVector!=bbj!.makeVector() dim zip$:"zipCode:c(1*=44),city:c(1*=44),state:c(1*=44),county:c(1*=)" zip=unt;open(zip)"zipCode" while 1 read(zip,err=*break)zip$ zipVector!.addItem(zip.zipCode$) wend rem use the java.util.Collections.sort(list!) method to sort the vector java.util.Collections.sort(zipVector!) The vector, zipVector! is now sorted by zip code. The nice thing about this is that BBjVectors implement the Java List and Collection interfaces, so all of the methods available in these interfaces are also available for vectors. Therefore we can use the .addAll(list!) method or the sort(list!) method as shown, with BBj Vectors.

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