! write a java program
CISC 3120:Design and Implementation of Software Applications I -
Zimi Li - Lab V.2
instructions
This is the second lab for unit V.2.
The lab will be distributed and worked on in class on Apr 24.
This lab is due at 11:59pm, May 7 and must be submitted through blackboard system.
Grading policy
20 points - Compilation: Each le must compile without error or warning into a class le.
20 points - Execution: Each executable must run without error or warning on valid input using the
command line parameters described above.
30 points - Correctness: Is/Are the algorithm(s) implemented correctly? Have an appropriate number
of word/document representations been used and used correctly?
10 points - Style: Is the structure of your program clear and coherent? Are functions and variables given
self-explanatory names? Are functions used to aid intelligibility of the code? Are functions used to reduce
repeated blocks of code? Is indentation, spacing, use of parentheses, use of braces consistent, and sensible?
For example, if you use brackets on the same line at the start of a block, always do so. If you place a
brace on the line following the start of the brace, always do so. If you put a space between variables and
operators, e.g. if (i == j), always do so. So, if (i == j) i = j+k; is bad. It should be if (i == j) i = j + k;
or if you prefer if (i==j) i=j+k;. You will be graded on consistency in these decisions, not on any particular
style.
10 points - Comment: Every function should include a comment minimally describing
1. what it does
2. what its inputs are
3. what its output is
4. who the author is
You may use a javadocs. Are there eective other comments throughout the code? For a good read check
out the google style guides: http://google-styleguide.googlecode.com/svn/trunk/javaguide.
html
10 points - Instructor's Discretion Has this assignment gone beyond the minimal requirements in
a substantive way? Is it especially clear? Is the code especially well written? Is the response particularly
thoughtful or insightful? Have non-trivial representations been examined?
1
1 Calculator
You should start from versions of the two Java objects:
SimpleCalc.java
CalcComponent.java
These give you an initial version of the calculator.
1.1 Extending addUp
The rst thing to do is to extend the functionality of the addUp function:
1. Make addUp work with double arguments.
Note that this will involve quite extensive changes, not just to the function, but also to some of the attributes
of the CalcComponent class.
2. Change the display to allow the reading and writing of double values.
You will need to use the class Double rather than Integer and increase the number of columns in the text
elds (and I'm sure do some other things that I have not listed).
3. Now extend addUp so that it works with any number of arguments
1.2 Add more functions
1. At the moment, the calculator only adds up numbers,providing functionality for the + key. Extend it so
that it provides functionality for the other keys,* / - (that is multiplies, divides and subtracts).
The functions you write should, like the modied addUp, handle double arguments (you don't need to make
them handle any number of arguments).
2. Now add some additional functions (again with double arguments):
sqrt (square root)
sin (sine)
max (maximum)
min (minimum)
Note that square root and sine only take one argument.
1.3 Alter the interface
At this point the interface starts looking a bit unsatisfactory. (It doesn't look like either my old high school
calculator, or the one on my computer). Those calculators just have one eld. The calculators use that one eld
to display input and show output. So, modify your calculator in the following way:
1. Reduce the number of text elds to 1.
2. Add an = (\equals") button.
3. Alter your code for the + button so that when the user hits +, the text eld is cleared, but the number
that was in it is stored.
4. The relevant calculation is only carried out once you have hit the = button.
2
5. For this simple version you can assume that you will have to deal with two possible sequences of operations
of the following form:
24 + 3 =
So, you expect a number, then, another number and then equals. When you get to the = you print the
answer in the text eld.
6. Now extend the calculator to work like this for all the binary operators.
7. Now consider the sqrt operation. For this you have a sequence like:
25 sqrt
So you expect a number and then the operator, and when you get the operator you print the answer in the
text eld.
8. Now extend the calculator to work like this for sine as well.
1.4 Extra Credits
The challenge problem is to modify the calculator further.
1. My computer has a calculator that behaves as follows.
2. The user can type in numbers into the text
eld as before, and pressing the operation keys (+, -, etc) just adds those symbols to the text eld.
3. When the user hits =, the computation in the text eld is carried out.
4. The challenge is to make your calculator work like this.
5. Writing the various symbols to the text eld is easy.
6. To do the computation, you will have to parse the string you get from the text eld.
Probably the easiest way to do this is to use the String method split.
This takes a string and from it generates an array of strings broken up in a way you specify.
If you complete the challenge you will get extra credit beyond what you would get for completing the previous
one (you only need to do one).
If you complete the challenge, you should (a) document in the comments exactly what kinds of sequence of
operation the calculator will handle and (b) change the text at the top of the window to read \Challenge
Calculator", so I know what I am dealing with.
2 Debrief
Attach a text le answering these questions:
Did you work alone, or did you discuss with other students? List their names.
How many hours did you spend on this assignment?
Would you rate it as easy, moderate, or dicult?
Are the lectures too fast, too slow, or just in the right pace?
Any other comments?
3
Additional Information
This is SimpleCalc.java
// A slightly more advanced version of the simple graphical calculator
import javax.swing.*; // Use the swing library
public class SimpleCalc{
public static void main(String[] args){
JFrame frame = new JFrame("My Simple Calculator"); // Set up frame
frame.getContentPane().add(new CalcComponent()); // Create new component
frame.setSize(300,200); // Set frame size
frame.setVisible(true); // Make frame visible
}
}
4
This is CalcComponent.java
// A graphical object that does some very basic arithmetic operations.
import java.awt.*; // Use the awt library
import java.awt.event.*;
import java.awt.geom.*;
import javax.swing.*; // Use the swing library
import java.util.Random; // Get random numbers
import java.lang.Number.*; // Need this for Integer
// The Moving Component class is a subclass of JComponent and defines an object that listens
// for actions (in this case multiple button presses).
public class CalcComponent extends JComponent implements ActionListener {
int a = 0;
int b;
int result = 0;
JButton buttons[];
JTextField aField;
JTextField bField;
JTextField rField;
Random randomGenerator = new Random(); // Need this to generate random numbers.
// Set up buttons and add a listener for each. The first four buttons (should)
// implement some arithmetic operations, the last resets the calculator.
public CalcComponent() {
// Set b to a random value (have to do this in a method) and use
// GridLayout to lay the interface out in two columns.
b = randomGenerator.nextInt(10);
setLayout(new GridLayout(0,2));
// Create buttons and add them
buttons = new JButton[5];
buttons[0] = new JButton("+");
buttons[1] = new JButton("-");
buttons[2] = new JButton("*");
buttons[3] = new JButton("/");
buttons[4] = new JButton("Again!");
for(int i=0; i<5; i++){
add(buttons[i]);
buttons[i].addActionListener(this);
}
// Create fields and add them
aField = new JTextField();
aField.setColumns(3);
add(aField);
bField = new JTextField();
bField.setColumns(3);
add(bField);
5
rField = new JTextField();
rField.setColumns(3);
add(rField);
// Send values to the textfields
display();
}
// Do the relevant operation on the numbers when a function key is
// hit.
public void actionPerformed(ActionEvent e){
// First, read the value of a from the textfield:
String aString = aField.getText();
Integer aInt = new Integer(aString);
a = aInt.intValue();
// Now respond to buttons
// The first button is +, so add up the numbers
if(e.getSource() == buttons[0]){
addUp(a, b);
}
// This is the Again! button, so reset a, b and result.
if (e.getSource() == buttons[4]){
a = 0;
b = randomGenerator.nextInt(10);
result = 0;
}
// Now send values to the text fields
display();
}
// A function to sum up two integers, writing the outcome into
// the attribute result.
public void addUp(int x, int y){
result = x + y;
}
// Display the values of a, b and result in the relevant text fields
public void display(){
Integer aInt = a;
aField.setText(aInt.toString());
Integer bInt = b;
bField.setText(bInt.toString());
Integer rInt = result;
rField.setText(rInt.toString());
}
}
6
12 years ago
Purchase the answer to view it

- 5cl6png1.zip
- simplecalc.java