Advertisement
Guest User

Untitled

a guest
Feb 21st, 2019
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.85 KB | None | 0 0
  1. Object comparison = this.inches.compareTo(obj.inches);
  2.  
  3. import java.io.*;
  4. import java.util.*;
  5. import java.lang.Integer;
  6. import java.lang.reflect.Array;
  7.  
  8. public class Distance implements Comparable<Distance> {
  9.  
  10. private static final String HashCodeUtil = null;
  11. private int feet;
  12. private int inches;
  13. private final int DEFAULT_FT = 1;
  14. private final int DEFAULT_IN = 1;
  15.  
  16. public Distance(){
  17. feet = DEFAULT_FT;
  18. inches = DEFAULT_IN;
  19. }
  20. public Distance(int ft, int in){
  21. feet = ft;
  22. inches = in;
  23. }
  24.  
  25. public void setFeet(int ft){
  26. try {
  27. if(ft<0){
  28. throw new CustomException("Distance is not negative");
  29.  
  30. }
  31.  
  32. }
  33. catch(CustomException c){
  34. System.err.println(c);
  35. feet =ft;
  36. }
  37. }
  38.  
  39. public int getFeet(){
  40. return feet;
  41. }
  42.  
  43. public void setInches(int in){
  44.  
  45. try
  46. {
  47. if (in<0)
  48. throw new CustomException("Distance is not negative");
  49. //inches = in;
  50. }
  51. catch(CustomException c)
  52. {
  53. System.err.println(c);
  54. inches = in;
  55. }
  56.  
  57. }
  58.  
  59. public int getInches(){
  60. return inches;
  61. }
  62.  
  63.  
  64. public String toString (){
  65. return "<" + feet + ":" + inches + ">";
  66.  
  67. }
  68.  
  69. public Distance add(Distance m){
  70. Distance n = new Distance();
  71. n.inches = this.inches + m.inches;
  72. n.feet = this.feet + m.feet;
  73.  
  74. while(n.inches>12){
  75. n.inches = n.inches - 12;
  76. n.feet++;
  77. }
  78.  
  79. return n;
  80. }
  81.  
  82. public Distance subtract(Distance f){
  83. Distance m = new Distance();
  84. m.inches = this.inches - f.inches;
  85. m.feet = this.feet - f.feet;
  86. while(m.inches<0){
  87. m.inches = m.inches - 12;
  88. feet--;
  89.  
  90. }
  91.  
  92. return m;
  93. }
  94.  
  95.  
  96. @Override
  97. public int compareTo(Distance obj) {
  98. // TODO Auto-generated method stub
  99.  
  100. final int BEFORE = -1;
  101. final int EQUAL = 0;
  102. final int AFTER = 1;
  103.  
  104. if (this == obj) return EQUAL;
  105.  
  106. if(this.DEFAULT_IN < obj.DEFAULT_FT) return BEFORE;
  107. if(this.DEFAULT_IN > obj.DEFAULT_FT) return AFTER;
  108.  
  109. Object comparison = this.inches.compareTo(obj.inches);
  110. if (this.inches == obj.inches) return compareTo(null);
  111.  
  112. assert this.equals(obj) : "compareTo inconsistent with equals";
  113.  
  114. return EQUAL;
  115.  
  116. }
  117.  
  118. @Override public boolean equals( Object obj){
  119. if (obj != null) return false;
  120. if (!(obj intanceof Distance)) return false;
  121.  
  122. Distance that = (Distance)obj;
  123.  
  124. ( this.feet == that.feet &&
  125. this.inches == that.inches);
  126. return true;
  127. else
  128. return false;
  129. }
  130.  
  131.  
  132. @Override public int hashCode(int, int) {
  133. int result = HashCodeUtil.inches;
  134. result = HashCodeUtil.hash(result, inches );
  135. result = HashCodeUtil.hash(result, feet);
  136. ruturn result;
  137. }
  138.  
  139. @Override
  140. public int compareTo(Distance obj) {
  141. ....
  142. if (this == obj) return EQUAL; <--- This
  143. ...
  144. }
  145.  
  146. Object comparison = this.inches.compareTo(obj.inches);
  147.  
  148. if (this.inches == obj.inches) return compareTo(null);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement