Weight Class with a Comparable Interface

posted by: Mr. Bergquist 14 March 2012 No Comment

You will implement a class to represent weights in pounds and ounces.  Your class will have two fields: pounds and ounces.  Recall that there are 16 ounces in a pound.  Your Weight objects should never keep track of more than 15 ounces, they should convert the value to pounds instead (i.e. 2lb 17oz should become 3lb 1oz).  Weights needs its toString method, ex: 1lb 4oz or 3oz or 7lbs 3oz.  In addition, your class will implement the Comparable interface to define how they are sorted (lighter to heavier).  Write a client that creates several weights, puts them in an ArrayList, sorts them, then print the ArrayList out, similar to the StudentL example from our website.

New Starter Code:
Weight.java (Just update this)
WeightClient.java (Run this when Weight is done)

NOTE: You do NOT need to create the Comparable Interface code, that is built into the java.util library for Collections (if you created it, remove it and its .class).  And your client code should have these lines similar to the  sample StudentLClient.java Client code (with corresponding StudentL.java  Object code) to demonstrate the sorting working:

                System.out.println(students);
		Collections.sort(students);
		System.out.println(students);

Good luck!

1 Star2 Stars3 Stars4 Stars5 Stars (15 votes, average: 4.27 out of 5)
Loading ... Loading ...

Leave a comment

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>