Monday, March 16, 2009

Shuffling numbers

One of the tiresome things about computers in general is the difficulty they have with recognising numbers. If you say to a child "add two and two" they will add the two numbers and give you four. If you say to the child "write two and two" they will write the string "two and two" or "2+2". If you subsequently asked them to solve, they will convert the string back to an equation and give you four again.

My first instinct after writing code which successfully shuffles strings was to extract numbers from them. Java has various methods for converting strings to numbers. A problem with all of them in my experience is that they are fussy about the string you pass to them for conversion. Accidentally appending non-numeric characters, like commas or brackets, usually generates exceptions. Coding to handle these exceptions is fraught with problems and messy.

After much thought I decided that rather than shuffling strings and converting them to number I would be better shuffling numbers, or more specifically number arrays. My code then became:

import java.util.*;

public class itemlist {
public static void main(String[] args) {
Integer[][] items = {{1,5,6},{5,2,7},{1,1,2}};
List<Integer[]> list = Arrays.asList(items);
Collections.shuffle(list);
System.out.println("First item: " + items[0][0]
+ " + " + items[0][1] + " = ");
System.out.println("Answer: " + items[0][2]);
}
}

It took a lot of fiddling to get this, not least because I didn't know how to assign the list type. Integer didn't work, Array didn't work, but Integer Array, written as Integer[], did.

Now I'll have to weave this into my app, and eventually perhaps post something on my web.

Saturday, March 14, 2009

Shuffling lists and arrays

My task is to add some elements to a list, shuffle it, and then generate a sublist.

The Collections Interface includes methods which can be used on all the subtypes of collection. Then each type of collection has some additional methods, specific to that type. Looking down the interface, I could use the add method to add elements one by one, the addall to add all the elements of another collection. The array operations include toArray, which presumably means putting elements in an array not taking them from it.

The Set Interface on first glance looks very similar to the generic one.

The List Interface has the sublist method but as with the others, the group add feature seems to require you to add from another collection. So how do you populate the first collection? The answer is given halfway down the page, and I'll quote from the page because there is nothing to be gained by paraphrasing:

"The Arrays class has a static factory method called asList, which allows an array to be viewed as a List. This method does not copy the array. Changes in the List write through to the array and vice versa."

This is very handy. Effectively it enables one to manipulate an array like a list collection. I applied it as follows:

import java.util.*;

public class jShuffle2 {
public static void main(String[] args) {
String[] items = {"1 + 5","5 + 2","1 + 1","2 + 5"};
List list = Arrays.asList(items);
Collections.shuffle(list);
System.out.println(list);
System.out.println("First item: " + items[0]);
}
}


I cheated a bit here because I borrowed code from the card shuffling class, which is fine as a demo of technique, but I will need to do more work because my app needs numbers as well as strings.

Monday, March 9, 2009

Algorithms versus arrays

I've often wondered why so many forum questions relate to poker games. It turns out that an example of how to use the List interface is shuffling cards and dealing a hand or hands from the deck. This is not only pleasingly clever, but it is also exactly the kind of operation I am trying to mimic. I am not shuffling and dealing cards but I am wanting to shuffle and deal from a collection of test items.

I am a bit embarrassed to admit that I am planning to use hard coded arrays to generate test items. The thing I boasted most about my school math software when I first produced it was that it used algorithms to generate test items rather that selecting from a finite list.

There are two problems with algorithms. One is the issue of repetition. The second is that of designing an algorithm to generate a precise level of difficulty.

When I was doing my research I collected a lot of data on which math questions children find easy. I even used Rasch methodology to quantify how easy they found them. What surprised me that the rules I had applied when I was writing my algorithms, to generate questions I expected to have various levels of difficulty, did not map reliably with those revealed by the results.

My current strategy is to rank the items generated by algorithms in my research according to their revealed level of difficulty, load them into collections, representing different levels of difficulty, and then use the methods of the List interface to generate sublists or arrays for use on the day.

Collections

This post is a bit of a non-sequitur. The thing is I posted a theory question and the reply took me back to a theory trail, which I had previously omitted.

The first odd this about this trail is that it has a by line, and none of the others do. What does that mean? I think it means it is esoteric - that it was added late by someone with a special interest in the subject. So what does it say?

I quote: "This chapter describes the Java Collections Framework. Here you will learn what collections are and how they can make your job easier and programs better." That sounds good. I posted the question because I had written some clumsy code in VB, and rather than just copying it out in Java, I wanted to use a different approach, and do the job better.

The second paragraph of the introduction says: "If you've used the Java programming language — or just about any other programming language — you're already familiar with collections." That seems a strange thing to put. I've used many languages to quite productive effect without ever having used "collections". And if I had, why would I be reading this chapter?

It's funny how particular words are used by many programming languages but are given different meanings in each one. The Mac used to have a product called HyperCard, which could be automated with a language called HyperTalk, which in turn was was loosely based on AppleTalk. One of the linguistic curiosities I liked about it was that what VB calls a variable and Java calls a field it called a container. I liked the idea of a variable being a container of a value, like a jug contains a quantity of milk.

Anyway, container is given a different meaning here: "A collection — sometimes called a container — is ... an object that groups multiple elements into a single unit". That sounds a bit like an array to me, but I won't complain, I'll read on.

We are next introduced to a Collections Framework, and we are told: "A collections framework is a unified architecture for representing and manipulating collections". We are also told: "Apart from the Java Collections Framework, the best-known examples of collections frameworks are the C++ Standard Template Library (STL) and Smalltalk's collection hierarchy". So it's not every language that uses collections, as was implied above, and it sounds like an esoteric area of those that do.

The first main lesson is called Interfaces. In previous lessons the Interface was a described as a variation on the class theme, and I raved on about how academic it all was from my perspective. Here we are told "The core collection interfaces encapsulate different types of collections" and these can be sets, lists, queues and so on. From the reply to my post it seems I an interested in the list, but I'll get to it in sequence.

There follow some definitions, including a better definition of a collection.

  • A collection represents a group of objects known as its elements.
  • The set is a collection that cannot contain duplicate elements, like a poker hand.
  • The list is an ordered collection (sometimes called a sequence). Lists can contain duplicate elements. The user of a List generally has precise control over where in the list each element is inserted and can access elements by their integer index (position).
  • The queue is a collection used to hold multiple elements prior to processing.
  • A sorted set is a Set that maintains its elements in ascending order.

Now I've been advised to use a list, but I this the sorted set might suit me better. I'll read on.

Thursday, March 5, 2009

A stopwatch timer

Of the three areas I need to research, a timer, dynamic graphics, and storing data, I shall begin by looking for a timer. I have seen reference to one but I can't remember where. I notice a link to search the tutorial in the top right hand corner of the page. I search "timers". I am led to a page on the Swing Timer, which begins by warning "Don't confuse Swing timers with the general-purpose timer facility that was added to the java.util package in release 1.3. This page describes only Swing timers." As it happens I am interested in "the general-purpose timer". It would be nice if there were a link on this text, but the is not, so I search "general-purpose timer", and guess what: I am led to the same text on the same page.

There is a link to the javax.swing.Timer) in the javax API so I follow that to look for clues. Certainly I can see where I might use it when I have my graphics going, but just now it is not what I want. So I reverse back into the API to look for the java.util.Timer. And this performs essentially the same function as the swing timer - it releases an event after a predefined interval. It's what mobile phones call a countdown timer. I'm looking for a stopwatch timer. So I search for stopwatch, but there are no matches.

In VB there is a timer object which can be made to work like a stopwatch. But an earlier incarnation of my app used a Mac tool called Hypercard. In that I used the system clock to measure the time elapsing between two events, simply by getting the system at the two events and measuring the difference.

When in doubt I go to this forum and I usually find an answer without posting a question. In this case it seems that I am not the only person in the world who has ever wanted to time anything but that other people seem to build their own timers. For my purposes, something along the following lines might work.

startTime = System.currentTimeMillis()
endTime = System.currentTimeMillis()
timeTaken =
endTime - startTime

It remains to be seen whether it actually does so.

Wednesday, March 4, 2009

Full Circle

My last entry took me full circle. I began this blog because I had written a few lines of code, but I had not read the instructions well enough to deploy either a stand alone application or an applet embedded in a web page. Now that I have a rudimentary applet running in a web page I am where I was when I began.

For the last few days I have been writing code again. There has been nothing to write about because there was no-one but myself to blame for any errors or confusion. I have had moments of frustration with NetBeans. It takes too much control. But for now the benefits outweigh the annoyances.

I have thought it might be nice to find a VB-Java dictionary, like a French-English dictionary, but then I took advantage of the NetBeans code writing aids to answer my questions without searching too far.

The question is where to go next in the Java Tutorial to make my Java applet more like Windows client-server application it sets out to mimic. There are three areas I need to research: a timer, dynamic graphics, and storing data. I am not quite sure where to begin. Certainly I shall be using the tutorial more as a reference document from now on, rather than reading it like a book.