Sunday, 3 July 2011

Java: Off to a Good Start

I missed last week! But as of yet I have no followers, so I guess it doesn't matter. Moving into Java, since I have an actual basis for writing about it now!

Java is an object oriented programming language. This means that it focuses on using objects (duh) to create apps with. Abstraction is key. Abstraction is basically having a general idea of what constitutes some object in question. Let's use the popular chair example. Everyone knows what a chair is. It's got to have a back (Or its just a stool), a seat and legs (or wheels, just something to elevate it from the ground). Chairs differ greatly from one to the other, color, texture, number of legs, padded or not, adjustable or not etc. However, they're all chairs, and thus the idea of a chair is an abstract concept, as opposed to a specific instance of a chair, "That red school chair".

In much the same way, classes are snippets of code that define a specific object and its general components. These classes are then used to create specific instances of said object in the programmer's code, eliminating the need to create an object wholly from scratch each time you want to use it. Think mass production.

But we need to know how to write this code in the first place, so now we know what abstraction is, and the point of object oriented programming, we should start with the basics.

A program is written through a sequence of statements, composed of functions, classes and function calls. Each statement is further composed of expressions, which are like the building blocks of the language.

Objects and their functions are defined by their class. For example, a chess piece object would have a move function, defined in the general class for pieces. Or more specifically, assume this hypothetical piece class had a function named forward. This function would take no parameters and would simply move the selected piece forward 1 square. To do this, you would need:


"piece." informs the program which specific object to use the function on.
"forward()" informs the program which function is being used, and the empty brackets indicate that it takes no parameters.
";" The semicolon is used at the end of almost every statement, allowing the computer to know when it is complete. Not caring about whitespace, this is the only way to tell.

Suppose now you wanted the piece to move forward several squares, until it could move no further, using only the above function. It would be tedious to type out...


...especially since it would require a different amount of lines depending on where the piece starts, and therefore a different program based on the piece's starting position. We would much rather use an algorithm. In pseudocode (general purpose "coding" that hasn't been fleshed out):

while ( piece isn't at the endpoint )
move piece forward

As you may have guessed, there is a "while loop" present in Java (and a lot of other programming languages). This loop continually executes a statement, or series of statements while some given condition is true. Over each iteration of this loop, the condition is checked again to see if there has been a change.
For the sake of this exercise, assume there is a function available to the piece object, canMoveForward(), which returns a value of true or false depending on whether the piece can move forward or not. Then our loop will look like:

while ( piece.canMoveForward() ) {

Within the brackets directly after "while", we have the condition. In this case, if the piece can move forward. After the condition is an opened curly brace, this opens the set of statements for the while loop to enact if it's condition is true. This allows multiple lines with a clear cutoff point with a closed curly brace. On a newline (purely for aesthetic reasons, it's easier to read this way), we have the statement(s) to enact, in our case, move the piece. Note the semicolon to tell the computer where each individual statement ends. Also note that there is no semicolon after the while loop is closed. Simply closing this is enough to tell the computer that this is where the while statement ends.

Well, that's all for now, longer than I plan to normally do these, but I had to make up for missing a week.

1 comment:

  1. thanks, I read some other examples through the web and yours is very clear with the +1 movement to the chess piece.

    Keep it up!