Differences between the Textbook and using UI.
The course uses the
UI
class (part of the ecs100 library) to manage all input and output, and also assumes you are using BlueJ.
The textbook, on the other hand, 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
Scanner
, and
System.in
. To get input, textbook code first creates a
new Scanner(System.in)
object, and then calls
next()
,
nextInt()
, and
nextDouble()
methods on this scanner object. 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 textbook code has no simple equivalent to the
ask...
methods -- it has to print out the question and then read the input value in two separate steps.
Calling methods directly vs 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. Textbook code is based on this convention.
BlueJ makes it possible to create a new object directly, and to call a method directly on that object. This means that we don't need to use the
main
method. Mostly, our programs will have a
setupGUI
method 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. This will then call the
main
method which will run the program.
Graphical output
Using graphical output with plain Java involves quite a lot of complication. The
ecs100
library hides the respective complexity. 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.
The
UI
object makes it much easier to create buttons, sliders, etc. and to use the mouse for input, that it is simply not worth worrying about how textbook code has to achieve the same, unless you really want to know how do things without the
ecs100
library. There is no simple way of converting respective textbook code to
UI
code except to ignore it and start from scratch.
You may also want to consult another
comparison between UI-based and textbook-based code.