JavaProgramming
Students will learn to program simple Applets using the open source IDE BlueJ and the Java SDK 2. |
This is even better than the last one baby! The late great Al "The Bear" Hite - Fried Hockey Boogie
This is how you feel after solving a programming problem - really, it's better than a MacDonald's (tm) hamburger.
Contents
Teacher Stuff
Students will find this unit easier if they have some previous experience with html, css and/or JavaScript. Coding requires self-discipline and patience in finding errors (debugging). Students can learn to be self-sufficient and treat difficulties as challenges.
Unit files (pdf format):
You will need the Java Development Kit and the BlueJ IDE for this course. Please see this page if you need instructions on how to get them. |
What is Programming?
Computers can be thought of as a huge collection of switches which can be on or off (in any one of two states). All the information presented by a computer is really stored in this form, known as binary machine code.
The binary number system is often used to describe the computer at this level.
A binary machine code program (or data stored in the computer might be represented like this:
- 0001010011100110
- 0001001100001111
- 1010001110101100
- 1001010000111011
This is tedious for humans to work with and they tend to make lots of mistakes when trying to do so. The first step for computer engineers was to make codes which could stand for binary instructions thus ADD might correspond to the code for adding two numbers together.
- GET 2309
- GET 2310
- ADD
- STO 2325
This language is also known as Assembly Language and it is still not very easy to handle so the next step was the development of High Level Languages like Java. One HLL statement usually translates into many machine code statements, you might see something like:
- total = number1 + number2;
Which could be the Java equivalent of the four assembly language statements given above. So computer programming is the business of writing code in one of these languages and then having another computer program translate that into binary machine code. This binary machine code can then run on the computer hardware.
An important result of this, from our point of view, is that computers do not understand anything that is not written according to the rules (syntax) of the Java language so the following will all cause an error of some kind:
- number1 + number2 = total;
- total = number1 + number2
- total = number 1 + number 2;
BlueJ is an IDE (integrated development environment) - a software application that helps you to write software applications (or computer programs). |
Your first program - as easy as 123 abc:
What it all means and see the Applet in action. You can also cut and paste the source code from here if you are not using BlueJ or do not wish to install the appletsimple template.
Explanation of the PressMe Applet code
Literals
Anything inside quotes "here is a literal" is literal - it will appear exactly as shown. You can include spaces. So you can change:
private TextField name = new TextField("Type your name here!");
to
private TextField name = new TextField(" Write your name ");
and it will still work.
Don't leave off the semi-colon that marks the end of the line(s) though.
Adding more objects
If you want to add more TextFields, Buttons or Labels to the GUI you can. You must add a new data member to define your new object:
private Button pressMeNot = new Button("Do not press me!");
and you must also add it in the init method:
add(pressMeNot);
and if it is a Button you must also add the actionListener:
pressMeNot.addActionListener(this);
Notice: There are rules for naming data members:
- They start with a letter, by convention in lowercase
- They have no spaces or special characters (but numbers are OK)
- They mustn't be a keyword that means something in Java (like Class void etc)
Notice that you don't have any way to know which button was pressed (yet).
Changing the properties of objects
All of the the things (TextField, Button. Label) which we have added to our Applet are object instances or just instances for short. All of these objects have properties stored in their own data members as defined in their own Classes. Eg, there is a Class TextField defined in Java.
One of the properties of TextField is the foreground colour; we can change that using a method called setForeground(Color), the colors are defined by another Class Color:
name.setForeground(Color.BLUE);
Add the above line to the init method of our Class PressMe.
More about syntax
About syntax errors and some common beginner mistakes. The rest of this course can be found on the website IB Computing under the ICT courses section.
|