Dec 20, 2007

Would You Like Some Variables As Well?

On a quick note beforehand... how did you find the looping post? I may end up redoing it for you guys if you didn't "grok in fullness" because I may not have done it justice, the <> statements blogger really doesn't like and I had to rewrite the whole thing a couple times. Am searching for another method to show proper form currently (but for now examples won't be tabbed in).

Alright so we have used variables already but they weren't really explained in any sort of detail. Variables are a sort of container but they are picky containers. Tupperware with attitude. If you try to put something in it it doesn't like it spits it back out with a nasty error message. To declare a variable you must say what type it is and give it a name. To use a variable it must be initialized. So here is an example of a statement declaring a variable:

int x = 0;

The int is the type of variable x is.
x is the name of our variable.
= 0 initializes our variable.

We can also declare a variable and initialize it in two steps:

int x;
x = 0;

Now for our purposes at the moment our variables are going to go inside our main() method. This is for reasons that will be explained in upcoming lessons.

Now you aren't just going to want to use integers. You are going to want to use many different types of variables. Here is a list of the variables for you:

int, these are integers and can store positive and negative nondecimal numbers.
boolean, these are true and false operators and must be set to true or false.
byte, can hold values from -127 to 128 according to this site.
double, can hold decimal values.
long, can hold really long nondecimal numbers, kind of like a super integer.
char, can store characters like "j" or "a".
String, can hold a bunch of characters strung together, like "hello there" or "ass22d&&^(*".

There we are, toy with those at will, we'll get them out of the main() method next time.

Dec 19, 2007

Our Methods are Getting Loopy

Hello, the post may be a little messed up, blogger really doesn't like < pre >!

Today we are going to learn "loops". Loops are a way of repeating code over and over and over and so on... There are a couple types of loops in JAVA, "while", "do-while" and "for" loops. We will do "while" and the basic "for".

The while loop:
- while some condition is true the loop will run.

while (some condition) {
//code to run

An example of this:

while (x > 0) {
//code to run while x > 0

The for loop:
- this type of loop is for some code that you know needs to run a certain amount of times. These are basically a while loop with a sort of counter built in.

for (some variable; condition; increment/decrement) {
//code to run

An example of this:

for (int x = 0; x < 5 ; x = x + 1) {
//we declare a variable x and set it to zero
//more on declaring variables later
//this will run when x < 5
//x will go up by one each time the loop runs through

A couple of things we have to go over here:
1) The "//" indicates a comment. A comment is a piece of writing that java ignores. It just skips merrily along right over it. "//" is used for 1 line comments.

2) "/* your comments */" is for larger comments. All writing in between the /* */ is ignored.

3) Operators... the <, >, != and == are your basic operators. THERE ARE 2 EQUALS SIGNS THERE! This is on purpose, when comparing two values you use TWO EQUALS SIGNS! When setting something equal to another thing you use one equals sign. The != means not equal to.

4) You can also combine these operators, <= and >=, less than or equal to and greater than or equal to. For a complete list go here.

So now let's make a small program with some of the things we know. We'll make it display a message a certain amount of times.

public class Display {
public static void main(String[] args) {
for (int x = 0; x < 5; x = x + 1){
System.out.println("Counting: " + x);

The output to the command line for this will be:
Counting: 0
Counting: 1
Counting: 2
Counting: 3
Counting: 4

The x is incrementing so you will see it go up by one each time starting from 0. Notice the x = x + 1 only runs once the loop has gone through once and not immediatly.

The line:
System.out.println("Counting: " + x);
Notice that this is not:
System.out.print("Counting: " + x);

.print prints continuously on one line.
.println print to a new line each time.

Also in that line we have "Five times" + x
The "+" in this acts to combine the two for output. It concatenates it, joins them together.

The while loop itself is a bit simpler:

public class Display2{
public static void main(String[] args) {
int x = 0;
while (x < 5) {
x = x + 1;

This will give us an output of:

This is enough to soak in for one day. We'll start making some simple programs soon so we can get this deep in our heads.

Dec 18, 2007

An Iffy Brew

Hello everybody,

Today we make some decisions, well the computer will. We are going to do "if" tests(! These are core elements that allow computers to make decisions that are based on a condition.)

If's take the general form of...

If (argument) {
code to run if true

The argument can be many things, generally they compare two things but it can be a boolean (true or false variable).
You can add an else as well...

If (argument) {
code to run if true
} else {
code to run if false

So to try this out we'll make a quick program to check if a number we generate is greater than or less than fifty.

public class OddOrEven{
public static void main(String[] args) {

double z = Math.random() * 100;
if (z <>
System.out.print("Less than 50");
} else {
System.out.print("Greater than 50");

We have our class in blue because all programs need a class.
Then we have our main method, it is the method that runs first.
In purple we have our statements
double z = Math.random() * 100;
-"double z" declares a double(a decimal value that can hold more information then a single) named z
-"= Math.random() * 100 " generates a pseudorandom number from 0 to 1 (computers can have true randomness), by multiplying by 100 we get a number between 0 and 100.
In brown we have our if else block.

If statements can also be nested which means an if block within an if block.

if (argument) {
code to run if just the first one is true
if (argument) {
code to run if both are true

Why don't you try to make a program that checks if the integer z is > 50 and if it is check if it is less than 75 and if z <> 25.

Good luck!
My sample answer is here.

Dec 17, 2007

Your Basic Blend

Hello there!

Alright so now we get to some of the basic code structure of Java!

First we have the source file.
- Inside your source file you put your classes.
-Inside your classes you put your methods.
-Inside your classes you put your statements.

A source file is a file with a .java extension.
Then inside your source file you have (this would be in your text editor):
(P.S. these statements should be tabbed but blogger takes them out. I'll get on it)

public class Heater {
void heat() {

So let's make the traditional initiation program "Hello World".

We know it must have a source file, class(es) , method(s) and statement(s).

file (in text editor save it as a .java)

public class HelloWorld {
public static void main (String[] args) {
System.out.print("Hello World");

Alright let's break that down...

public class HelloWorld {
-the public means it can be accessed by all
-class means it is a class...
-HelloWorld is the name of the class
-you must open the class with a brace {

public static void main (String[] args) {
- once again public means it can be accessed by all (as opposed to private which we'll get to)
- static we'll do later
- void means it is a method with no return type
- main() is the name of the method, the main() method always runs first!
- (String[] args), this is a line that creates an "array" of strings and were calling that array args and is a parameter of main(). This allows for information to be entered into the program from the command line as far as I know.
- you must open the method with a brace {

System.out.print("Hello World");
-System.out.print puts the string(characters, letters, numbers strung together) that is in the parenthesis () and quotes "" into the command line.
- you must end a statement with a semicolon ;

Then you finish up by closing the method then the class.

Now we want to run this.
First we must compile this into a .class file.
Navigate to the folder your file is in in command prompt (unsure for mac)
Type dir to check that the .java file is in the directory.
Then now that we have the Javac command because of the last post type:

If there are no errors in your program this will compile. Otherwise your going to get some odd messages. I'll put some common errors and errors I made when I first did this.

- Java is case sensitive, capitols matter!
- All statements must end with a semicolon!
- Your string must be within quotes!
- You must open classes and methods with opening braces!
- You must close classes and methods with closing braces!

Alright so if you have your .class file now you can type (still in the same directory):
java HelloWorld
and voila! You get the uber-exiting message Hello World.

Now that you have that you can edit your original code to make that say whatever you want. If you want the message to appear on new lines you just have to put in another identical statement with a different string. Have fun!

Dec 16, 2007

The Basic Tools

Okay so lets get the stuff you'll need to run some Java programs!

Alright so first if your on Windows you'll need Java 2 Standard Edition SDK which can be downloaded here, just click on the one for JDK 6 Update 3. If your on a Mac you have it already.

When you are writing Java programs write them in a text editor that you can make the file a .java, the simplest ones like notepad are good.

You now need to set the PATH variable of your computer, follow this guide (windows) for that because I found it rather annoying. This is so you can use the Javac command in command prompt to run your program.

I have no clue how to change the PATH variable for Mac or if there even is a PATH variable but this may help because I believe that Mac runs on UNIX. Scroll down to Common Error Messages on UNIX Systems it has an explanation.

Now I find most of that annoying to the extreme but the books and tutorials I have read all say that you should start off running programs in the command line. Personally I think that you should download Eclipse. It is far easier to tell where you made errors and far easier to correct them resulting in faster learning I believe (if you can't find your mistakes and don't know what they are you can't correct them right?).

Here is a link to pretty much the ultimate reference guide for the Java API(explained later, bookmark this you'll need it eventually).

So now you have all that you will need.
Comment if you need help.