time due | submission file |
---|---|
11:00 PM Friday 19 October, 2007 | csci/201/Homework4 |
At exactly 11:00 PM, on Friday, 19 October, 2007, a program will be run to copy your homework out of your csci/201/Homework4 directory on the UNCA Computer Science server. Your homework must be stored in this directory exactly as specified a bit later in this handout in order to be copied. Programs that are not stored correctly will not be graded and will be considered unsubmitted.
At 11:00 PM on each of the three days following 19 October, additional attempts will be made to copy programs that are submitted late. There is a penalty of 15% for each day a program is submitted late.
Write a calculator program using a while
loop
(or do
-while
loop)
and a switch
or if
-else
statement.
Your program can be thought of as always holding a single number,
as does a real calculator.
We'll call that number current_total.
When your calculator is first turned on,
current_total is zero.
Your program begins by asking the user to select an
operation (+, -,
*, /, %, or q
)
and then, if the operation is not q,
to enter one floating point number.
We'll call that number operand.
Your program then does one of the following:
This behavior is repeated indefinitely.
A sample run might proceed as follows:
The current total is 0 Enter an operation (+, -, *, /, %, or q): + Enter an operand: 2 The result is 2.0 The current total is 2.0 Enter an operation (+, -, *, /, %, or q): * Enter an operand: 3.0 The result is 6.0 The current total is 6.0 Enter an operation (+, -, *, /, %, or q): - Enter an operand: 1.1 The result is 4.9 The current total is 4.9 Enter an operation (+, -, *, /, %, or q): $ $ is an illegal operation The current total is 4.9 Enter an operation (+, -, *, /, %, or q): % Enter an operand: 2.0 The result is 0.9 The current total is 0.9 Enter an operation (+, -, *, /, %, or q): q
Remember to adhere to the style guidelines specified in assignment 2.
Java's Scanner
class does not naturally read characters.
We suggest you insert (by cut-and-paste) the following method
definition into your program.
public static char nextOperation(Scanner s) { try { return s.next().charAt(0) ; } catch (Exception e) { return 'q' ; } }
Let's assume you have created a Scanner
called
stdin
in your program.
Scanner stdin = new Scanner(System.in) ;
Then you can use the nextOperation
method to read the
operator.
char operation = nextOperation(stdin) ;
Your completed program must be stored within
a subdirectory
csci/201/Homework4 of your Linux home
directory. If you create a project named Homework4
at project location csci/201 as was done in
the first week's lab this will happen automatically.
If you complete this assignment on your "home" system,
you still must:
(1) create a Homework4
projects;
(2) accept the defaults when creating your project thus
creating a program with a Main
class within
the homework4
package;
(3) create a csci/201/Homework4
directory within your Linux account to store your work; and
(4) copy the src/homework4/Main.java
of your Homework4
project at home into your
csci/201/Homework4 directory at UNCA.