|
|
||
| You are here: | ||
|
A couple of examples which need rounding out to be of any practical use.
All arrays have a public length field which holds the number of items. Loop to set up the Button panel. Buttons can also be associated with action command Strings which we can then use in actionPerformed - previously we have used named Buttons.
SL Mastery aspect complex selection statements.
Our old friend the if else chain . Although not stricly neccessary, we add a + sign to the end to show the user that the add key was pressed. Here we add an err symbol.
The line is used to indicate that the sum is being displayed next. In a dossier we could document these as " user-friendly features ".
Check the string.
Not much can go wrong since the numerals are entered via the pad, however, two or more decimal points could be entered. We still want to trap this error rather than letting the program crash.
Source code: WordSquare.java
2D Button array
A different starting letter could be chosen. substring(int st, int ln) is a method of the String Class taking ln letters starting at st . See JETS pages on Strings.
Set it up so the increment operation is active initially.
SL Mastery Aspect: Nested Loops (always needed to deal with 2D arrays) e.getSource() works with Button elements too. indexOf(substring) returns the element of the string where substring starts (if any). see JETS String page .
We are moving on to the previous/next character in the ALLOWABLE String. So we check to see if we reached the start/end and wrap around. These buttons just set whether we travel forward or backward through the ALLOWABLE String. If you have followed the course this far, you have almost all the tools you need to complete a successful SL dossier. Sorting and searching are useful topics, followed by simple file-handling; then you are ready to go. |
On the page: [ cash register | word squares ] In Java the arrays may be of simple (primitive) data types or of other objects such as buttons, text fields and so on. A common example might be to have a keypad which is useful in quite a few applications such as the following CashRegister Class: A keypad might be a useful object in many different applications.
Source code: CashRegister.java
/** for (int i = 0; i < nButtons; i++ ) // for each label String command = e.getActionCommand(); // action string - set in init() // see if the character is part of a number: /** Exercises Hopefully, the above example raises a few possibilities in your mind - an ATM machine and a calculator are the obvious ones. You could also try to add a subtract option and perhaps a % key for calculating and adding sales tax. A more interesting and challenging task is to create a Keypad Class (easier with just the numeric keys and a decimal point) which has a method to return the number entered. Perhaps you can find a way to disable the decimal key when it has already been used once when entering a number.
import java.awt.*; /** public class WordSquare extends java.applet.Applet implements ActionListener // The set of allowable characters in the alphabet // When clicked, the square goes up one letter or down according to Button square [][] = new Button[ROWS][COLS]; // button grid /** // for each array element // take single letter from string of allowable characters increment.setEnabled(false); // disable the increment button at the start // check to see if we went out of range
Related: [ Java home | Previous: Chomp | Next: Parallel arrays ] |
2D arrays are not needed in the dossier but are on the syllabus so could be asked in an examination question.
|
|
|
|||
|
Questions or problems related to this web site should be addressed to Richard Jones who asserts his right to be identified as the author and owner of these materials - unless otherwise indicated. Please feel free to use the material presented here and to create links to it for non-commercial purposes; an acknowledgement of the source is required by the Creative Commons licence. Use of materials from this site is conditional upon your having read the additional terms of use on the about page and the Creative Commons Licence. View privacy policy. This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 2.5 License. © 2001 - 2009 Richard Jones, PO BOX 246, Cambridge, New Zealand; This page was last modified: May 31, 2009 |