Due Date: Thursday October 28, 1999 at start of class.
Assignment:
Build a primitive calculator that is similar to the demo calculator
that you can run from the link in the Lab10 segment of the web page.
Background
This is a four function calculator with a clear key as in the applet
you can access from the link for this lab. I f you do not have the
printed handout, you should run that applet to see what the final product
should look like.
- Input from the user and output to the user will be through TextField
objects.
- There will be five buttons: + - X / clear
- There will be a minimum of three Label objects one for each TextField.
You may want to add a fourth Label object at the top with the product name
of your invention/design...
- The layout will be flow layout so be sure to size your applet to
insure that the components are nicely arranged.
- You do not have to arrange your components as shown above.
- The next page shows you how to get the text from the TextField objects
and how to put text into the TextField objects
You should make the font larger than the default size so older people
(re: Jim) can read the buttons and see what they typed in. Colors
are nice if the text is well contrasted from the background
Handin Instructions:
You should hand in a folder that contains these two files: Lab10.java
and Lab10.html. DO NOT HAND IN THE FILE Lab10.class - it is too big.
Name the folder with this convention
LastName-FirstInitial-Lab10
Helpful Hints and Actual Information You Will Need for Lab 10:
Using TextField objects for user input:
All user input from the keyboard is text -- Yes! - text.
When the user types 23 thinking they just typed in the number twenty three,
they actually typed the text digit 2 and the text digit 3. The software
must convert this text to a number.
You will have to convert the text into the type double since we want
to be able to compute with decimal numbers. Doing this requires two
steps:
1. getting the actual text from the TextField object.
2. converting the text into a double
1. To get the text you can use the TextField object's method getText().
Let's assume we have a TextField object called input. We would write:
input.getText();
This returns the text that the user typed into the TextField, input.
2. To convert the text typed by the user into a real number of
type double, we use the following method:
Double.parseDouble(some text to be parsed);
"Parsing" means to break down into something meaningful. We want
to parse the text into a number. There is something very subtle that
you must notice and remember in this line of code. The word "Double"
begins with an uppercase "D". To declare a real variable we use the lowercase
"d" like this:
double num = 0.0;
Classes in Java begin with Uppercase letters so Double is a class,
not a real number like double. Double is a class that allows us to
treat real numbers as objects - something that is useful from time to time
like now. The actual code for converting the user's text input to
a real number is something like this assuming that the variable num is
declared as type double;
num = Double.parseDouble(input.getText() );
This will take the text entered by the user into the TextField object
input and convert it into a real number and store that value in the double
variable num.
Having Your Program Put the Computed Answer Into the TextField Object
To put text into a TextField object, you use the following TextField
class method (assuming that output is a TextField object:
output.setText("some text to be put into the TextField object");
BUT you will not have text - you will have computed a double when you
add/subtract/multiply/divide the two numbers. If you have the computed
result stored in a double variable called answer, this will be WRONG!
output.setText( answer);
Sorry but you must put text or stuff between two quotation marks into
the TextField . Alas and Alack we are doomed.... Nope, we will result
to trickery... We will trick the TextField object output into thinking
it has text by putting in an empty or null string. Things in quotation
marks are called strings. If you have a pair of empty quotation marks
(""), you have a null string. Even though it is empty, it is considered
text. What we do is to add the answer to the empty string like this:
output.setText( "" + answer);
No kidding - this really works... This is called concatenation.
We concatenate the answer to the null string using the + operator.
Getting the size of your applet window :
You can adjust the applet window when you use the applet view by using
the mouse. It is frequently easier to make the original applet window big
and then adjust it to a good size. Once you have the window set,
you need to know how big it is. The applet can tell you this.
You need to use a Dimension class object. You can do the following
in the paint method when you are developing your code.
Dimension dim;
dim = getSize();
showStatus("width = " + dim.width + " and height is " + dim.height
);
This declares a Dimension object called dim. the Applet class method getSize( ) can measure the height and width of the applet window and return those values to a Dimension object. The Applet class method showStatus displays the status of the applet in the last line or status line of the applet at the bottom of the screen. Note that the argument in the parenthesis must begin with text meaning that it must begin with quotation marks. The + operator literally adds (or concatenates) the next argument to the original text.
You should comment out the showStatus line in your final version of the applet --- the user does not need to see it.