5. Add another menu item that will allow the user to sort the runners by their PRs. Producing this report will require several additional actions: The class CrossCountryRunner will need to implement the interface Comparable. This will in turn require writing a method called compareTo in the class CrossCountryRunner. Check the documentation for the interface Comparable.
6. You will need to sort the array or ArrayList.
a. If you are using an array, sort the array using the static method “sort” in the class Arrays (notice the extra s at the end).
b. If you are using an ArrayList, sort the ArrayList using the static method “sort” in the class Collections (notice the extra s at the end).
7. After sorting the array or ArrayList in the desired order, the user will be able to select the option to display the content. Visit all the entries and add them to the text area as they are visited.
Here is my code:
/**
* @name
* @VER 1.0
*/
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.util.*;
import java.io.*;
public class Project5 extends JFrame implements ActionListener
{
JMenuBar menuBar;
JMenu firstMenu;
JMenuItem loadMenuItem;
JMenuItem sortMenuItem;
JMenuItem exitMenuItem;
JTextArea text;
ArrayList <CrossCountryRunner> al = new ArrayList <CrossCountryRunner>();
public Project5()
{
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(400,500);
setTitle("CS162 Project 5");
menuBar = new JMenuBar();
firstMenu = new JMenu("File");
loadMenuItem = new JMenuItem("Load");
sortMenuItem = new JMenuItem("Sort Runners By PR");
exitMenuItem = new JMenuItem("Exit");
loadMenuItem.addActionListener(this);
exitMenuItem.addActionListener(this);
firstMenu.add(loadMenuItem);
firstMenu.add(sortMenuItem);
firstMenu.add(exitMenuItem);
menuBar.add(firstMenu);
setJMenuBar(menuBar);
text = new JTextArea(5,20);
text.setText("First\tLast\tPR\n-----\t-----\t--\n");
JScrollPane scrollPane = new JScrollPane(text);
add(BorderLayout.CENTER, scrollPane);
pack();
setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
Object source = e.getSource();
if (source == exitMenuItem)
{
System.exit(0);
}
if (source == loadMenuItem)
{
loadFile();
displayContent();
}
if (source == sortMenuItem)
{
sortPRs();
}
}
public void loadFile()
{
JFileChooser chooser = new JFileChooser();
int status = chooser.showOpenDialog(null);
if (status == JFileChooser.APPROVE_OPTION)
{
File file = chooser.getSelectedFile();
readFile(file);
}
else
{
text.setText("No file selected.");
}
}
public void readFile(File file)
{
Scanner scanner = null;;
try
{
scanner = new Scanner(file);
}
catch(IOException ioe)
{
text.setText("There was a problem opening the file.");
}
while(scanner.hasNext())
{
String string = scanner.nextLine();
CrossCountryRunner c1 = new CrossCountryRunner(string);
al.add(c1);
}
}
public void displayContent()
{
for (int i = 0;i < al.size();i++){
text.append(al.get(i) + "\n");
}
}
public void sortPRs()
{
}
public static void main(String args[])
{
new Project5();
}
}
/**
* CrossCountryRunner
*
* @name
* @VER: 1.0, Winter 2011
*/
public class CrossCountryRunner implements Comparable <CrossCountryRunner>
{
private String firstName;
private String lastName;
private int pr;
public CrossCountryRunner(String s)
{
String parts[] = s.split(",");
firstName = parts[0];
lastName = parts[1];
pr = Integer.parseInt(parts[2]);
System.out.println(firstName + " " + lastName + " " + pr);
}
public int getPR()
{
return pr;
}
public String getLastName()
{
return lastName;
}
/**
* compareTo
*
* @return -1 if this.pr < otherRunner.pr
* 0 if this.pr == otherRunner.pr
* 1 if this.pr > otherRunner.pr
*/
public int compareTo(CrossCountryRunner otherRunner)
{
if (this.pr < otherRunner.pr)
return -1;
else if (this.pr == otherRunner.pr)
return 0;
else
return 1;
}
public String toString()
{
return firstName+"\t"+lastName+"\t"+pr;
}
}

New Topic/Question
Reply



MultiQuote









|