fork download
  1. public class Main {
  2.  
  3. public static class FeetAndInches implements Comparable<FeetAndInches> {
  4. private int feet;
  5. private int inches;
  6.  
  7. public FeetAndInches(String s) {
  8. String[] parts = s.split("\\.");
  9. if (parts.length == 1) {
  10. feet = Integer.parseInt(parts[0]);
  11. inches = 0;
  12. } else if (parts.length == 2) {
  13. feet = Integer.parseInt(parts[0]);
  14. inches = Integer.parseInt(parts[1]);
  15. } else throw new IllegalArgumentException();
  16. }
  17.  
  18. @Override
  19. public int compareTo(FeetAndInches that) {
  20. if (this.feet == that.feet) {
  21. return Integer.compare(this.inches, that.inches);
  22. } else {
  23. return Integer.compare(this.feet, that.feet);
  24. }
  25. }
  26. }
  27.  
  28. public static void main(String[] args) {
  29. FeetAndInches f1 = new FeetAndInches("5");
  30. FeetAndInches f2 = new FeetAndInches("5.10");
  31. System.out.println(f1.compareTo(f2));
  32. }
  33.  
  34. }
Success #stdin #stdout 0.06s 47092KB
stdin
Standard input is empty
stdout
-1