Results 1 to 2 of 2
  1. #1
    boodruns4ever is offline Member
    Join Date
    Apr 2012
    Posts
    1
    Rep Power
    0

    Default Help with an Java exam question

    I have an exam using eclipse tomorrow and this is the only question I'm stuck on and just trying to see if I could get any help. Here's the question:

    ---------------------------------------------------------------------------------------------
    "Read the whole question before beginning. You will need both Comparable and Comparator."

    A Boat has a name, integer of masts, and double weight. The List fleet is an ArrayList of Boat objects. Boats are usually sorted by name, but in this case you want to be able to do two or more orderings, by number of masts, and by weight.

    A) Write the Boat class so that it can be sorted easily by name.

    B) Write two or more classes that will allow you to perform the other two sorts. (Hint: the code to sort by number of masts can be much shorter than the weight code.)

    C) Show the two lines of code in main() that will sort fleet by name, then by weight.

    ----------------------------------------------------------------------------------------

    All I have is:
    import java.util.ArrayList;
    import java.util.Collections;
    import java.util.Comparator;


    public class Boat implements Comparator<Integer> {
    private String name;
    private int masts;
    private double weight;

    public Boat(String name, int masts, double weight){
    this.name = name;
    this.masts = masts;
    this.weight = weight;
    }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Help with an Java exam question

    Boat should implement Comparable<Boat> not Comparator<Integer>. Then try to give it the only method that Comparable requires it to have, compareTo(...). There are many examples of this to be found here and elsewhere.

Similar Threads

  1. Replies: 0
    Last Post: 04-06-2012, 08:24 PM
  2. Replies: 0
    Last Post: 03-26-2012, 01:11 PM
  3. Java exam question help
    By eoins2345 in forum New To Java
    Replies: 7
    Last Post: 08-20-2011, 07:04 PM
  4. Exam prep Question
    By tonto2010 in forum New To Java
    Replies: 4
    Last Post: 05-10-2011, 01:11 AM
  5. Java 6 Exam
    By varsh in forum Advanced Java
    Replies: 3
    Last Post: 04-23-2010, 06:14 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •