Now for this assignment, there is also a choice between two simple examples
using basic components and layouts.
-
Simple calculator applet.
Look at the example problem 10.10 on page 577 of the Deitel textbook,
which shows a GUI for a calculator. For your applet create a similar GUI
with the textfield at the top for the calculator display and with buttons
for numbers and operators. You should have at least one arithmetic operator,
such as "+", "clear", and "=".
You may use a combination of border layout and grid layout, such as
shown in the class examples BorderLayout1.java.
To implement the buttons:
-
Note that you need to have a variable to keep track of at least one number,
the "accumulator". This is initialized by the program to 0.
-
Whenever you push a number button, it appends that digit to the string
in the textfield at the top.
-
Whenever you push the = button, the digits in the textfield are converted
to a number (you can use the method Integer.parseInt), and put into the
accumulator.
-
Whenever you push the + button, it adds the contents of the textfield as
an integer (parseInt again) to the current contents of the accumulator
and displays the result in the textfield.
-
For the clear button, clear both the textfield and set the accumulator
to 0.
-
Keyword search applet.
For this applet, you will need to make up some data to be searched.
I suggest having an array of song titles (each song title is represented
as a String in one element of the array) and an accompanying array of artists.
Then the GUI will represent finding song titles by keyword search. There
will be a textfield for the user to type in a keyword. Then you should
also have a choice of case sensitive or case insensitive search. This can
be represented by two radio buttons, a drop-down menu or list.
You might introduce a boolean variable to keep track of whether the
search is to be case sensitive or not. When the user selects the case sensitive
checkboxes, choice or list, you can just change the value of this variable.
Then when the user hits enter on the keyword, design a search function
to search the strings in the song title array. You can use the String method
indexOf to find occurences of the keyword in each string in the array.
Design a way to make the search case insensitive (probably using string
method toLowerCase), depending on whether the search is supposed to be
case sensitive or not.
Use a layout of some form; I leave the design up to you.
When you submit your applet, link both the applet (.html file) and the
source code (.java file) to your homework page (along with problem and
program description as usual). Also please make a note of what browser
or appletviewer your applet looks best in.