Advertisement
Guest User

Untitled

a guest
Nov 20th, 2017
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.44 KB | None | 0 0
  1. package fracCalc;
  2. import java.util.Scanner;
  3. public class FracCalc {
  4.  
  5. public static void main(String[] args)
  6. {
  7. // TODO: Read the input from the user and call produceAnswer with an equation
  8. Scanner scanner = new Scanner(System.in);
  9. String input = scanner.nextLine();
  10. produceAnswer(input);
  11.  
  12. }
  13.  
  14. // ** IMPORTANT ** DO NOT DELETE THIS FUNCTION. This function will be used to test your code
  15. // This function takes a String 'input' and produces the result
  16. //
  17. // input is a fraction string that needs to be evaluated. For your program, this will be the user input.
  18. // e.g. input ==> "1/2 + 3/4"
  19. //
  20. // The function should return the result of the fraction after it has been calculated
  21. // e.g. return ==> "1_1/4"
  22. public static String produceAnswer(String input)
  23. {
  24. int num1,num2,den1,den2,whole1,whole2;
  25. String completed = "";
  26. String[] splitup = input.split(" ");
  27. String first = splitup[0], second = splitup[1], third = splitup[2];
  28. System.out.println(third);
  29. if(first.indexOf("/") != -1 && third.indexOf("/") != -1) { // if both have fractions in them
  30. String[] firstFrac = splitup[0].split("/");
  31. String[] secondFrac = splitup[2].split("/");
  32. den1 = Integer.parseInt(firstFrac[1]);
  33. den2 = Integer.parseInt(secondFrac[1]);
  34. if(firstFrac[0].indexOf("_") != -1) {
  35. String[] wholeNumber1 = firstFrac[0].split("_");
  36. num1 = Integer.parseInt(wholeNumber1[1]);
  37. whole1 = Integer.parseInt(wholeNumber1[0]);
  38. }
  39. else {
  40. num1 = Integer.parseInt(firstFrac[0]);
  41. whole1 = 0;
  42. }
  43. if(secondFrac[0].indexOf("_") != -1) {
  44. String[] wholeNumberAndNumerator = secondFrac[0].split("_");
  45. num2 = Integer.parseInt(wholeNumberAndNumerator[1]);
  46. whole2 = Integer.parseInt(wholeNumberAndNumerator[0]);
  47. }
  48. else {
  49. num2 = Integer.parseInt(secondFrac[0]);
  50. whole2 = 0;
  51. }
  52. }
  53. else if(first.indexOf("/") == -1 && third.indexOf("/") != -1) {// if the second fraction has a fraction in it but the first doesnt
  54. whole1 = Integer.parseInt(first);
  55. num1 = 0;
  56. den1 = 1;
  57. String[] secondFrac = splitup[2].split("/");
  58. den2 = Integer.parseInt(secondFrac[1]);
  59. if(secondFrac[0].indexOf("_") != -1) {
  60. String[] wholeNumber1 = secondFrac[0].split("_");
  61. num1 = Integer.parseInt(wholeNumber1[1]);
  62. whole1 = Integer.parseInt(wholeNumber1[0]);
  63. }
  64. else {
  65. num1 = Integer.parseInt(secondFrac[0]);
  66. whole1 = 0;
  67.  
  68. }
  69. }else if(first.indexOf("/") != -1 && third.indexOf("/") == -1) { // if the first fraction has a fraction in it but the second doesnt
  70. whole2 = Integer.parseInt(third);
  71. num2 = 0;
  72. den2 = 1;
  73. String[] firstFrac = splitup[0].split("/");
  74. den2 = Integer.parseInt(firstFrac[1]);
  75. if(firstFrac[0].indexOf("_") != -1) {
  76. String[] wholeNumber1 = firstFrac[0].split("_");
  77. num1 = Integer.parseInt(wholeNumber1[1]);
  78. whole1 = Integer.parseInt(wholeNumber1[0]);
  79. }
  80. else {
  81. num1 = Integer.parseInt(firstFrac[0]);
  82. whole1 = 0;
  83.  
  84. }
  85. }
  86. System.out.println("whole:" + whole2 + " numerator:" + num1 + " denominator:" + den1);
  87. return ("whole:" + whole2 + " numerator:" + num2 + " denominator:" + den2);
  88. }
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement