Calculator
Lab #6 (AY2007/8 Semester 1)
Date of release: 9 October 2007, Tuesday, 23:59.
Submission deadline: 17 October 2007, Wednesday, 23:59.
School of Computing, National University of Singapore

0 Introduction

This lab contains one exercise. You are required to submit one program.

You are advised to review the material covered in chapters 1 through 9 and read Programming Style, Design and Marking Guidelines before attempting this lab assignment. You should not use syntax or constructs not covered in lectures; you shouldn't need to and may even be penalized for doing so (if the objective of the assignment is undermined).

A word of advice: Code incrementally. This is even more important now than before as your program gets longer. Don't try to finish all parts of a program then compile it. Write your program in bits and pieces and compile frequently. Try to maintain a compilable program even while you are working on it. Submitting a compilable program that partially works is better than submitting an un-compilable one. This last point especially applies to the programming exams. Seriously, code incrementally.

The following topics have not been covered and hence you should not use them (if you are in doubt whether you can use certain features not mentioned below and are not yet covered in class, please consult your discussion leader or lecturer first):

You should use Scanner class on System.in for input and System.out for output in your programs, unless otherwise stated.

Your last output statement should be a println and not print.

Test your programs thoroughly with your own input data before you submit to CourseMarker. Do not use CourseMarker as a debugging tool. For this lab, the number of submissions is set to 8. Only your last submission will be graded.

In general, the expected time to complete a lab assignment (including all graded exercises) is between 3 to 5 hours. You should aim not to spend more than 5 hours. It might be difficult at first, but it should get easier as you gain experience. Make that your target.


1 Exercise 1: Calculator (100%)

1.1 Learning objectives

1.2 Task

Write a program Calculator.java to implement a simple calculator of basic operations (addition, subtraction, multiplication and division) on integers.

The user may enter a string comprising an operand, a binary operator, and a second operand, with any number of space characters between them. There may be leading spaces and trailing spaces in the input.

If an invalid input is encountered, a message "Error in expression" is displayed and the calculation is skipped. Otherwise, the respective operation is carried out, and the result displayed. The user then enters another string. To end the calculation, the user enters 'q' (without any leading or trailing spaces).

The operand is an integer. Examples of valid operands are 23, 0, -123, but not 23.5 (not an integer), 51.0 (not an integer), +123 (unary plus not accepted). You may assume that the operands and the result are within the range of value of the int type.

You need to handle division-by-zero error so that your program does not crash when it happens.

A partial code is given:

The data members given in the partial code are just suggestions. You are free to use your own approach.

Please refer to the sample outputs below.

1.3 Sample runs

Sample run using interactive input (user's input shown in blue; output shown in bold purple):

$ javac Calculator.java
$ java Calculator
     123  +   345  
123 + 345 = 468
-20+13
-20 + 13 = -7
  004   -10
4 - 10 = -6
 12*-0011
12 * -11 = -132
30  /  4 
30 / 4 = 7
  123/-7 
123 / -7 = -17
q

Another sample run that shows some invalid inputs:

$ java Calculator
xyz
Error in expression
a + b
Error in expression
12345
Error in expression
-10 * 
Error in expression
12.3 + 32
Error in expression
1 + 2 + 3
Error in expression
+30 / 3
Error in expression
30 // 7
Error in expression
333 / 0
Division by zero error
333 / 4
333 / 4 = 83
q

1.4 Submission

Submit your program Calculator.java through CourseMarker.

1.5 Important notes


2 Deadline

The deadline for handing in all programs is 17 October 2007, Wednesday, 23:59. Late submissions will NOT be accepted.

Start early. Do not wait till the eleventh hour and rush to meet the deadline.





tantc@comp.nus.edu.sg
Tue Sep 11 15:45:35 SGT 2007