![]() |
|
Nav-Bar |
April 1999 Tutorial |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
General Programming Principles
The easiest way to think of a program is as a list of instructions,
which are executed in order. Consider the following list of instructions:
To write a computer program you need to know the "commands" your computer will understand. For example, if you want to print something to the screen, it would be worth knowing that the java command for doing this is System.out.print("Message"); This will do as you expect. So a java program is a list of instructions, all carried out in order. There a commands to perform sums such as addition, multiplication etc., as well as commands for getting user input, drawing graphical interfaces, displaying pictures, and many other useful things. Types of Data One important aspect of programming is in using "variables". These are named pieces of memory that store particular data. For example, you might want to store an integer to use later. In Java, all you need for this is: int variableName; This says that the variable called "variableName" will store an integer. Note that CAPITALISATION IS IMPORTANT. VariableName is different to variableName which is different to variablename. Notice that we haven't said what the integer is. To actually define which integer "variableName" is to store, we just use the assignment operator which in java is the equals "=" sign. So: variableName = 10; specifies that "variableName" now stores the value 10. We can later
set it to a different value if we want to, in the same we did here. The
following table shows the other types of data we can store using the above
method:
Remember that capitalisation is important. So to set up a particular datatype you use the commands: long longValue;
You'll notice that I've set the value of "byteValue" at the same time as creating it. You'll do this quite a lot when programming - mainly because it cuts down on typing and is more elegant. Sometimes you CAN'T do this though - ie. when you don't know what value it's going to store yet! Notice that the 'String' type has a capital first letter where the others do not. This signifies it is in fact a CLASS (more on this later). In fact, the String type is quite special in Java. If you've programmed in C you may be surprised to find that the string can be set in the same way as the basic (numerical) types. That is: String name;
This will do as you expect, ie. it specifies that "name" will store a String, then sets it to David, then it creates a String called surname that stores the String Vivash. Notice the double quotes around "David" and "Vivash". These are very important, and are different to the single quotes - ' '. For now all you need to know is that your String values need to be surrounded with double quotes. Object-Oriented Programming You might wonder why I'm talking about Object-Oriented programming already when I haven't even explained "normal" programming yet... I haven't even told you how to write a "Hello World" program in Java yet :) Well bear with me and I'll tell you. The term "Object Oriented programming" may seem quite scary. In fact, it's just a certain way of programming - a way Java supports reasonably well. From it's name you might guess that it's something to do with programming with "Objects". And you'd be right to guess that. The main reason Object-Oriented scares people is because of the jargon used. I could probably dedicate an entire web site containing explanations on all of the jargon - and then another web site explaining all my definitions probably. Terms like "inheritance", "polymorphism" and "encapsulation" form the backbone of Object-Oriented programming languages, and can be a bit daunting to get hold of - but there's no reason you have to know their meanings to program in an Object-Oriented way. Every program you write in Java is an object (or more strictly speaking
a "class", but I'll explain this later). So what exactly is an "Object"?
Well, if you look around yourself now you'll be able to see lots of objects.
Around me now I see a coffee cup, a monitor, a keyboard, a carpet, a book,
a door and lots of other things. Every object has certain properties associated
to it - such as colour, size, age and other things. For example, my monitor
has the following properties:
So we have some functions that can be used to change properties, and some that tell us what certain properties are. This is what Object-Oriented programming is all about. It's about creating objects - which have certain properties, and then writing functions that can change these properties or just simply tell us what they are. Classes - The Object cookbook Still no actual programming eh? Well we'll get to that in a minute... I'm about to make an important distinction, so listen carefully. When you create a definition of an object - ie. what properties it's going to have and what functions there are going to be, this is called a "class". When you actually create an "instance" of this class, the thing you have created is an "object". So the class is the blueprint, and the object is the created article. It's possible to create many objects all from the same class. So you could have lots of monitor "objects" all created from the monitor "class". They would all contain their own properties and have access to the functions. The Java API (Application Programming Interface) has details of all of the classes that Java lets you use in its normal distribution. It's like a cookbook full of recipes. Every class is a recipe. When you make one of the recipes in the cookbook (or rather, when ou ask your mum to make you one of the recipes), it's like creating an object in Java. The recipe is the class, with the object being the created delicasy. How to write an Class This is the easy bit. Every program you write in Java is a class. Remember that when you compile your Java source code, a class file is created. Whether it is in fact a class in the sense of what has been described above is besides the point - a class is just a Java program. Look at the following: public class Monitor
public void setScreenWidth(int
width)
public int getScreenWidth()
The above is a class. It's incomplete, but it would work, trust me.
The first line asserts that we've created a class called "Monitor". The
five lines following that create a few properties - such as the serialNumber
and screenHeight. Notice that I've set them all to zero to begin with.
The first function above has the name "setScreenWidth", and has one
argument - "int width". This just means that the variable called "width"
will be an integer (we haven't set it to a value yet).
So we've defined a function that can be used by anyone who creates a Monitor object, which doesn't return any values but expects an integer to be sent to it as input. Contrast this to the second function. This one is public also, so anyone can use it. Where the previous function had "void" though, this function has "int". So whilst the last function didn't return anything (ie. no output), this function returns an integer. Notice, however, that the brackets after the function name are empty. This means that no input is expected to this function. So where the first function expected an integer, this one expects nothing. This class doesn't actually DO anything by itself though. Remember that a "class" is just a blueprint - a recipe. To actually USE these properties or functions we need to create an "instance" of the class - an Object. So now we'll actually write a program - one that uses this Monitor class. public class MonitorApplication
int wid1 = myMonitor.getScreenWidth();
System.out.print("Initial screen width=");
System.out.print("Final screen width=");
Okay, so this seems a little bit more complex. To get this program to work, you need to have a compiled the Monitor class first. To do this, just type in the source code given for the class "Monitor" and save it to a file called "Monitor.java" Then go to a command line and type in "javac Monitor.java" This will create the Monitor class for us, and we'll be free to use it as we have in the above program. So let's analyse this program. The first line is obvious - it just states that this is a public class (remember that all Java programs are "classes"), and that its name is MonitorApplication. One thing I didn't mention before is that the class name must also be the name of the file it's saved in. So we'd have to save the above program to a file called "MonitorApplication.java". To compile it you do the same as before, ie. "javac MonitorApplication.java" will compile the source code, then just type "java MonitorApplication" to run it. Analysing the Class Code You'll notice that the MonitorApplication "class" is pretty pathetic as classes go - I mean, there's no properties stored in it, and there's only one function (the function does create a few variables, as you can see, but these are only accessable inside the actual function). The function it does have is quite special though. The function definition is: public static void main(String args[]) Now, we've come across most of these keywords before. The first one,
"public", we already know just says that the function is accessable to
everyone. The second one's new though - "static". Don't worry about this
for now. Just know that if a function is called "main" as this one is,
you need the "static" keyword shoved in there.
In fact, the definition of the "main" function, shown in the line above will always be the same in your programs - so as long as you copy it as it is above, you shouldn't go far wrong. So why have we called it "main". Well, the "main" function is quite special in a class, because it means that the class can be "run" as a program. Remember that our Monitor class had only two functions and neither of these were "main" - this means it can't be run. When a program is run each line is executed one ofter the other. However,
any functions inside the program need to be explicitly "called". Look at
the Monitor class again:
So you see, going through these in order results in nothing being done.
The Java interpreter will see that you've got no "main" method so won't
bother trying to run the above program.
So lets have a look to see what MonitorApplication actually does:
So we seem to have done a lot of work to print out two lines of text, yeah? Well, okay, yes we have. But there's a lot of key concepts of Object Oriented programming demonstated above. I mean, even if you decide to give up now, at least you can say you've "done a bit" of Object Oriented programming now. The good thing about Object Oriented programming is that it's not as scary or difficult as some people would have you believe. All we really did above was define how a Monitor Object should behave (in the Monitor class), then we just created one and used it in our MonitorApplication class. And this is all Object Oriented programming is about - defining, creating, and using "Objects". There are a few more things to it than that, but this is the general idea. You may have noticed one odd thing about our MonitorApplication class - the "System.out" lines. These are Objects as I said, but we didn't explicitly create them like we did our myMonitor object. The reason for this is because they're automatically created for use when our program begins. There isn't really much you need to know about the "System.out" object at the moment, apart from knowing that you'll always be able to use it and two of the functions you can call from it are "print" and "println". That's all Folks Well, after possibly excessive use of pale shaded tables, this months tutorial is officially over. Have I gone too fast? Probably. Don't worry about writing you own classes just yet - if you can read and just about understand what I've done with my Monitor class you've done well. Remember that Java is an Object Oriented programming language, so if you want to program it well it's best to learn it in an Object Oriented way. Anyway, I'll leave you with this months exercises. Enjoy! Exercises
|
2-4-99