Advertisement
Guest User

Untitled

a guest
Jul 4th, 2019
272
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.32 KB | None | 0 0
  1.  
  2. public class RatNum {
  3.  
  4. private int num;
  5. private int den;
  6.  
  7.  
  8. public RatNum() {
  9. this.num = 0; //numerator
  10. this.den = 1; //denominator
  11. }
  12.  
  13. public RatNum(int a) {
  14. // a denotes the numerator
  15. this.num = a;
  16. this.den = 1;
  17. }
  18.  
  19. public RatNum(int a, int b) {
  20. if (b==0) {
  21. throw new NumberFormatException("Denominator = 0");
  22. }
  23. this.num = a;
  24. this.den = b;
  25. if (gcd(a,b) == 1) {
  26. int RN = a/b;
  27. } else {
  28. int gcdN = (a)/(gcd(a,b)); // gcdN = greatest common divisor, Numerator
  29. int gcdD = (b)/(gcd(a,b)); // gcdN = greatest common divisor, Denominator
  30. int RN = (gcdN)/(gcdD);
  31. }
  32. }
  33.  
  34. public RatNum(RatNum r) { // %%%%%%%%%%%%%%%%%%%% SEEEEEEEEEEEEEEEEE HÄRRRRRRRRRRRRRRRRRR %%%%%%%%%%%%%%
  35. this.num = r.num;
  36. this.den = r.den;
  37.  
  38. }
  39.  
  40. public int getNumerator() {
  41. return this.num;
  42. }
  43.  
  44. public int getDenominator() {
  45. return this.den;
  46. }
  47.  
  48. public static int gcd(int a,int b) {
  49. if (a == 0 || b == 0) {
  50. throw new IllegalArgumentException("Please, enter a non-zero integer.");
  51. }
  52. if(a < 1) {
  53. a = a * (-1);
  54. }
  55. if(b < 0) {
  56. b = b * (-1);
  57. }
  58. int r = a % b;
  59. if (r==0) {
  60. return b;
  61. } else {
  62. a=b;
  63. b=r;
  64. return gcd(a,b);
  65. }
  66. }
  67.  
  68. public static void main(String[] args) {
  69.  
  70. System.out.println(gcd(4,8));
  71.  
  72. }
  73.  
  74.  
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement