Advertisement
Guest User

thingoxcccc

a guest
Nov 17th, 2019
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.77 KB | None | 0 0
  1. // Lab09bvst.java
  2. // The Rational Class Program II
  3. // This is the student, starting version of the Lab09b assignment.
  4.  
  5.  
  6. import java.util.Scanner;
  7.  
  8.  
  9. public class RationalLabC
  10. {
  11. private static int num1, den1; // numerator and denominator of the 1st rational number
  12. private static int num2, den2; // numerator and denominator of the 2nd rational number
  13.  
  14. public static void main (String args[])
  15. {
  16. enterData();
  17.  
  18. Rational r1 = new Rational(num1,den1);
  19. Rational r2 = new Rational(num2,den2);
  20. Rational r3 = new Rational();
  21.  
  22.  
  23. r3.multiply(r1,r2);
  24. System.out.println("\n\n" + r1.getOriginal() + " * " + r2.getOriginal() + " = " + r3.getReduced());
  25. r3.divide(r1,r2);
  26. System.out.println("\n" + r1.getOriginal() + " / " + r2.getOriginal() + " = " + r3.getReduced());
  27.  
  28. // 100 Point Version Only
  29. // r3.add(r1,r2);
  30. // System.out.println("\n" + r1.getOriginal() + " + " + r2.getOriginal() + " = " + r3.getReduced());
  31. // r3.subtract(r1,r2);
  32. // System.out.println("\n" + r1.getOriginal() + " - " + r2.getOriginal() + " = " + r3.getReduced());
  33. System.out.println();
  34. }
  35.  
  36. public static void enterData()
  37. {
  38. Scanner input = new Scanner(System.in);
  39. System.out.print("\nEnter the 1st numerator ----> ");
  40. num1 = input.nextInt();
  41. System.out.print("\nEnter the 1st denominator --> ");
  42. den1 = input.nextInt();
  43. System.out.print("\nEnter the 2nd numerator ----> ");
  44. num2 = input.nextInt();
  45. System.out.print("\nEnter the 2nd denominator --> ");
  46. den2 = input.nextInt();
  47. }
  48. }
  49.  
  50.  
  51. class Rational
  52. {
  53. private int firstNum; // entered numerator
  54. private int firstDen; // entered denominator
  55. private int reducedNum; // reduced numerator
  56. private int reducedDen; // reduced denominator
  57. private int gcf;
  58. public Rational()
  59. {
  60. firstNum=0;
  61. firstDen=0;
  62. }
  63. public Rational(int x,int y)
  64. {
  65. firstNum=x;
  66. firstDen=y;
  67. }
  68. public void multiply(Rational x, Rational y)
  69. {
  70. firstNum = x.firstNum*y.firstNum;
  71. firstDen = x.firstDen*y.firstDen;
  72. reduce();
  73. }
  74. public void divide(Rational x, Rational y)
  75. {
  76. firstNum = x.firstNum*y.firstDen;
  77. firstDen = x.firstDen*y.firstNum;
  78. }
  79. public String getOriginal()
  80. {
  81. return String.valueOf(firstNum) + "/" + String.valueOf(firstDen);
  82. }
  83. private void reduce()
  84. {
  85. getGCF(firstNum, firstDen);
  86. firstNum/=gcf;
  87. firstDen/=gcf;
  88. }
  89.  
  90. public String getReduced()
  91. {
  92. getGCF(firstNum,firstDen);
  93. return String.valueOf(firstNum/gcf)+"/"+String.valueOf(firstDen/gcf);
  94. }
  95.  
  96. public void getGCF(int n1,int n2)
  97. {
  98. int rem = 0;
  99. do
  100. {
  101. rem = n1 % n2;
  102. if (rem == 0)
  103. gcf = n2;
  104. else
  105. {
  106. n1 = n2;
  107. n2 = rem;
  108. }
  109. }
  110. while (rem != 0);
  111. }
  112.  
  113.  
  114. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement