Programming 102b

There are a lot of specialized operators in a programming language. A lot more than I’m familiar with. Some of these are called ??Syntactical Sugar? as they combine two or more the functions of two or more operators in a single statement.

For instance, instead of writing

a = a * (b / PI);

you can write

a *= a / PI;

thus condensing the expression a bit (though not necessarily making it any clearer).

There are also increment and decrement operators that adds one or subtracts one from a variable, like these expressions:

a++ = a +1;
–a = a ?? 1;

The difference in whether the increment operator is put before or after the variable is that the incrementation takes place before (prefixed) or after (postfixed) assignment of a new value to that variable. Thus

b = a++;

is broken into two statements:

b = a;
a = a +1;

and the same with

b = ++a;

which is the same as writing

a = a + 1;
b = a;

Our lecturer noted that all of this is, indeed, just sugar coating, and we shouldn’t use it if it confused us. But of course, it does exude a certain level of intellectual coolness to be able to use them successfully.

This sort of incrementation is the basis for the name of the Object Oriented Programming language called C++. Basically, it’s the programming language C + 1. I understand that hackers enjoy this sort of wit.

When the computer determines the order in which to calculate an expression, it usually takes each operator from left to right. But it runs from left to right on several levels of precedence, depending on their operators.

These levels of precedence are (simplified):

1st level: expressions in ( ), ++ and ?? (postfix) (evaluated from left to right)
2nd level: unaries (eg. +2 or -3), ++ and — (prefix) (evaluated from right to left)
3rd level: * / % (evaluated from left to right)
4th level: + – (evaluated from left to right)
5th level: = += -= *= /= (evaluated from left to right)

This means that when the computer resolves an expression like this:

weight / (height*height) = bodyMassIndex;

it first calculates the expression in the parenthesis, then divides that expression before assigning the result to the variable bodyMassIndex.


Right, then. Enough theory. Here’s the source code for my first program. It is an applet – which means it is meant to run through a web page. Like this.

Basically all Java programs borrow bits of code from pre-defined libraries or packages, making the “standing on the shoulders of giants” line even more concrete as you use other people’s code as the basis for your own in order to avoid reinventing the wheel.

For applets, some of the most relevant packages are the “swing” and “awt” packages which contain code that allows you to draw geometric shapes in webpage.

Now, as I mentioned earlier, there are three kinds of Java programs – regular command line programs, GUI programs and applets. Each use different commands to achieve their effects. Applets and GUI programs are much better at handling graphics while command line programs are much better at handling text.

We started out with a simple applet to generate coloured circles in order to give us a clear feeling of making the computer do something with programmed commands. As the Assignment Sheet shows, we started out with a working program that drew a yellow circle. We should then modify the program to draw another, smaller circle on top of the yellow one, and calculate the area of both.

By simply reusing the commands already in the program and changing the parameters (sizes and colours), this proved fairly easy.

Following that, we were given a wide range of various small assignments to do. Such as this program which prints out


in just one statement by using the “\n” command to force a new line in the middle of a string.

But all of these programs were limited in that they couldn’t be altered when run, and that they couldn’t respond to changes or unforeseen circumstances. Which is where boolean values, loops and user input become central. More on this soon.


Add Yours →

Hi Andreas,

Your applet doesn’t draw circles for me. I tried Firefox and explorer on two different machines.


That is odd. It works for me.. I don’t know if it uses any special Java 1.5 stuff, but I shouldn’t think so.

Leave a Reply