handout #19

CSE142—Computer Programming I

Programming Assignment #6

due: Monday, 11/15/04, 9 pm

This assignment will give you practice with external input files.  You are going to write a program that simulates a simple calculator.  The input file will contain a series of expressions like the following:

38.4 - 2.7 * 19.4

You will read this input line as a series of tokens and will “do the math.”  We will be doing a very simple calculator-like computation.  There won’t be any rules of precedence to figure out, for example.  We will simply evaluate this expression from left to right, the way a calculator would.  So first we read 38.4.  Then we see that we’re supposed to subtract 2.7, which leaves us with 35.7.  Then we are supposed to multiply by 19.4, which leaves us with 692.58.

Your program will need to prompt the user for a file name and will have to keep asking for a file name until the user gives a legal response.  The easiest way to accomplish this is to copy the “boilerplate” code from handout #18 (the method called getInput) into your program.

Your program has to recognize all of Java’s normal arithmetic operators (+, -, *, /, %) as well as the operator “^” which represents exponentiation.  You will want to use Java’s Math.pow method to perform the exponentiation.

Look at the attached log of execution to see exactly how your program is to behave.  You are to give an introduction to the user, then prompt for a file name, then process each input line and show each input with its value, and then show some overall statistics.  Notice that whitespace within the input line is preserved when you echo it to the user in the console window.  You are required to exactly reproduce the format of this log.

You may assume that the input file has no errors.  In particular, you may assume that each line of input begins with at least one number and then has a series of operator/number pairs.  There will be whitespace separating the various numbers and operators.

Your program should have the following static methods in addition to method main:

·        a method to give an introduction to the user

·        a method to repeatedly prompt the user for an input file name until a legal file name is given

·        a method to process one line of this input file (just one line, not the entire file)

·        a method that processes the entire file

You may define more methods than this if you find it helpful, although you will find that the limitation that methods can return only one value will tend to limit how much you can decompose this problem.

Remember that to compare Strings for equality in Java, you need to use the equals method rather than the usual “==” operator.

For example, if you want to see if the String variable token is equal to a dollar sign, you’d say:

if (token.equals("$"))

    <do something>

We will once again be expecting you to use good programming style and to include useful comments throughout your program.

Your program should be stored in a file called Calculator.java.  You will need to include the files Scanner.java and calculator.txt from the class web page (under the “assignments” link) in the same folder as your program.

Input file calculator.txt

84.3

2   +   2

18.2 - 4.3

17.9 * 12.3

18.4 / 2.1

23 % 3

2 ^ 3

19      *  3    /  4

38.4 - 2.7 * 19.4

203 % 7.2 ^ 2   -    3 / 2

9 - 2  *  3.4 + 17.8 / 2.4 % 3.8 * 7.4

3 -  4   + 8 * 2 / 3 - 2 + 18 * 2 % 9.1 * 7.4 - 2.8

29 - 3 + 4 * 7 / 2 % 18 * 3 - 42.8 + 17.4 * 2.3 - 3.4 / 2.5 - 3.8

Log of execution (user input underlined)

This program will process an input file of

arithmetic expressions.  Your file should

have one expression per line.

 

What is the name of the input file? foo.txt

File not found.  Please try again.

What is the name of the input file? calc.txt

File not found.  Please try again.

What is the name of the input file? calc.dat

File not found.  Please try again.

What is the name of the input file? calculator.dat

File not found.  Please try again.

What is the name of the input file? calculator.txt

 

84.3 = 84.3

2   +   2 = 4.0

18.2 - 4.3 = 13.899999999999999

17.9 * 12.3 = 220.17

18.4 / 2.1 = 8.761904761904761

23 % 3 = 2.0

2 ^ 3 = 8.0

19      *  3    /  4 = 14.25

38.4 - 2.7 * 19.4 = 692.5799999999998

203 % 7.2 ^ 2   -    3 / 2 = -0.520000000000007

9 - 2  *  3.4 + 17.8 / 2.4 % 3.8 * 7.4 = 15.78666666666669

3 -  4   + 8 * 2 / 3 - 2 + 18 * 2 % 9.1 * 7.4 - 2.8 = 33.7066666666667

29 - 3 + 4 * 7 / 2 % 18 * 3 - 42.8 + 17.4 * 2.3 - 3.4 / 2.5 - 3.8 = 12.872

 

Total lines    = 13

Average result = 85.36978754578755