1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298
import javax.swing.JOptionPane; public class Rational { public static Rational ZERO = new Rational(0); public static Rational ONE = new Rational(1); private int numerator; // the numerator private int denominator; // the denominator public Rational(int numerator, int denominator) { // dealing with x/0 throwing exception if (denominator == 0) { throw new IllegalArgumentException("Denominator must be > 0"); } this.numerator = numerator; this.denominator = denominator; } // input constructor public Rational() { int num,den; String numStr,denStr; while(true) { try { numStr=JOptionPane.showInputDialog(null,"Input numerator:"); if (numStr==null) System.exit(0); num = Integer.parseInt(numStr); denStr=JOptionPane.showInputDialog(null,"Input denominator:"); if (denStr==null) System.exit(0); den = Integer.parseInt(denStr); if(den == 0) { // dealing with x/0 throwing exception throw new IllegalArgumentException("Denominator must be > 0"); } break; } catch(Exception e) { System.out.println(e); } } numerator = num; denominator = den; } // double constructor public Rational (double d) { int offset=1; while(d*offset!=(int)(d*offset)) { offset*=10; } numerator=(int)(d*offset); denominator=offset; } // string constructor public Rational(String numerator, String denominator) { int num = Integer.parseInt(numerator); int den = Integer.parseInt(denominator); if(den == 0) { // dealing with x/0 throwing exception throw new IllegalArgumentException("Denominator must be > 0"); } this.numerator = num; this.denominator = den; } // adding methods public Rational add(Rational arg) { return new Rational(arg.numerator*denominator+numerator*arg.denominator, denominator*arg.denominator); } public Rational add(int i) { return new Rational(i*denominator+numerator,denominator); } public Rational add(double d) { Rational addedVal = new Rational(d); return new Rational(addedVal.numerator*denominator+numerator*addedVal.denominator,denominator*addedVal.denominator); } // end of adding methods // multiplication methods public Rational mul(Rational arg) { return new Rational(arg.numerator*numerator, arg.denominator*denominator); } public Rational mul(int i) { return new Rational(i*numerator,denominator); } public Rational mul(double d) { Rational mulVal=new Rational(d); return new Rational(mulVal.numerator*numerator, mulVal.denominator*denominator); } // end of multiplications // subtraction methods public Rational sub(Rational arg) { return new Rational(arg.numerator*denominator-numerator*arg.denominator, denominator*arg.denominator); } public Rational sub(int i) { return new Rational(numerator-i*denominator,denominator); } public Rational sub(double d) { Rational subVal=new Rational(d); return new Rational(subVal.numerator*denominator-numerator*subVal.denominator, denominator*subVal.denominator); } // end of subtraction // division methods public Rational div(Rational arg) { if(arg.numerator == 0) { throw new ArithmeticException("Dividing by 0 bad idea ;)"); } int newDen=denominator*arg.numerator; int newNum=numerator*arg.denominator; return new Rational(newNum, newDen); } public Rational div(int i) { if(i == 0) { throw new ArithmeticException("Dividing by 0 bad idea ;)"); } return new Rational(numerator, denominator/i); } public Rational div(double d) { if(d == 0) { throw new ArithmeticException("Dividing by 0 bad idea ;)"); } Rational divVal=new Rational(d); int newDen=denominator*divVal.numerator; int newNum=numerator*divVal.denominator; return new Rational(newNum, newDen); } // end of division // comparison methods public boolean equals(Rational arg) { return arg.numerator*denominator==arg.denominator*numerator; } public boolean lessThan(Rational arg) { if (arg.denominator*numerator<denominator*arg.numerator) return true; else return false; } public boolean greaterThan(Rational arg) { if (arg.denominator*numerator>denominator*arg.numerator) return true; else return false; } // problem with (-2,1), (1,-1) etc... public boolean lessThanOrEqual(Rational arg) { if (arg.denominator*numerator<=denominator*arg.numerator) return true; else return false; } public boolean greaterThanOrEqual(Rational arg) { if (arg.denominator*numerator>=denominator*arg.numerator) return true; else return false; } // end of comparsion public String toString() { Rational red=reduce(); return red.numerator+"\\"+red.denominator; } // Working reduction // I don't know how to implement "void reduce()" private Rational reduce() // { int n = numerator; int gcd = denominator; int tmp; if(n < gcd) { tmp = n; n = gcd; gcd = tmp; } while(n!=0) { tmp = n; n = gcd%n; gcd = tmp; } return new Rational(numerator/gcd,denominator/gcd); } public static void main(String[] args) { Rational a=new Rational(); Rational i=new Rational(-2,1); Rational d=new Rational(1,-1); Rational s=new Rational("25", "75"); System.out.println(i.lessThanOrEqual(d)); /* Printing results JOptionPane.showMessageDialog(null,"DEFINED RATIONAL NUMBERS:\n-------------------------------------------\n" + "User rational number: "+a + "\nRational [1] (integer): "+i + "\nRational [2] (double): "+d + "\nRational [3] (string): "+s + "\n-------------------------------------------\nADDITION:\n-------------------------------------------\n" // Addition +a + " + " +i+ " = " + a.add(i) + "\n" +a + " + " +d+ " = " + a.add(d) + "\n" +a + " + " +s+ " = " + a.add(s) + "\n" +d + " + " +s+ " = " + d.add(s) + "\n" + "-------------------------------------------\nSUBTRACTION:\n-------------------------------------------\n" // Subtraction +i + " - " +a+ " = " + a.sub(i) + "\n" +d + " - " +a+ " = " + a.sub(d) + "\n" +s + " - " +a+ " = " + a.sub(s) + "\n" +d + " - " +s+ " = " + s.sub(d) + "\n" + "-------------------------------------------\nMULTIPLICATION:\n-------------------------------------------\n" // Multiplication +a + " * " +i+ " = " + a.mul(i) + "\n" +a + " * " +d+ " = " + a.mul(d) + "\n" +a + " * " +s+ " = " + a.mul(s) + "\n" +d + " * " +s+ " = " + s.mul(d) + "\n" + "-------------------------------------------\nDIVISION:\n-------------------------------------------\n" // Division +a + " : " +i+ " = " + a.div(i) + "\n" +a + " : " +d+ " = " + a.div(d) + "\n" +a + " : " +s+ " = " + a.div(s) + "\n" +d + " : " +s+ " = " + s.div(d) + "\n" + "-------------------------------------------\nCOMPARSIONS:\n-------------------------------------------\n" // Comparsions +a + " = " +i+ " = " + a.equals(i) + "\n" +a + " < " +d+ " = " + a.lessThan(d) + "\n" +a + " > " +s+ " = " + a.greaterThan(s) + "\n" +a + " <= " +s+ " = " + a.lessThanOrEqual(s) + "\n" +a + " >= " +d+ " = " + a.greaterThanOrEqual(d) + "\n", "Results", JOptionPane.PLAIN_MESSAGE); */ } }
Refactorings
No refactoring yet !
Ants
January 7, 2010, January 07, 2010 08:12, permalink
Huh? lessThanOrEqual() doesn't work, but lessThan() works for the same inputs? They should both succeed or both fail since they are basically doing the same operation. This points to the most basic improvement that you can do to this code: instead of repeating yourself, try to re-use functions.
Anyway the issue is that you should be consistent about where you keep the sign of ration number. Consistently store the sign with the numerator (or the denominator). This will allow your cross multiplication to keep the sign on the appropriate sides of the comparison.
Hi,
I have a problem with one of the comparsions... "lessThanOrEqual" works fine for rational numbers like (3,1), (2,3) etc... But if I try to compare for example (-2,1) and (1,-1) or (2,1) (1,-1) I get wrong answer... I have no idea how to rewrite this piece of code... Please help.