Fraction Calculator Spec

advertisement
AP Computer Science
Java Project
Fraction Calculator
Introduction:
The objective of this assignment is to create a calculator to compute arithmetic operations between
integers and/or fractions and output the result as a reduced mixed fraction. This project will test much of
what you have learned thus far in the school year. This project will serve as your cumulative assessment
of the skills you have acquired over the course of the first quarter. It is very important that you READ this
document completely. Many students have done this project and gotten ‘working’ code, but cannot pass
my testing because they did not follow this guide to the letter. You must provide all the static methods
listed in section 5 or your project will not pass the checkpoints!
This is an individual project. You may talk about the algorithms, but under no terms should you share
code or pseudocode. If you are caught sharing or taking code, you will get a zero on the entire project.
Since this is a major project, this can have a major impact on your grade. In years past, code has been
pasted on Facebook and shared. If you have done this before, or are asked to do this, don’t. This is the
project where different and unique codes start to emerge. There are many ways to complete this project,
and your thinking will be unique to you. I have many examples in my two years of giving this project of
where students waited until the deadline and pressured other students to pass their code on. Don’t fall
into either of these camps. Get help from me, talk through the problems with your classmates.
Please…Please DON’T steal or share code. Consider yourself warned.
You may only use Objects learned in class. If you find an object in the API you think would help, talk to
me first. If you use an object that I do not approve, you may either miss a check point or be docked
points and be asked to redo code without using said object. The reason for this is some objects will
abstract the algorithms I wish for you to puzzle out. These are important to your understanding of
computer science and problem solving in general.
Specifications:
- Input:
-
-
-
Input will be in the form of a value, followed by an arithmetic operator, and then another value.
Values and operators will be separated by a single space. Values will contain no spaces.
Input values may be in the form of mixed fractions, proper fractions, improper fractions or
integers. The integer and fraction parts of a mixed fraction will be separated by an underscore (_)
(e.g., “1_3/4” is one and three fourths to distinguish it from “13/4” which is thirteen fourths).
The calculator will support the 4 basic arithmetic operations: add (+), subtract (-), multiply (*),
and divide (/).
The program should accept an equation, calculate the result in the proper form, print it to screen,
and then be ready to accept another equation. The user should be able to exit the program by
entering the command “quit" instead of an equation.
See the examples section below for clarification.
- Output
-
-
The output value must always be reduced and never improper (it may be an integer, fraction, or
mixed fraction, as appropriate). Example: a result of 10/4 should be printed to the screen as
2_1/2).
See the examples section below for clarification.
- Examples
Input
Output
Notes
1/4 + 1_1/2
8/4 + 2
-1 * -1/2
-11/17 + -1/17
1_3/4
4
1/2
-12/17
Input may be an improper fraction.
1. Sample Execution Log (user input bold and underlined)
Welcome to the Fraction
Enter an expression (or
5/6
Enter an expression (or
1_3/4
Enter an expression (or
4
Enter an expression (or
1/2
Enter an expression (or
-12/17
Enter an expression (or
1
Enter an expression (or
Goodbye!
Calculator!
"quit"): 1/2 + 1/3
"quit"): 1_1/2 + 1/4
"quit"): 8/4 + 2
"quit"): -1 * -1/2
"quit"): -11/17 + -1/17
"quit"): 1/3 * 3
"quit"): quit
2. Additional Comments
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.

The topics we have been learning in class are designed to allow you to complete this assignment. Remember to
review your notes and the textbook.
o Scanner, indexOf, substring,contains etc.
My helpful hint: use the Integer class parseInt() method. [int num =
Integer.parseInt(str.substring(0,5));]
3. Checkpoints
Checkpoint1(due 11/25/15): your program will correctly parse the string fractions into the integer
components and echoes the parts of the input.
o Enter your input or quit to exit? 1/4 + 1_1/2
Fraction 1: 1/4
Operation: +
Fraction 2: 1_1/2
-
Checkpoint2(due 12/8/15): your program will produce the correct result when fractions are not mixed
Checkpoint3(due 12/16/15): your program is amazing! You use an input loop that quits when the user
types Yes or yes or y to the question : Enter your input or quit to exit?
4. Grading Scheme (45 points total)
Parses input correctly: 3 points
Addition: 3 points
Subtraction: 3 points
Multiplication: 3 points
Division: 3 points
Result reduction: 3 points
Mixed fraction input: 3 points
Mixed fraction output: 3 points
Handles negatives: 3 points
Checkpoints met: 15 points
Comments/style: 3 points
5. Methods Required:
reduce has two int parameters for numerator and denominator and returns a String
multiply has four int parameters for numerator1,denominator1,numerator2,denominator2 and returns a
String
add has four int parameters for numerator1,denominator1,numerator2,denominator2 and returns a String
doTheMath has 5 parameters 1 String for operator, four int parameters for
numerator1,denominator1,numerator2 there is no return.
processFractions has 3 String parameters for fraction1, operator, fraction2 there is no return
main method – is the code input engine. You will have a while loop that uses the fence post to prompt the
users for input or quit to exit.
6. Extra Credit:
2.5 points for calculators that can handle more than one operation (e.g. 1 + 1 + 1 - 1/2)
2.5 points for correctly mainlining the order of operations with more than two inputs (e.g. 1 - 2 * 4 returning -7
instead of -4)
2.5 points for handling bad input gracefully (e.g. 1 + + 1/2 does not cause the program to crash)
2.5 points for an infinite precision calculator. This means your calculator can handle very large numbers and still
produce the right results. You will need to use the 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.
Download