Note that there are some explanatory texts on larger screens.

plurals
  1. POIssues utilizing the comparable interface
    primarykey
    data
    text
    <p>I am trying to utilize the comparable interface to sort an array. The method compareTo() is written so that diamonds are ordered first by carot, then by clarity OR color, whichever is better for the particular diamond. Since there are 23 grades of color, but only 11 grades of clarity, regard the first two color grades as equal in grade to the first grade of clarity, the next two color grades equal in grade to the second grade of clarity, and so on. I'm not sure how to compare the color to the clarity since one is a string and the other is a char. Here is what I have so far. </p> <pre><code>public Diamond(String sN, double car, String clar, char col, String cutType) { stockNumber = sN; carot = car; clarity = clar; color = col; cut = cutType; } public int compareTo(Diamond other) { if (getCarot() &lt; other.getCarot()) { return 1; } else if(getCarot() &gt; other.getCarot()) { return -1; } else return 0; } public String toString() { return "{stockNumber :: " +getStock() + " carot :: " +getCarot() + " clarity :: " +getClarity()+ " color :: " +getColor() + " cut :: " +getCut()+"}"; } //gets the stock number of the diamond public String getStock() { return stockNumber; } //gets the carot size of the diamond public double getCarot() { return carot; } //gets the clarity of the diamond public String getClarity() { return clarity; } //gets the color of the diamond public char getColor() { return color; } //gets the cut of the diamond public String getCut() { return cut; } </code></pre> <p>Diamond[] stones = new Diamond[16];</p> <pre><code>stones[0] = new Diamond( "A1023", 1.0, "VS1", 'F', "brilliant"); stones[1] = new Diamond( "A5911", 1.1, "VVS2", 'G', "rose"); stones[2] = new Diamond( "C5427", 1.0, "VS1", 'D', "princess"); stones[3] = new Diamond( "D8307", 1.6, "SI1", 'H', "brilliant"); stones[4] = new Diamond( "B4825", 0.3, "I1", 'D', "rose"); stones[5] = new Diamond( "A1844", 2.1, "VS2", 'D', "lozenge"); stones[6] = new Diamond( "A3747", 3.1, "SI2", 'W', "baguette"); stones[7] = new Diamond( "E6393", 2.3, "VS2", 'I', "brilliant"); stones[8] = new Diamond( "C5619", 2.8, "VVS1", 'E', "pear"); stones[9] = new Diamond( "E8348", 1.4, "VS2", 'G', "brilliant"); stones[10] = new Diamond( "D2381", 1.7, "I3", 'G', "brilliant"); stones[11] = new Diamond( "C9253", 1.3, "VS2", 'H', "baguette"); stones[12] = new Diamond( "G3459", 2.1, "VS2", 'H', "rose"); stones[13] = new Diamond( "B3598", 2.4, "VVS2", 'D', "pear"); stones[14] = new Diamond( "D9836", 2.8, "IF", 'E', "princess"); stones[15] = new Diamond( "E1046", 2.2, "FL", 'E', "rose"); Arrays.sort( stones ); for ( int j=0; j&lt;stones.length; j++ ) System.out.println( stones[j].toString() ); </code></pre> <p>} </p> <p>} </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload