Pocket calculator - School of Informatics

advertisement
School of Informatics, University of Edinburgh
Computer Science 1 Ah
CS1Ah Practical 1
Pocket calculator
This is an individual practical exercise which requires you to submit some files electronically.
A system which measures software similarity will be used to compare all of the submissions in
order to detect unreasonable levels of collaboration. Your attention is drawn to the guidelines
on plagiarism presented in Appendix A of the Computer Science 1 course guide.
This practical has been issued on Thursday 24th October. The deadline for your solutions
is Wednesday 6th November at 5pm. Your solutions must be submitted electronically from
your DICE account with the handin command (which is a short-hand version of the submit
command). No alternative methods of submission will be accepted. Specifically, submissions
via email will not be accepted and submissions on floppy disk will not be accepted. No late
submissions will be accepted.
To submit your files, execute the handin command exactly as it is shown in this document.
In particular, make sure that you use the correct practical identifiers, filenames and extensions. Remember that all names are case-sensitive. Failure to execute the proper submission
command cannot be investigated after the practical deadline. Execute the handin command
from a shell window (not the KDE command “Run Command” window), so you can see any
error messages which result. If in doubt, consult a laboratory demonstrator for assistance.
You can repeat the handin command to resubmit improved versions of your work; later
submissions will override earlier ones. But do not resubmit after the deadline: your work
would be treated as a late submission.
The four practical exercises in Computer Science 1Ah are equally weighted and together
they constitute 25% of the final mark for the course. For each practical, a mark out of 25 is
recorded. This practical exercise has three parts, Parts A, B, and C. Marks are divided between
the three parts with equal weighting, and one mark will be allocated for good programming
style.
1
School of Informatics, University of Edinburgh
Computer Science 1 Ah
Introduction
In this practical you will construct a simple calculator in Java. The focus of this
practical is on basic computational arithmetic. You will be supplied with all of the
necessary code for performing the Java graphical operations. Your task will be to
construct the working mechanism of the calculator. By the end of this practical you will
have an understanding of the following concepts: infix evaluation, integer arithmetic,
and arbitrary precision arithmetic. A picture of the calculator appears in Figure 1.1.
Figure 1.1: The calculator application
A calculator performs infix arithmetic. This means that the operator (e.g. +, –, *, /)
appears between the operands in the manner that you are accustomed to writing (for
example, 1 + 2). Other forms of computer arithmetic are prefix where the operation
appears before the operands (as in + 1 2) and postfix where the operation appears after
the operands (as in 1 2 +).
OP
L
R
Figure 1.2: Infix Tree
The method that we will use to evaluate infix expressions relies on a tree structure
illustrated in Figure 1.2. The tree contains a left node L, a right node R, and an
operation node OP. The subtraction 5 – 4 is represented in Figure 1.3. The left node
of the tree contains the left operand of the calculation (5), the right node contains the
right operand (4), and the operation node contains the operator (–). It is important that
the left and right nodes are not confused as 5 – 4 is not equal to 4 – 5.
2
School of Informatics, University of Edinburgh
Computer Science 1 Ah
–
5
4
Figure 1.3: Infix calculation
A calculator performs a sequence of such arithmetic operations. Consider the following sequence of key-presses:
7
*
6
/
2
+
5
=
Figure 1.4 illustrates how this sequence of calculations is performed inside the
calculator using the tree. Each step of the diagram represents a key-press:
*
7
*
/
7
7
Step1 (7)
Step2 (*)
Step3 (6)
/
+
42
2
Step5 (2)
6
42
Step4 (/)
+
21
21
Step6 (+)
5
Step7 (5)
26
Step8 (=)
Figure 1.4: Calculations
The rules for evaluation appear below. You should convince yourself that the above
example follows these rules.
1. If a numeric key is pressed, the number is entered into R (steps 1, 3, 5 and 7).
2. If an operator key (+, –, * or /) is pressed and OP is empty, then the contents of R
are shifted into L , and the operation is entered into OP (step 2).
3. If an operator key is pressed and OP is not empty, then the calculation represented
by the tree is performed and the result is stored in L (L = L OP R). OP is then
updated with the new operation, and R is cleared (steps 4 and 6).
4. If the equals key (=) is pressed, then the calculation represented by the tree is
performed and the result is entered into L (as in the previous rule). OP and R are
then cleared (step 8).
For simplicity we will only be concerned with integer arithmetic in this practical. Thus,
our division operations will ignore the remainder of the calculation (e.g. 8/3 = 2).
3
School of Informatics, University of Edinburgh
Computer Science 1 Ah
Exercise
Before starting the practical you should make your own copy of the files in the directory
/group/teaching/cs1/Ah/Pracs/Calculator. You can do this efficiently using the
command:
cp -r /group/teaching/cs1/Ah/Pracs/Calculator ˜
You should only enter the above command once. If you use it a second time you will
overwrite your copy of the practical and you will lose the work which you have done.
Your home directory will now contain a directory Calculator containing subdirectories PartA, PartB, PartC, and PartD. For Part A of the practical you should issue the command: cd ˜/Calculator/PartA and edit the file PartA.java. Similarly,
for Part B you should change to the directory ˜/Calculator/PartB and edit the file
PartB.java and so forth.
To compile your Java files you should ensure that you are in the correct directory
and then issue the command:
javac *.java
If any errors are detected in your code they will be displayed at this point. Once you
have eliminated all of the errors and compiled your code, you may issue the following
command to start the calculator application.
java Calculator
Working on your computer at home
If you have access to a computer at home, you may wish to use it when doing some
of your practical work. For this you will need a copy of Sun’s Java Development Kit
Version 1.4 or similar. You can obtain it from Sun’s Web site at java.sun.com or it is
available on the CD-ROM included with the textbook, “Java: How to Program” by Deitel
and Deitel.
You will need to make a copy of the Calculator directory. If you have Internet
access, the most convenient way to do this is to copy files from the web, although this
is tedious. A better way is to use the secure copy command scp to remotely copy files
from your DICE account. Another way, or if you do not have Internet access, is to take
the files home on a floppy disk. On the DICE machines, the command mcopy can be
used to copy files onto a DOS formatted floppy disk.
Please consult a lab demonstrator if you need help with how to retrieve the files
needed for the practical exercise. Please do not ask your tutor or the course lecturers
about this.
4
School of Informatics, University of Edinburgh
Computer Science 1 Ah
Part A: Basic calculations
Your first task is to complete the code found in the file PartA.java. You have to provide
the body for the apply() method shown below.
private void apply(int right);
This method performs an optimised version of the infix evaluation technique described
in the introduction. Essentially, Rule 1 is merged with Rule 2, 3, and 4. This method
is invoked from the methods associated with the keys on the calculator. For example,
the add method is invoked when the + button is clicked, and this invokes the apply()
method. The method parameter right is the value displayed on the calculator screen
at the time the method is invoked. This corresponds to the value stored in the R node
for infix evaluation.
You have already been provided with the clear() method in the PartA class, which
is invoked when the ‘AC’ (all-clear) button is clicked. You should not need to alter this
method. The PartA class also contains the following fields.
private int left;
private char op;
The left field should be used as the L node. Similarly, the op field should be used
as the OP node. You should complete the apply() method according to the following
rules.
1. If the op node is blank(’ ’), then right should be copied into left.
2. If the op node is non-empty, the designated arithmetic operation should be performed: left op right, and the result should be stored in left. (After the
apply() method has completed, the method which invoked it will ensure that the
op field is updated with the corresponding new operation, so the apply() method
does not need to do this.)
3. If the equals button is pressed, and the op node is empty, the contents of right
should be copied into left. If op is non-empty, an arithmetic operation should
be performed as in the previous rule. (After the apply() method has completed,
the equals() method will ensure that the op field is set to a blank(’ ’) so the
apply() method does not need to do this.)
Once you have completed the apply() method, you should test the calculator with a
range of calculations. When you are happy that your calculator is behaving correctly
you can submit it for assessment with the following command.
handin A1a PartA.java
The code A1a is an abbreviation for “Computer Science 1 Ah, Practical 2, Part A”. If you
later discover an error and want to submit an improved Part A then use the handin
command again. Later submissions will overwrite earlier ones.
5
School of Informatics, University of Edinburgh
Computer Science 1 Ah
Part B: Arbitrary precision arithmetic
You may have noticed that the calculator that you constructed in Part A gives incorrect
results when dealing with large numbers. For example, enter the following calculations
and observe the results.
9876543210
=
1000000
1000000
1
25
–
*
2147483647
/
0
=
=
–
10
=
=
The problem occurs because the Java int type is limited to the range of values between –2147483648 and 2147483647. This corresponds to the maximum and minimum possible values for a 32-bit binary value (using 31 bits and 1 sign-bit). The
maximum and minimum values are given by the Java constants Integer.MAX VALUE
and Integer.MIN VALUE respectively.
When a value exceeds the maximum value (as in the first and second calculation),
arithmetic overflow is said to occur. Similarly, when a value becomes less than the
minimum value, arithmetic underflow occurs (as in the third example). Lastly, a division by zero results in an infinite value which is represented by the maximum possible
integer value.
A temporary solution to the underflow and overflow problem would be to use the
Java 64-bit long datatype. However, Java provides a much better solution to this
problem through the use of the java.math.BigInteger class. This class provides
support for arbitrary precision integer arithmetic. This allows much larger integers to
be represented although ultimately, of course, they are still limited by the size of the
memory of the computer and the arithmetic rule that division by zero is undefined
still applies. To represent a division by zero you should set the value of the divzero
boolean variable to true.
The BigInteger class provides the following methods, among others:
public
public
public
public
BigInteger
BigInteger
BigInteger
BigInteger
add(BigInteger val);
subtract(BigInteger val);
multiply(BigInteger val);
divide(BigInteger val);
Your task is to rewrite the operations of your calculator to use the BigInteger datatype.
You should create your solution by editing the file PartB.java, completing the apply()
method as before. When you are happy that your calculator is behaving correctly you
can submit it for assessment with the following command.
handin A1b PartB.java
Later submissions will overwrite earlier ones.
6
School of Informatics, University of Edinburgh
Computer Science 1 Ah
Part C: Memory functions
The calculator provides buttons for memory store (M) and memory recall (MR). In this
part, your task is to make these buttons operational. The file PartC.java contains the
following additions:
public boolean memory;
private BigInteger memval;
public BigInteger memIn(BigInteger right);
public BigInteger memRecall(BigInteger right);
You should use the boolean memory field to indicate when a value is stored in memory.
The value itself should be kept in the memval field.
The memIn() method is invoked when the ‘M’ button is clicked; the memRecall()
method is invoked when ‘MR’ is clicked and the memory indicator is true. You need to
complete the bodies for these two methods. You should arrange that clicking ‘M’ when
the calculator display is 0 clears the memory.
When you are happy that your calculator is behaving correctly you can submit it for
assessment with the following command.
handin A1c PartC.java
Later submissions will overwrite earlier ones.
Practical prepared by Stephen Gilmore.
David Aspinall, 2002/10/24 09:16:30.
7
Download