11 Replies - 343 Views - Last Post: 23 April 2011 - 07:17 PM Rate Topic: -----

#1 christry  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 23-April 11

Comparable Help

Posted 23 April 2011 - 03:20 AM

Heres what I need to do:

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;
    }

    }



Is This A Good Question/Topic? 0
  • +

Replies To: Comparable Help

#2 darek9576  Icon User is offline

  • D.I.C Lover

Reputation: 195
  • View blog
  • Posts: 1,620
  • Joined: 13-March 10

Re: Comparable Help

Posted 23 April 2011 - 03:34 AM

Awesome code. And?
Was This Post Helpful? 0
  • +
  • -

#3 christry  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 23-April 11

Re: Comparable Help

Posted 23 April 2011 - 03:36 AM

View Postdarek9576, on 23 April 2011 - 03:34 AM, said:

Awesome code. And?


Thanks.. but what do you mean by "And?"

Above the code I posted what I needed help doing =\
Was This Post Helpful? 0
  • +
  • -

#4 darek9576  Icon User is offline

  • D.I.C Lover

Reputation: 195
  • View blog
  • Posts: 1,620
  • Joined: 13-March 10

Re: Comparable Help

Posted 23 April 2011 - 03:41 AM

First: it's called sarcasm. Second: what's your problem?
Was This Post Helpful? 0
  • +
  • -

#5 christry  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 23-April 11

Re: Comparable Help

Posted 23 April 2011 - 03:45 AM

View Postdarek9576, on 23 April 2011 - 03:41 AM, said:

First: it's called sarcasm. Second: what's your problem?


My problem? You're the one being very rude.
Was This Post Helpful? 0
  • +
  • -

#6 cfoley  Icon User is offline

  • Cabbage
  • member icon

Reputation: 1508
  • View blog
  • Posts: 3,219
  • Joined: 11-December 07

Re: Comparable Help

Posted 23 April 2011 - 05:15 AM

What a welcome to the forum. Poor show. :(

Your compareTo() method looks good to me. The Collections class has some methods for sorting Lists. Try implementing your sortPR method with that. Have a shot and get back to us if you have problems.
Was This Post Helpful? 0
  • +
  • -

#7 CasiOo  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 996
  • View blog
  • Posts: 2,212
  • Joined: 05-April 11

Re: Comparable Help

Posted 23 April 2011 - 06:14 AM

I don't see the problem. All you need is to use the Collections.sort( al );
Was This Post Helpful? 0
  • +
  • -

#8 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2696
  • View blog
  • Posts: 10,556
  • Joined: 15-July 08

Re: Comparable Help

Posted 23 April 2011 - 07:51 AM

View Postchristry, on 23 April 2011 - 06:45 AM, said:

View Postdarek9576, on 23 April 2011 - 03:41 AM, said:

First: it's called sarcasm. Second: what's your problem?


My problem? You're the one being very rude.


I think he meant what's wrong with your code.
However, next time you post, please follow the instructions in the big box above the posting screen:
  • :code:
  • Include details about errors and what your question is
  • Use proper English and spelling
  • Tell us what you've tried to solve your problem


You need to do 2 and 4. Thanks!
Was This Post Helpful? 0
  • +
  • -

#9 christry  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 23-April 11

Re: Comparable Help

Posted 23 April 2011 - 07:57 AM

View PostCasiOo, on 23 April 2011 - 06:14 AM, said:

I don't see the problem. All you need is to use the Collections.sort( al );


I tried doing collections.sort(al); and for some reason absolutely nothing changes. The code will compile fine with no errors but when I click on sort by PRs in the dropdown menu nothing happens
Was This Post Helpful? 0
  • +
  • -

#10 christry  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 23-April 11

Re: Comparable Help

Posted 23 April 2011 - 08:03 AM

Here is what I am currently trying:

        if (source == sortMenuItem)
        {
            sortPRs();
        }



and

    public void sortPRs()
    {
        Collections.sort(al);
            displayContent();
    }


Was This Post Helpful? 0
  • +
  • -

#11 christry  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 23-April 11

Re: Comparable Help

Posted 23 April 2011 - 08:15 AM

Sorry for triple post!

I actually got it working now. I did not have
sortMenuItem.addActionListener(this);


but now whenever I get it to sort by the PR, it just adds onto the list with the newly sorted ones. So I have a list of unsorted ones followed by sorted ones.. how can I fix this?

This post has been edited by christry: 23 April 2011 - 08:16 AM

Was This Post Helpful? 0
  • +
  • -

#12 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8032
  • View blog
  • Posts: 31,202
  • Joined: 06-March 08

Re: Comparable Help

Posted 23 April 2011 - 07:17 PM

in displayContent() do
text.setText(null);
before the for() loop appending to it

Happy coding
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1