Advertisement
Guest User

Untitled

a guest
Jan 16th, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.91 KB | None | 0 0
  1. import java.util.*;
  2. public class example{
  3. public static int number1;
  4. public static int number2;
  5. public static int wholenum1;
  6. public static int wholenum2;
  7. public static int division1;
  8. public static int division2;
  9. public static int newn; //New;;;;;;;;;;
  10. public static int newd; //New;;;;;;;;;;
  11. public static void main(String[] args) { //Main method
  12. //Scanner scan = new Scanner(System.in);
  13. System.out.println("Welcome to FracCalc!");
  14. System.out.println("This version of Frat Calc supports (+, -, * and /)");
  15. System.out.println("Type quit to quit Frac Calc, and type test to test Frac Calc");
  16. System.out.println("Bye. Come again!");
  17. }// Ends main
  18.  
  19. public static String produceAnswer2(String input) { //Method2
  20. boolean multipleoperations = false;
  21. String[] x = input.split(" "); //Splits the string apart
  22. String fraction1 = x[0];
  23. String operator = x[1];
  24. String fraction2 = x[2];
  25. String operand1 = parseOperands(fraction1); //Calls the ParseOperands method
  26. String operand2 = parseOperands(fraction2); //Calls the ParseOperands method
  27. String[] operand3 = operand1.split(" ");
  28. int wholenum1 = Integer.parseInt(operand3[1]);
  29. int numerator1 = Integer.parseInt(operand3[3]);
  30. int denominator1 = Integer.parseInt(operand3[5]);
  31. String[] finaloperand2 = operand2.split(" ");
  32. int wholenum2 = Integer.parseInt(finaloperand2[1]);
  33. int numerator2 = Integer.parseInt(finaloperand2[3]);
  34. int denominator2 = Integer.parseInt(finaloperand2[5]);
  35. String resultingnumbers = ("");
  36. int result = 0;
  37. int numeratoranswer = 0;
  38. int denominatoranswer = 0;
  39. }
  40.  
  41. public static String Addition(String input) {
  42. if (operator.equals("+")) { //This if statment occurs when the user uses addition
  43. result = wholenum1 + wholenum2;
  44. numeratoranswer = (numerator1 * denominator2 + numerator2 * denominator1);
  45. denominatoranswer = (denominator1 * denominator2);
  46. numeratoranswer = result * denominatoranswer + numeratoranswer;
  47. result = 0;
  48. resultingnumbers = result + ("_") + numeratoranswer + ("/") + denominatoranswer;
  49. }
  50.  
  51. if (operator.equals("-")) { //This if statment occurs when the user uses subtraction
  52. int num = 0;
  53. int num2 = 0;
  54. if (wholenum1 < 0) { //If statements below determine the numerators values
  55. num = wholenum1 * denominator1 - numerator1;
  56. }
  57. if (wholenum1 > 0) {
  58. num = wholenum1 * denominator1 + numerator1;
  59. }
  60. if (wholenum2 > 0) {
  61. num2 = wholenum2 * denominator2 + numerator2;
  62. }
  63. if (wholenum2 < 0) {
  64. num2 = wholenum2 * denominator2 - numerator2;
  65. }
  66. if (wholenum1 == 0) {
  67. num = numerator1;
  68. }
  69. if (wholenum2 == 0) {
  70. num2 = numerator2;
  71. }
  72. num *= denominator2;
  73. num2 *= denominator1;
  74. numeratoranswer = num - num2;
  75. denominatoranswer = denominator1 * denominator2;
  76. result = 0;
  77. resultingnumbers = result + ("_") + numeratoranswer + ("/") + denominatoranswer;
  78. }
  79. if (operator.equals("*")) { //This if statment occurs when the user uses multiplication
  80. numeratoranswer = (wholenum1 * denominator1 + numerator1) * (wholenum2 * denominator2 + numerator2);
  81. denominatoranswer = (denominator1 * denominator2);
  82. result = 0;
  83. resultingnumbers = result + ("_") + numeratoranswer + ("/") + denominatoranswer;
  84. }
  85. if (operator.equals("/")) { //This if statment occurs when the user uses division
  86. numerator1 = (wholenum1 * denominator1 + numerator1);
  87. numerator2 = (wholenum2 * denominator2 + numerator2);
  88. int temporarydenominator2 = denominator2;
  89. denominator2 = numerator2;
  90. numerator2 = temporarydenominator2;
  91. numeratoranswer = numerator1 * numerator2;
  92. denominatoranswer = denominator1 * denominator2;
  93. result = 0;
  94. resultingnumbers = result + ("_") + numeratoranswer + ("/") + denominatoranswer;
  95. }
  96.  
  97. result = numeratoranswer / denominatoranswer;
  98. numeratoranswer = numeratoranswer % denominatoranswer;
  99. resultingnumbers = result + ("_") + (Math.abs(numeratoranswer)) + ("/") + (Math.abs(denominatoranswer));
  100. if (result == 0 && numeratoranswer == 0 && denominatoranswer == 1) {
  101. resultingnumbers = ("0");
  102. return resultingnumbers;
  103. }
  104. if (numeratoranswer == 0 && denominatoranswer == 1) {
  105. resultingnumbers = ("") + result;
  106. return resultingnumbers;
  107. }
  108. if (result == 0) {
  109. resultingnumbers = ("") + numeratoranswer + ("/") + denominatoranswer;
  110. return resultingnumbers;
  111. }
  112. return resultingnumbers;
  113. }
  114.  
  115.  
  116. public static String parseOperands(String operand2) { //Method 3
  117. boolean underscore = false;
  118. boolean fraction = false;
  119. boolean dividezero = false;
  120. String wholenum = ("");
  121. String fractionunparsed = ("");
  122. String numerator = ("");
  123. String denominator = ("");
  124.  
  125. for (int j = 0; j < operand2.length(); j++) {
  126. if (operand2.charAt(j) == '_') { //If statement checks characters in String for an underscore
  127. underscore = true;
  128. }
  129. if (operand2.charAt(j) == '/') { //If statement checks characters in String for a dash
  130. fraction = true;
  131. }
  132. }
  133. if (underscore == true) { //If there's a underscore this will split the string into a fraction and a whole
  134. String[] components = operand2.split("_");
  135. wholenum = components[0];
  136. fractionunparsed = components[1];
  137. }
  138. else {
  139. if (fraction == true) { //If there's no underscore but there's a fraction this will be used
  140. wholenum = "0";
  141. fractionunparsed = operand2;
  142. }
  143. else { //If there's no underscore and no fraction this will be used
  144. wholenum = operand2;
  145. fractionunparsed = ("");
  146. }
  147. }
  148. if (fraction == true) { //If there's no fraction this will take the fraction and split it into the numerator and denominator of the fraction
  149. String[] fractions = fractionunparsed.split("/");
  150. numerator = fractions[0];
  151. denominator = fractions[1];
  152. }
  153. else { //If there's no fraction both the numerator and denominator are set to values of 0 and 1
  154. numerator = "0";
  155. denominator = "1";
  156. }
  157. String operand2parsed = "wholenumber: " + wholenum + " numerator: " + numerator + " denominator: " + denominator;
  158. if (denominator.equals("0")) {
  159. dividezero = true;
  160. return "dividebyzero";
  161. }
  162. return operand2parsed; //Returns operand2parsed
  163. }
  164. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement