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 18, 2007
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() {
1stStatement;
2ndStatement;
}
}
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 HelloWorld.java (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:
javac HelloWorld.java
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!
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() {
1stStatement;
2ndStatement;
}
}
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 HelloWorld.java (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:
javac HelloWorld.java
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!
Labels:
classes,
Command Line,
compiling,
GreyM,
Hello World,
Javac,
main,
methods
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.
Bye
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.
Bye
Labels:
API,
Command Line,
Eclipse,
GreyM,
J2SE,
JAVA,
Javac,
PATH variable,
Starting Off
Subscribe to:
Posts (Atom)