Thursday, January 29, 2009

Friday, Jan 30th , Fun with for loops: part the Second

OK, up until now, we've been working strictly with applications, and very simple ones too: no added bells or whistles. This will now change, and here are the differences:



1) we will be creating an applet, which needs a webpage, which TextPad creates for us (you'll see)



2) we will be doing some "import" statements. This means we will be bringing in some pre-defined classes from other parts of JAVA



3) we will use those pre-defined classes to create objects



4) we will "instantiate" those objects by naming them



OK, let's get right to the program; here it is:



import java.applet.*;

import java.awt.Graphics;
public class FirstFor extends Applet

{

public void paint( Graphics g )

{

for (int i=1; i < 100; i++)
g.drawLine( 10, 10, 250, 10 + i * 10 );

} //end for loop

}//end paint
}//end class FirstFor


Save it, properly name it, then compile it


Then, run this applet by hitting Ctrl + 3, then clicking OK


Alrighty then, now for the explanation...

Cheers,

Uncle Paulie

Tuesday, January 27, 2009

Friday, Jan 30th , Fun with for loops: part the First

Last semester I showed you how to create a for loop in Visual Basic. It works in a pretty similar fashion in JAVA: you can use it to do the same action over and over, no matter what that action might be. Here is an example of how this works:




public class ForPrint

{

public static void main( String args[ ] )

{

for (int i=1; i < 100; i++)
{

System.out.println( "Hello World!" );

}

//end for

}//end main
}//end class ForPrint






You will notice that we are already starting to use a lot of "{" and "}" This can get confusing, ESPECIALLY if you simply type through a program from top to bottom. Here are two techniques that can help you with this right from the get go:

A) always type in your curly braces FIRST, then go back and type in the code that goes between them



B) If you get confused as to which "{" goes with which "}", then you can use this trick in TextPad --> highlight a curly brace, then use the Ctrl + M keyboard shortcut. It will go to the other curly brace that is associated with the one you just used. If it doesn't go to the curly brace you thought it was supposed to go to, you've messed up somewhere, and you either have too many of them or too few.



Cheers,

Uncle Paulie

Monday, January 26, 2009

a new line on console printing

OK, so in the last application, you created a Java program that output one line of text to a screen. In this program, you learn to create multiple lines by using the newline character --> "\n"

The newline character forces the cursor inside the system console to go down to the next line. This can be useful if you want to display a column of number values, rather than have them all strung together on the same line.

You will use the newline character in conjunction with a little device called a concatenator, which looks like a "+" sign. This concatenator can be used to glue together several different strings into one. It's also a cheap knock off way of converting a number in Java into text: anytime you have a String on either side of a concatenator, the whole thing automatically becomes a String.

Anyway, here is the program:

public class HelloPrime
{
public static void main( String args[] )
{
System.out.println( "1"
+ "\n" + "2"
+ "\n" + "3"
+ "\n" + "5"
+ "\n" + "7"
+ "\n" + "11"
+ "\n" + "13"
+ "\n" + "17"
);
}
}

Friday, January 23, 2009

JAVA, day One Tuesday January 27th

Greetings, loyal code monkees!


OK, so today is a very big day in the history of this class: our first day of JAVA programming! Before we do anything, you all should probably make sure of these three things:



A) JAVA FOLDER that you have a folder somewhere on your desktop or in your My Documents folder thats named after yourself, plus the word JAVA. Please do NOT create it in the H drive. If you do, and you have problems, they become YOUR problems.



B) TEXTPAD Make sure that you have a shortcut to the TextPad program, and that it starts when you click on the shortcut. TextPad is the IDE (integrated Development environment) that we will be using, so its important that your copy works.



C) JAVA sdk Make sure that TextPad is linked to JAVA properly. There are two things you have to do regarding that:



1) Make sure that when you click on the TOOLS menu item in TextPad, that you either see a submenu that says "External Tools" or you have the External Tools listed directly. These tools are as follows and I quote:



Compile Java Ctrl + 1

Run Java Application Ctrl + 2

Run Java Applet Ctrl + 3



2) Make sure that these tools actually work by writing, saving, compiling and running a small sample program. Here is what you do:



in TextPad, type in the following:



// the year’s first Java program

// first name last name



public class HelloWorld

{
public static void main( String args[] )
{
System.out.println( "Hello World!" );
} //end main

} //end class



SAVE the program as a Java file in your Java folder.

Make sure the file is named EXACTLY the same as the name of the public class


Then, compile your program by hitting Ctrl + 1, or by clicking on the menu item.

If this goes correctly, you will get a message "Tool completed successfully"


Also, you will notice that there is now a second file in your folder. It has a .class extension



Then, run this program as an application by hitting Ctrl + 2 (You can always tell its an application and not an applet by the fact that it has a main method.)



If it works, then rejoice and be of good cheer. You've just taken your first step into a larger universe



Mr. L