View Single Post
Old 02-05-2004, 22:18   #1 (permalink)
Datapunter
Pro Punter
 
Datapunter's Avatar
 
Join Date: 23 Oct 2003
Location: Westdorpe
Age: 43
Posts: 5,530
Default Lesson 4 - Language, completing the basics

Lesson 4 ( Word document )

Punters Lounge JAVA programming course.
Lesson 4 - Learning the language, completing the basics.

Let's complete the basics of the language. I'm not going to re-write the on-line tutorial, which would take things too far for our basic course. I will simply reference the summary pages of each subject and highlight what's really needed for this basic course at this moment.

4.1 Object Oriented Programming

First there is the whole Object Oriented Programming. I think we've covered that sufficiently so far to give you an understanding of objects and how they apply in a program. What's important here is that objects are defined in a class. There are thousands of objects that can be used in Java, even more when using objects written by others. The standard objects can be found in an object library. For each object you get a description of its constructors, states and methods. For now just have a quick look to get a sense. At any time during the course you can use it to review the objects we'll be using.

java.sun.com/j2se/1.4.2/docs/api/overview-summary.html

Oh, and ROOKIE's, don't be overwhelmed by the size or quantity. It's just a matter of taking it all one step at a time.


4.2 Variables

Definition: A variable is an item of data named by an identifier.

Thats it, a cardboard box, you put something in and write a name on it. There are several types of variables just like there are cardboard boxes for different things. Most important for now is int, a numerical value. Summary:

java.sun.com/docs/books/tutorial/java/nutsandbolts/variablesummary.html


4.3 Operators

Definition: an operator performs a function on one or more operands.

Operators do something with data. In our case most important are the simple arithmic operators:
+ Add
- Subtract
/ Divide
* Multiply
Also important are the conditional operators that we use to examine the data and direct the flow of our program: (each operator returns true if its condition is true)
> Greater then
< Smaller then
>= Greater or equal then
<= Smaller or equal then
== Equal ?
!= not equal ?

java.sun.com/docs/books/tutorial/java/nutsandbolts/opsummary.html


4.4 Control Flow Statements

Control flow statements direct the flow of execution of your program. Sometimes the program must do things only if a condition is met. Or it must do things repetitively a number of times. Without these statements the program would simply run from top to bottom and then stop. Most important for now are the loop statements while, do while and for and the descision making statements if else and switch case.

java.sun.com/docs/books/tutorial/java/nutsandbolts/flowsummary.html

So far we have seen the if else statement. At this point we need to take a closer look at the other statements.




4.4.1 FOR

The for statement is used to execute a part of the program a number of times, again the actual number of times is determined by a condition.

for ( start ; end ; increments )
    {
      do this part of the program;
    }


start defines the starting point of the sequence
end defines when the for loop ends
increments defines the steps taken each time the for statement is done once


Most simple example is doing something a number of times:

for ( int i = 0 ; i < 10 ; i = i + 1 )
    {
      System.out.println("For is now done : " + i + "times.");
    }


This will display the text 10 times. It starts at i = 0, and is repeated as long as i < 10, each time the for loop is done i is incremented by 1. So 10 times in all.

Note that we start with i = 0,
then the end condition is checked and for continues as long as the end condition is true,
if the end condition is false for will loop once and after that the increment statement is done.
So if i = 9 for loops once more and then increments i to 10, not before.

You can make this as complicated as you want and the statements inside the for loop can change the ending condition. Keeping it simple is highly advised.


4.4.2 WHILE

while ( condition true )
    {
      do this part of the program;
    }


So as long as the condition is true do the statements between the pointed brackets.

int Bank = 5;
while ( bank > 0 )
    {
      System.out.println("Still money in the bank.");
      Bank = Bank - 1;
    }


Here the while statement checks if the Bank is greater than 0 and the text will be displayed 5 times. After that the Bank is less than 0 and the program will no longer do the part inside the while statement. In this case it is possible that the part inside the while statement, pointed brackets, is never done because the condition is checked first.


4.4.3 DO WHILE


In the case of the do while statement however the part between the pointed brackets is done at least once and then the condition to continue is checked.

do
    {
      do this part of the program;
    }
while ( condition is true );


Why the difference? Well the part inside the while statement, between the pointed brackets, could change the condition. In this case the Bank is lessened at least once and then repeatedly until it is 0.

int Bank = 5;
do
    {
      Bank = bank - 1;
      System,out.println("Bank = " + Bank);
    }
while ( Bank > 0 );




4.4.4 IF … ELSE


We have already seen this statement. At this point i would just like to point out that if else can be repeated like this so several conditions can be checked in one large if else statement.

If ( condition 1 true )

    {
      do this part of the program;
    }
    else if ( condition 2 true )
    {
      do this part of the program;
    }
    else if (condition 3 true )
    {
      do this part of the program;
    }
    else
    {
      do this part of the program;
    }



4.5 Generating Random numbers

Before we start the assignments here is a way of generating random numbers. Java knows a class called Random and in that class is a method called nextInt(). What you need to do is create a new object of the Random class and then you can generate random integer numbers in a given range. The range is determined by the argument you pass to the method. JAVA is extremely consistent. If you say nextInt(100) it will generate random numbers between 0 and 100 BUT it will generate THE SAME random numbers each time ! So to make our numbers truly random we use the milliseconds of the computers clock which is different each time we run the program.

int mynumber = 0;
Random numberx = new Random(System.currentTimeMillis());

mynumber = numberx.nextInt();


Will set mynumber to a value at random between -999999999 and +999999999.

To use the Random object you need to include its class. Java classes are organised in libraries to put it simple. When you wish to use an object that is not part of the basic classes library, you must include its library at the start of the program like this:

import java.util.*;

So the full program becomes:

import java.util.*;

public class Lesson4 {
public static void main(String[ ] arguments) {
    int mynumber = 0;
    Random numberx = new Random(System.currentTimeMillis());

    mynumber = numberx.nextInt();
    System.out.println(mynumber);
}
}


This is the full description of the Random object.
java.sun.com/j2se/1.4.2/docs/api/java/util/Random.html


Assignment 4.1 ( Lesson41.java )
Write a program that display’s random numbers as long as the numbers are less than 0.


Assignment 4.2 ( Lesson42.java )
Write a program that displays random numbers as long as the generated number is larger than the previously generated number.


Assignment 4.3 ( Lesson43.java )
Write a program that generates 10 random numbers and displays the word under if the number is negative and over if it is positive.


Assignment 4.4 ( Lesson44.java )
Go back to the program where we had a bank and a bet. Change it so a random number below 0 is a losing bet and a random number above 0 is a winning bet. Just one bet.


Assignment 4.5 ( Lesson45.java )
Expand previous assignment so you keep betting until you go bankrupt. And with a bit of luck this program will run forevever. In that case press pause or control-C.

It produces about the same amount below 0 as above, so odds under 2 should result in bankrupcy. And odds above 2 should make you a millionare. If you want to use decimal odds you need to use a double in stead of an int like this:

double Bank = 1000;
double Bet = 100;
double Odds = 1.5;


See how long you last at odds of 1.91.
And how long it takes to become a millionare at odds of 2.12

This last assignment you can take as far as you want. You could also generate the odds of the bet using random numbers. And if the bank doubles you could continue but with a changed stake. Have fun.
Datapunter is offline