Lesson 2 ( Word document ) Punters Lounge JAVA programming course. Lesson 2 - Learning the language, a few calculations
Now we are going to start learning the JAVA language. For those of you in the ROOKIE class I suggest you follow my lead here. Feel free to have a look at the tutorials I will include for the SCHOLAR class but beware that for beginners the technical language used may give you a headache.
For those in the
SCHOLAR class you can use this tutorial:
java.sun.com/docs/books/tutorial/java/index.html
All I'm doing here is a simplified version of that tutorial. It all depends on your level and type of experience how fast you pick things up. Don't be afraid to change class, even I still get headaches from reading some of those tutorials.
2.1 Object Oriented Programming
What is Object Oriented Programming? Well first what does an object mean here? An object is simply the representation of a real-life object. You can look at the whole world as a collection of objects: the world, inside that are continents, cities, a house, and a table, right down to molecules and atoms. Each object consists of a collection of smaller objects. Objects are not limited to simple physical items but can get complex like a country or an army. Basically it is a way of looking at things. How does it relate to programming? Well it is simply a way of programming where the programmer chooses to look at the world as a collection of objects and then applies that way of looking at things to the programming itself.
There are two things you need to know about objects and that is that they have a state and behaviour. As an example lets look at a simple motorbike.
Our motorbike has a throttle and a brake. That's it. The throttle and the brake are called methods. The behaviour is throttle up, throttle down and brake. The state is the speed of the motorbike. You as a user use the methods of the object to change its state. So you use the throttle and brake to change the objects speed.
This is what you do in object oriented programming. You write a piece of software, consisting of objects that have methods and state. This short explanation is enough for now. It gives you an idea of what's involved. We will come back to the subject later.
2.2 Variables
In a Java program you use variables to store information. A variable is a container you put something in and you give the container a name. A bit like a card board box, you put something in and write on the box what's in it.
2.3 Operators
An operator is a command. You use an operator to tell the program to do something. The plus sign + is an operator. You use it to tell the program to add two values together.
Here are some simple mathematical operators.
+ addition
- subtraction
* multiplication
/ division
2.4 Control flow statements
Is a statement telling the program to proceed in a certain direction depending on a condition being met or not. For example the IF THEN statement. It reads like this:
IF (condition met) THEN (proceed here) ELSE (proceed here).
Or something like this:
IF (beerglas is empty) THEN (goto fridge and get more) ELSE (drink up).
Some conditional operators that can be used in the IF statement
> greater then
< less then
== equal
!= not equal
2.5 Example program
Lets look at a small program and see if we can identify all of the above and explain a bit more as we go along.
public class Lesson2 { public static void main(String[ ] arguments) { - int Bet = 100;
int Odds = 3;
int WinLose = 0;
if (WinLose == 0)
{
}
else
{
- Bank = (Bank - Bet) + (Bet * Odds);
}
System.out.println(Bank);
} }
First line
public class Lesson2 {
Second line
public static void main(String[ ] arguments) {
These first 2 lines create a class called Lesson2. A class is a definition of an object. In our case it's our entire program. The word public tells Java that it is a publicly accessible object. To be able to do anything our object must have at least one method and that is defined in the second line. The method is called
main and it is run automatically when the program is run. For now ignore
public static void and
(String[ ] args). That will be explained later in the course.
Lines 3 - 6
int Bank = 1000; int Bet = 100; int Odds = 3; int WinLose = 0;
Now these lines define 4 new objects. The type of object is int, short for integer; the names of the objects are Bank, Bet, Odds and WinLose. You could say these are 4 card board boxes big enough to contain an integer value and the names Bank, Bet, Odds and WinLose have been written on the boxes. The
= sign is used to assign values to the object. The
= sign is called an operator, it tells the computer to do something. The whole of a line is called a statement.
Note that you must end the line with a
; sign so Java knows where the statement ends.
Line 7
if (WinLose == 0)
Here we have an
IF THEN ELSE statement. Note the command is called IF THEN but we do not actually write the THEN word. The part between the round brackets is the condition we check. In this case we use the
== sign to check if the contents of WinLose is 0 or not. If it is 0, the condition is true and the program will proceed with the next line. If WinLose is not 0, the condition is false and the program will continue after the ELSE statement.
Line 8 – 10
{ Bank = Bank - Bet; }
Since WinLose is 0 that means out bet lost so we subtract the stake from the bank to get a new value for the bank.
Line 11
else
This is where the program continues if the condition after IF was not true.
Line 12 – 14
{ Bank = (Bank - Bet) + (Bet * Odds); }
Since WinLose was 0 this line is never executed. If you change the value of WinLose to 1 and compile and run the program again then this line will be executed. In this case we have a winning bet so we subtract the bet from the bank and then add the return from the bet back to the bank.
Line 15
System.out.println(Bank);
This line is the primary command for writing something to the screen and it prints the current contents of the bank.
Line 16 and 17
} }
Note now the usage of brackets. When we as human beings read a text we depend on punctuation, comma's, capital letters, paragraphs to give us an indication of how we should read the text. In case of a computer program you must not give the computer an indication, you have to tell it EXACTLY how to read the text or in this case the program.
The opening bracket of the first line has a closing bracket on the last line. The opening bracket on the second line has a closing bracket on the last but one line. All the actions done when the condition after the IF statement is met are enclosed in a pair of brackets. Just like all the actions done after the else part are enclosed in brackets.
These brackets tell the computer how the program must be read. Round brackets are part of statements or are used to group operators. Pointed brackets group pieces of commands and statements so they are executed as a block.
To run this program create a new file in the
C:\JAVA directory called
Lesson2.java. Make sure you get the upper and lower case characters the same. The name of the program must be the same as the name on the first line of the program. Type the program in as above or copy and paste. Compile the program using this command
C:\JAVA>JAVAC Lesson2.java and then run the program using
C:\JAVA>JAVA Lesson2
When typed as above you should get 900 on your screen. Change the value of WinLose to anything but 0 and you should get 1200.
Downloading the program is possible here :
Lesson2.java 2.6 Using a program editor.
So far you have been using a simple text editor. And there is nothing wrong with that, but there are more specialised editors about. One is
JCREATOR.
It can be downloaded for free from this site:
www.jcreator.com
goto the site, click on
download and select the second one called
JCreator LE version
(the professional version you have to pay for)
I am not recommending this specific editor and there are others about just as good, maybe even better, but it works for me.
What a program editor does is make life a lot easier by helping out in the writing of the program. For example this editor displays different elements in the program in different colours. Or when you move the cursor to a start bracket like ( { [ it automatically highlights the corresponding ending bracket. Saves a lot of aggrevation and searching for simple typing errors. Just download it and have a look at the programs we’ve made so far.
Check out the basic functions like:
File -
Open,
File -
Save,
Build –
Compile,
Build –
Execute file.
The rest you can look at later, these will do for now. You can check out the rest of the gooddies later.
It's important to note here that later in the course you will need to be able to run Java programs from within a msdos box. So the editor does not replace that, it only adds to it.
2.7 Adding comments to your program.
The problems with computer programs is that when you want to change or expand them after a while, you usually haven't got a clue anymore what you where thinking when you wrote it in the first place. This is why we add comments and documentation all over the place. You can add a single line of comments by starting a line with 2 backslashes, like this:
// all of this is commenting on the program
Or you can add a whole block of comments like this:
/* starting to comment * * i could write my life story here * * end the block of comments on the next line */ Assignment 2.1 ( Lesson21.java )
Play about a bit with this program. See what happens if a bracket is missing. Try and add some calculations. Or change the IF condition.
Assignment 2.2 ( Lesson22.java )
Change the program so that when WinLose is 0 then Bet loses, when WinLose is 1 the bet wins but when WinLose is anything more than 1 the bet is void. And have it display the correct Bank.
Assignment 2.3 ( Lesson23.java )
Add a variable called Winnings. Calculate the winnings (or losses) and at the end of the program display the Bank and the amount lost or won.