string manipulation, user input Student approved

Implement a basic calculator that handles fractions.

The program should read in fraction equations and print the results in a continuous loop until the user types "quit"

Input will be either mixed fractions, proper fractions, improper fractions or integers

Input will be separated by spaces – exactly one space between each fraction and operator.

The integer and fraction parts of a mixed fraction will be separated by an underscore.

Negatives are allowed – the negative sign should go immediately before the whole part of the number (with no space in between)

The operators will be +, -, *, and /

The output needs to be in standard mixed fractions, properly reduced (i.e. 1/2 instead of 2/4, 1_1/4 instead of 5/4)

- No numbers in the fraction will exceed the limits of a Java int (between -2,147,483,648 and 2,147,483,647) after they are multiplied – which means the input will be between -32,768 and 32767.
- As you make changes think about how you are going to ensure all different scenarios still work. In other words figure out how you want to test this in advance.

Accept fraction string as input and display corresponding improper fraction.

```
Enter the fraction: 1_1/2
Improper Fraction: 3/2
Goodbye!
```

Breakdown of Points:

- 2 Points: Comments: Header, Methods, Logic
- 1 Points: Proper formatting/Indentation
- 3 Points: Handling Whole number, fractions, Mixed Numbers
- 2 Points: Calculating Correct improper fraction
- 1 Points: Correct input/output formatting
- 1 Points: Breaking into reusable code for future checkpoints

Parsing the fractions: displays mixed and improper fractions and operation entered. Interpret Quit command: exit on this command otherwise continue processing.

```
Enter an expression (or "quit"): 1/4 + 1_1/2
Fraction 1: 1/4 = 1/4
Operator: +
Fraction 2: 1_1/2 = 3/2
Enter an expression (or "quit"): quit
Goodbye!
```

Breakdown of Points:

- 4 Points: Comments, formatting, Indentation, modular code o 3 Points: Handling Whole number, fractions, Mixed Numbers o 2 Points: Calculating Correct improper fraction
- 4 Points: Correct Parsing of Equation String
- 2 Points: Handling Quit
- 3 Points: Correct loop implementation
- 2 Points: Correct input/output formatting

Do one operation (+) fully functional including improper, mixed fractions. Welcome to the Fraction Calculator!

```
Enter an expression (or "quit"): 1_1/2 + 1/4
1_3/4
Enter an expression (or "quit"): 8/4 + 2
4
Enter an expression (or "quit"): quit
Goodbye!
```

Breakdown of Points:

- 3 Points: Comments, formatting, Indentation, modular code
- 2 Points: Handling Whole number, fractions, Mixed Numbers
- 3 Points: Correct loop implementation, Quit
- 2 Points: Correct Parsing of Equation String
- 2 Points: Calculating Correct improper fraction
- 3 Points: Correct Addition implementation
- 3 Points: Correct Result calculation in reduced format
- 2 Points: Correct input/output formatting

Everything should work as described in section 2.

Breakdown of Points:

- 5 Points: Comments, formatting, Indentation, modular code
- 5 Points: Handling Whole number, fractions, Mixed Numbers
- 4 Points: Correct loop implementation, Quit
- 5 Points: Correct Parsing of Equation String
- 10 Points: All operations implementation
- 5 Points: Calculating Correct improper fraction
- 10 Points: Correct Result calculation in reduced format
- 5 Points: Correct input/output formatting
- 6 Points: Handling corner cases (divide by 0, wrong fractions etc)

- 5 points: calculators that can handle more than one operation (e.g. 1 + 1 + 1 - 1/2)
- 5 points: correctly handling the order of operations with more than two inputs (e.g. 1 - 2 * 4 returning -7 instead of -4)
- 5 points: handling bad input gracefully (e.g. 1 + + 1/2 does not cause the program to crash)
- 10 points: an infinite precision calculator. This means your calculator can handle very large numbers and still produce the right results. You may look at Java BigInteger class. This involves a lot of extra work, so consider it carefully or do it only after you have finished the required parts.

Additional extra credit for other advanced behavior based on complexity and completeness. Maximum extra credit available 30 points.

```
public class FractionalCalculatorCP1
{
public static void main(String[] args)
{
// TODO: first write the getNumerator, getDenominator, and convertToFraction
// methods below. When you run the program, these five lines will help you
// see if your code is working. You can add additional lines here if you
// think of other inputs you want to test.
//
// Once you're happy with those three methods, comment out these five lines
// and instead write the code required by the fractional calculator spec for
// checkpoint 1. You'll need to read an input from the user using a Scanner,
// then use your convertToFraction method to display it in fraction form.
System.out.println("The numerator of 3/4 is: " + getNumerator("3/4"));
System.out.println("The denominator of 4/9 is: " + getDenominator("4/9"));
System.out.println("8 as a fraction is: " + convertToFraction("8"));
System.out.println("9/10 as a fraction is: " + convertToFraction("9/10"));
System.out.println("2_1/3 as a fraction is: " + convertToFraction("2_1/3"));
}
/**
* getNumerator should find the numerator from a string that is a fraction.
*
* Precondition: the fraction parameter must be a valid fraction (a numerator, a
* slash, and a denominator, like 3/4).
*
* @param fraction - the fraction to find the numerator of
* @return the numerator
*/
public static int getNumerator(String fraction)
{
// TODO: write code here that uses indexOf and substring to find the
// numerator part of fraction, then use Integer.parseInt to convert
// it to a number and return it.
return 0; // remove this incorrect line and write your own implementation!
}
/**
* getDenominator should find the denominator in a fraction string
*
* Precondition: the fraction parameter must be a valid fraction (a numerator,
* a slash, and a denominator, like 3/4)
*
* @param fraction - the fraction to find the denominator of
* @return the denominator
*/
public static int getDenominator(String fraction)
{
// TODO: write code here that uses indexOf and substring to find the
// denominator part of fraction, then use Integer.parseInt to convert
// it to a number and return it.
return 0; // remove this incorrect line and write your own implementation!
}
/**
* convertToFraction should take a string input that may be a whole number, a mixed
* number, or a fraction and return it as a proper or improper fraction.
*
* @param input - a whole number, mixed number, or fraction to convert
* @return - the equivalent of input as a fraction, proper or improper
*/
public static String convertToFraction(String input)
{
// TODO: use the String methods indexOf and substring along with some
// if and else statements to figure out if input is whole, mixed, or
// fraction and to convert it to a proper or improper fraction. Be sure
// to use getNumerator and getDenominator whenever you have a fraction
// and need the numerator or denominator.
return ""; // remove this incorrect line and write your own implementation!
}
}
```

```
Welcome to the Fraction Calculator!
Enter an expression (or "quit"): 1/2 + 1/3
5/6
Enter an expression (or "quit"): 1_1/2 + 1/4
1_3/4
Enter an expression (or "quit"): 8/4 + 2
4
Enter an expression (or "quit"): -1 * -1/2
1/2
Enter an expression (or "quit"): -11/17 + -1/17
-12/17
Enter an expression (or "quit"): 1/3 * 3
1
Enter an expression (or "quit"): quit
Goodbye!
```