Results 1 to 2 of 2
Thread: Help with an Java exam question
- 04-20-2012, 03:36 AM #1
Member
- Join Date
- Apr 2012
- Posts
- 1
- Rep Power
- 0
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;
}
-
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
-
Tutorial:Review of Java Design Patterns for the Java Architect Exam
By Java Exam in forum OCMJEAReplies: 0Last Post: 04-06-2012, 08:24 PM -
Tutorial:Review of Common Java Architectures for the Java Architect Exam
By Java Exam in forum OCMJEAReplies: 0Last Post: 03-26-2012, 01:11 PM -
Java exam question help
By eoins2345 in forum New To JavaReplies: 7Last Post: 08-20-2011, 07:04 PM -
Exam prep Question
By tonto2010 in forum New To JavaReplies: 4Last Post: 05-10-2011, 01:11 AM -
Java 6 Exam
By varsh in forum Advanced JavaReplies: 3Last Post: 04-23-2010, 06:14 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks