Advertisement
Guest User

cosa

a guest
Apr 2nd, 2020
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.83 KB | None | 0 0
  1. public class Fraction {
  2.  
  3. int numerator, denominator;
  4.  
  5. public Fraction(int nume, int denom) {
  6. numerator= nume;
  7. denominator= denom;
  8. }
  9.  
  10. @Override
  11. public String toString() {
  12. return "Fraction [numerator=" + numerator + ", denominator=" + denominator + "]";
  13. }
  14.  
  15. private static Fraction simplify(Fraction toSimplify) {
  16. int smallest;
  17. if (toSimplify.numerator < toSimplify.denominator) {
  18. smallest= toSimplify.numerator;
  19. } else
  20. smallest= toSimplify.denominator;
  21. int aux= smallest/2;
  22. boolean check= false;
  23. while (aux > 1 && !check) {
  24. if (toSimplify.numerator% aux == 0 && toSimplify.denominator% aux == 0) {
  25. check= true;
  26. }
  27. aux--;
  28. }
  29. if (check) {
  30. return new Fraction(toSimplify.numerator/(aux + 1), toSimplify.denominator/(aux + 1));
  31. } else {
  32. return toSimplify;
  33. }
  34. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement