compare To public int compareTo(Apple rhsApple) Specified by: compare To in interface Comparable compare To1 publ
-
- Site Admin
- Posts: 899603
- Joined: Mon Aug 02, 2021 8:13 am
compare To public int compareTo(Apple rhsApple) Specified by: compare To in interface Comparable compare To1 publ
import ... public class Apple implements Comparable<Apple> { public int size; public String type; public AppleColor color; public Apple (int size, String type, AppleColor color){ this.size = size; H public int getSize() { return size; } public String getType() { return type; } public AppleColor getColor() { return color; } this.type = type; this.color = color; public int compareTo (Apple rhsApple) { return 1; } /** * Place your code for comparing Apples by size here. * @param rhs Apple Apple object that is on the right-hand side * @return int result of comparison public int compare Tol (Apple rhsApple) { return 1; } /** * Place your code for comparing Apples by type here. * @param rhs Apple Apple object that is on the right-hand side * @return int result of comparison */ public int compare To2 (Apple rhsApple) { return 1; } /** * Place your code for comparing Apples by color here. * @param rhs Apple Apple object that is on the right-hand side * @return int result of comparison */ public int compare To3 (Apple rhsApple) { return 1; } public String toString() { return getColor() + " " + getType() + public static void main(String[] args) { ArrayList<Apple> apples = new ArrayList<>(); apples.add(new Apple( size: 5, type: "Granny Smith", AppleColor.GREEN)); apples.add(new Apple( size: 2, type: "Golden Delicious", AppleColor.YELLOW)); apples.add(new Apple( size: 1, type: "Red Delicious", AppleColor.RED)); apples.add(new Apple( size: 4, type: "Honeycrisp", AppleColor.REDYELLOW)); apples.add(new Apple( size: 3, type: "Gala", AppleColor.REDYELLOW)); apples.add(new Apple( size: 2, type: "Braeburn", AppleColor.REDYELLOW)); } System.out.println("Original List: "); System.out.println(apples.toString()); System.out.println("Sorted List: "); Collections.sort (apples); System.out.println(apples); apple of size + getSize(); }
Constructor Details AppleColor private AppleColor() Method Details values public static AppleColor[] values() Returns an array containing the constants of this enum class, in the order they are declared. Returns: an array containing the constants of this enum class, in the order they are declared valueOf public static AppleColor valueOf (String name) Returns the enum constant of this class with the specified name. The string must match exactly an identifier used to declare an enum constant in this class. (Extraneous whitespace characters are not permitted.) Parameters: name - the name of the enum constant to be returned. Returns: the enum constant with the specified name Throws: IllegalArgumentException" - if this enum class has no constant with the specified name NullPointerException if the argument is null
public enum AppleColor { } RED, YELLOW, GREEN, REDYELLOW