Variables
Weaker learners struggle with variables. Data types, declaration, initialization, local variables, scope of a variable, global variables, assigning a parameter to a variable that has been passed back by a method, and the properties of an object class (ie variables) are all concepts that cannot be rushed.
Gogga – it.jar
Gogga is your best friend (see Funworks text books). Sophisticated programming structures can all be introduced in a few weeks even to novice programmers using Gogga. Leaners love Gogga – trust me on this.
Primary storage. Teach persistence.
If a value becomes redundant, save it into a stand alone variable which you can overwrite with a fresh value when the old value is no longer useful eg total = total + score;
For values that are useful for that sesson only ie use an array (or a list array)
For values that have permanent value – Save to a text file or a database.
Strings
Strings are of infinite length – do not consider them to be the same as a field in a database.
We use the “+” character to add new characters to an existing String thereby creating something new e.g. a return value that encompasses all the fields in a record.
Because they are of infinite length you can add to them making them longer and longer. You can save a whole novel into a String, you would just need to use the escape character “\n” to create new lines.
When coding in Java think “String”. Most variables, most of the time should be String (even numbers). This allows us to use Java’s powerful String and character methods. When you need to do math or arithmetric you can convert a String back to a number e.g. “25” converts back to 25.
Phone numbers, ID numbers, postal codes, serial numbers etc should all be saved as Strings.
By using Strings to store numbers we can concatenate them together and pass “bundles of numbers” from one method (or class) to another (use a delimiter to separate values). When they arrive in the new method as a parameter we can then “unbundle” them and convert them back to numbers. An alternative to this is to pass an array of numbers if they are of the same datatype.
Temporary variables
Sometimes we make a duplicate copy of a variable, of an array, or even a record. That way we can work with the temporary copy first before we overwrite the original. Example: we do this when we swop two values around – we need the help of a temporary variable.
Sometimes we make a single stand-alone copy of a value or even a record. That way we can work with the single copy first before we copy it to its final destination.
Deleting
Deleting is overwriting – new data or code takes up the space that the old character occupied. Example: When we delete a record in a database a “hole” is left behind in the database structure. Then we use the compact tool and all the records are moved up to fill the holes left by records that have been deleted in the past.
Arrays
We have to know the size of an array to work with it – this is fundamental.
We can use a loop with “myArr.length” which will iterate from the beginning to the final index of the array regardless. NOTE: length is a property and not a method – therefore no round brackets.
We can use a loop with a previously coded counter which will iterate from the beginning to the counter size (even although the array may be a lot bigger)
Be able to – search, sort, delete, find duplicates, find maximum, find a minimum value and calculate the average. Also fill an array with default values (or random values), duplicate an array or combine two arrays together.
Arrays can be passed as a parameter to another method or class.
Know how to code an array literal e.g. int[ ] myArray = {2,4,6,8,10,12} ; NOTE semi colon at the end of the line. This is used together with myArray.length as there is no counter or null values inside the array.
Boolean – datatype
Boolean is exceedingly useful. Taught well, complicated tasks can be achieved by simply creating Boolean variables that store the current status of true or false. The examiner may have in mind a slick 3-line solution which you don’t know – but the addition of a few extra lines with a Boolean variable can solve the question just as well.
Secondary storage – irregular text files
Text files and the data stored in them can take many different forms and structures. See the dedicated post on “Reading from Irregular text files” for more.
Briefly . . .
- One record (field) per line – one line, no delimiter.
- One record with different fields per line – one line with a delimiter.
- One record with different fields each on their own line – no delimiter.
- One record with different fields – one line with a delimiter where the record is part of another and separate record structure.
- One record with different fields per line – one line where you have to use both the delimiter and other character handling methods to extract the individual fields.
- One record is an element of a parent class and the next record may be an element of the child class.
- Two or more different text files that need to be combined to create a full and complete record.
- Read from two different text files at the same time (therefore you need a different counter for each of them)
Defensive coding and data validation
To ensure good quality data. Important for students wanting a distinction as they need it for their PATs and Data Validation SBA task.
Records stored in text files and databases
Java is very poor at formatting records. Structured report layout is not part of the SAGS but is needed for the PAT). Therefore the number of records in a database should be kept to less than 15 and field lengths should be more or less the same length e.g. there is nothing to be gained in using long double-barrelled last names in your database alongside the name “Smit”.
GUIs and JFrameForm
Introduce slowly over two years, gradually building on previous skills, adding new components every time.
Students learn text fields easily but struggle with text areas – therefore teach them sooner rather than later.
Typed Methods
Typed methods can only return one value but what if you need to return 2 integers, one double, 3 strings and one Boolean for example? You merely concatenate them all into a single String with a delimiter between fields and return that.
Move away from as soon as possible . . .
* The keyboard as the principal input medium
Move away from the keyboard as soon as possible. Get learners to work with input stored in a text file or database.
* System.out.println as the principal output method
Move away from this as soon as possible – use it mainly for the bare-bones approach, troubleshooting and for exams that have tight time limits. Students must get used to reporting output using showMessageDialog and JFrameForm – this is important for their PATs.
* One class one method programs
Move away from this as soon as possible or it becomes entrenced as “the way to code”. One class with many static methods (early grade 10), and then programs with a UI class and a manager class ASAP.
* A linear main method that always executes everything top to bottom
Introduce event driven programs as soon as possible e.g. use JOptionPane and the switch-case statement to allow even novice learners to choose events off a simple menu structure. If you don’t do this they struggle to visualize their PAT as an event driven environment.