Differences between the Textbook and using UI.

The course uses the UI class (part of the ecs100 library) to do all the input and output and also depends on using BlueJ. The textbook uses plain java. This means that most of the code examples in the textbook are a bit different from the course, and are also a bit more complicated. Here are some hints for understanding the text book and converting from textbook code to code using UI.

Text Output.

The textbook uses System.out.print(...) and System.out.println(...) These work exactly the same way as UI.print(...) and UI.println(...), except that the output ends up in a different window. You can simply replace System.out by UI in textbook code.

Text Input

The UI makes it much easier to read input from the user. The textbook uses a Scanner and System.in. To get input, the textput first creates a new Scanner(System.in), and then calls next(), nextInt(), and nextDouble() methods on the scanner. The UI acts like a scanner, so you can get the same effect as textbook code by ignoring the line with new Scanner(System.in); and calling the next... methods directly on UI.

Note that the textbook has no simple equivalent to the ask... method - it has to print out the question and then read the input value in two separate steps.

Calling methods directly, and the main method.

In standard java, the only way to run a program is for it to have a public static void main(String[] args) method. This method is what is called when you run a program. This is what the textbook does.

BlueJ makes it possible to create a new method directly, and to call a method directly on an object. This means that we don't need to use the confusing main method. Mostly, our programs will have a constructor that puts buttons on the window, and makes the buttons call the methods directly. This lets you make nicer programs very quickly and lets you test out your methods very easily.

However, you can use the textbook code directly. To run a textbook program using bluej, right click on the class (the box in the project window), and select the main method from the drop down menu. That will call the main method which will run the program.

Graphical output

Using graphical output with raw Java involves quite a lot of complication. The ecs100 library wraps up the complication and hides it. However, the actual drawing commands for the UI class are mostly the same as the drawing commands for raw Java. To use the textbook code, you can get rid of all the code dealing with JFrames and Canvas, etc, and just use the draw... methods directly on the UI object.

Buttons, mouse, etc

The UI object makes it so much easier to create buttons, sliders, etc and to use the mouse for input, that it is simply not worth worrying about how the textbook does it, unless you really want to know how do things without the ecs100 library. There is no simple way of converting the textbook code to UI code except to throw it away and start again.