Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class FracCalc
- {
- static int wholeNumber1;
- static int numerator1;
- static int denominator1 = 1;
- static int wholeNumber2;
- static int numerator2;
- static int denominator2 = 1;
- public static void main(String[] args)
- {
- Scanner console = new Scanner(System.in);
- boolean go = true;
- do
- {
- System.out.print("Enter an equation: ");
- String operand1 = console.next();
- if (operand1.equalsIgnoreCase("quit"))
- {
- go = false;
- }
- String operator = console.next();
- String operand2 = console.next();
- parseOperand1(operand1);
- parseOperand2(operand2);
- int newNumerator1 = convertImproper(wholeNumber1, numerator1, denominator1, operand1);
- int newNumerator2 = convertImproper(wholeNumber2, numerator2, denominator2, operand2);
- System.out.print("The equation is: " + newNumerator1 + "/" + denominator1);
- System.out.print(" " + operator + " ");
- System.out.println(newNumerator2 + "/" + denominator2);
- }
- while(go == true);
- System.out.println("Thanks for using FracCalc! Have a wonderful day!");
- console.close();
- }
- public static void parseOperand1(String number)
- {
- if(number.indexOf('_') == -1)
- {
- wholeNumber1 = 0;
- }
- else
- {
- wholeNumber1 = Integer.parseInt(number.substring(0,number.indexOf('_')));
- }
- numerator1 = Integer.parseInt(number.substring(number.indexOf('_') +1, number.indexOf('/')));
- denominator1 = Integer.parseInt(number.substring(number.indexOf('/') +1));
- }
- public static void parseOperand2(String number)
- {
- if(number.indexOf('_') == -1)
- {
- wholeNumber2 = 0;
- }
- else
- {
- wholeNumber2 = Integer.parseInt(number.substring(0,number.indexOf('_')));
- }
- numerator2 = Integer.parseInt(number.substring(number.indexOf('_') +1, number.indexOf('/')));
- denominator2 = Integer.parseInt(number.substring(number.indexOf('/') +1));
- }
- public static int convertImproper(int wholeNumber, int numerator, int denominator, String number)
- {
- if(number.indexOf('_') == -1)
- {
- return numerator;
- }
- else
- {
- return wholeNumber * denominator + numerator;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement