Go Back   Gaming Gutter


» Site Navigation
» Home
» FAQ
» Log in
User Name:

Password:

Not a member yet?
Register Now!
» Advertisement
» Recent Threads
Go to first new post Time Travel
01-03-2009 04:54 PM
Last post by Hallandale
Today 05:42 PM
24 Replies, 151 Views
Go to first new post What's your favorite...
10-29-2008 03:47 PM
by beeger
Last post by Deadly_Sin
Today 05:39 PM
128 Replies, 897 Views
Go to first new post [Suggestion] Adults Only...
Yesterday 03:31 PM
by Howl
Last post by Hodizzle
Today 05:38 PM
41 Replies, 336 Views
Go to first new post What's your weather?
11-08-2008 04:43 PM
by reirei
Last post by Deadly_Sin
Today 05:38 PM
261 Replies, 1,653 Views
Go to first new post Have you ever played a...
10-31-2008 12:11 AM
Last post by Deadly_Sin
Today 05:37 PM
313 Replies, 2,113 Views
View Single Post

 Basic Java Tutorial
Old 11-14-2007, 10:38 PM   #1 (permalink)
FireWrath
Banned

Undisclosed FireWrath is offline
 
Join Date: Oct 2007
Posts: 3,210
GPoints: 129
iTrader: 1 / 100%
FireWrath - Total CelebrityFireWrath - Total CelebrityFireWrath - Total CelebrityFireWrath - Total CelebrityFireWrath - Total CelebrityFireWrath - Total Celebrity
Rep Power: 0
Basic Java Tutorial

Well I couldn't find a Java tutorial here, so I thought I'd throw mine at you.

Quote:
This is a tutorial on how to program, so I will just assume you already know what to do with the code, and how to compile it. I will first show you some code, and then break it down for easier understanding of it. With that being said:

Let's begin.

Code:
public class Name {
    public static void main(String[] ughs) {
        System.out.println("Hello World!"):
        }
}
Ah yes, the ever so famous Hello World program. What this does is it simply shows "Hello World!" without the quotes, of course.

The first line simply gives your class name, and in this case it's Name (duh). The second line is always included in programs. public means that the method could be accessed from outside of the class, but seeing how this is to be a simple tutorial, I won't go into detail about that. Another thing you want to keep in mind for more advanced programming is that word "void." It states that the method isn't supposed to return anything.

The third line is what makes the program print the two words. We have the quotes there to indicate that there are no variable (which will be covered later) used thereThe semi colon tells the program that the statement is finished. The last two letters "ln" before the opening parentheses stands for line. What that does is it makes sure that the next time the program prints on the next line, rather than continuing from where it was ended.

If we had something like

Code:
System.out.print("Hello");
System.out.print("World"!);
We would see HelloWorld! printed, but if stuck the ln to the end of print, we'd get

Quote:
Hello
World!
Seeing how the the the first quote indicates the begining of the text, and the second one indecates the end, if we wanted to stick quote(s) in the text, we would how to do it another way, but how? Using a combination of sybols what we called escape characters. The backslash "\" is what allows us to use the double quote. \' would insert a single quote, \" inserts a double quote, \n would end the line, so everything after would start on the next line, much like the "ln" mentioned above. \\ sticks a backslash, and \t is a tab (five spaces).

Now let's move to the variables (as promised).

So what is a variable, you ask? It's a memory location that stores a value. So let's look at some code now...

Code:
public class Name {
    public static void main(String[] ughs) {
        int number = 0, Number = 1;
        double nUmber = 2.01, nuMber = 3;
        int numBer = number + Number;
        int numbEr = number + nUmber;
        double numbeR = number + nuMber;
        }
}
The reason why I started the counting at 0 is because that's the way computers count. Anyway, Java is case sensitive, so if the variable name is the same, but the capitalization is different, that's ok; however, you cannot use the same variable name more than once. You cannot use certain words for variable names, and public, class, void, main, int, and double are just some of them if you needed an example.

You for variables there, the first two are ints, they are integers (whole numbers), where as double have decimals.

If you print number, you will see 0, whereas if you print out nUmber you will see 2.01. Even though I didn't end nuMber with a number, it will still print out 3.0. Since numbEr is an int, and there's a double in the equation, you can't run the program. NumBer, on the other hand, is an int, and so are the other two variables, so there's no problem. numbeR; however, is a double, so it will have no problem printing any kind of number, so in that case, it would print 3.0. If want to divide, you would use /, to subtract it's -, and to multiply use *. A not as well known character is the modulus operator, which is %. What that does is it finds the remained for an integer, and an integer only. Here's an example

Quote:
_2_ R:16
45 |100
-84
----
16
Note: assignment statements (those that use =) work from left to right, and even though that may not appear very important, it is in the more advanced coding.

Typecasting

This is where you change the type your variable is so the program can run without errors. We have agreed that

Code:
public class Name {
    public static void main(String[] ughs) {
        int number = 0, Number = 1;
        double nUmber = 2.01, nuMber = 3;
        int numBer = number + Number;
        int numbEr = number + nUmber;
        double numbeR = number + nuMber;
        }
}
Would cause an error if we tried printing numBer, right? If we tweaked numbEr just a bit to

Code:
int numbEr = number + (int) nUmber;
We could print out numbEr without an error and get 3 for our answer.

So now that you know how to print text with variable, and you know how to print text without, you may want to know how to do both, so here's an example for ya:

Code:
System.out.println("Number = " + Number);
The plus there tells the program to combine the text and the variable, otherwise it would expect to be ended, so you would get an error trying to run it.

String variables

First off, String veriable hold text values, much like ints and doubles hold number values, and declaring the String variables isn't much harder. Look for yourself

Code:
String word = "Hello World!";
The major difference is that strings have quotes around them. If you were to print word (the variable), then you would see Hello World!

String methods

Strings also have different methods, and to help you understand them, take a look at the word computer.

Code:
String s = computer;
Quote:
C o m p u t e r
0 1 2 3 4 5 6 7
That's how a computer would count the letters. With that being said, if you wanted to print the letter that is in the fourth slot, you would use:

Code:
System.out.println(s.charAt(2));
charAt is the method that figures out what letter is in what slot of the string (and keep in mind, since computers start counting from 0, C corresponds with 0, and r corresponds with 7).

Say you wanted to print the length of the string, you'd use

Code:
System.out.print(s.length());
Since we're not looking at the position of the slots of the characters, that would print the amount of characters, which 8.sub string (should be one word, but the forum doesn't allow it, so I added a space above and below) is the way to go.

Code:
System.out.println(s.sub string(4));
Once again we're going to character counting, so that would print uter. You can also specify where it end printing by adding a comma and a second number, like System.out.println(0,2); which would only print the first three characters.

Printing out the slot a character is at is not hard, just use the indexOf method.

Code:
System.outprint(s.indexOf("o"));
That would print out 1. Keep in mind that you can look for a phrase, a word, or a full blown sentence in your string; however, if what your looking for is not found, then it will print out -1.

If then else

This is simliar to the theory in math class "If p, then q." This has makes a decision based on what you're asking, and if it's true, it does one thing, otherwise it'll do something different. This may be complicated, so here's an example

Code:
int number = 1;
if (number < 0)
{
    System.out.println("Number is less than zero.");
}
else if (number == 0) { 
    System.out.println("Number is zero.");
}
else {
    System.out.println("Number greater than zero.");
}
This is pretty obvious, the first line of the if statement is what the program goes for, so if it's true, it'll stop by printing out Number is less than zero. If it's not true, it'll go down to the next step, the else if, and it does the same thing. If that's also false, finally we get to the else, that's saying if everything above is false, this is what must happen. Notice that the else if has two equals signs, that's because a single sign means you're giving the variable a value; therefore, you would use == so it wouldn't think that. When comparing strings; however, you would use the .equals method.

Code:
String word = "hello", Word = "world";
if (word.equals(world)
    System.out.println("word is the same as Word.");
else
    System.out.println("Word is no the same as Word.");
if (word.equals("hello")
    System.out.println("word is \'hello\'.");
else
    System.out.prtinln("word is \'"+word+"\'.");
Once again, since it's a string, different rules apply; however, when using that, notice that in the fist case there are no quotes, that's because we're
comparing word to another variable, and in the second one we're not, so therefore we add the quotes. You also may have noticed that there are not braces {} in the if's, that because if you have only one statement after following the if statement, then they are not needed; however, if you were to add another line, then you would need the braces.

For loops

Those are control structures which are used when you want to repeat something.

Code:
int count;
for(count = 1; count <= 5; count = count + 1)
{
    System.out.println(count);
}
So that first time count appears in the statement above is count given its value. The second determines when the loop stops (as soon as it's false). The third part is what happens to count when everything in the loop has happened, in this case after count has been printed. The output, which is what the user sees when something is printed, will be

Quote:
1
2
3
4
5
An easier way of having that third part of the for loop would be to use count+=1; When I told you that assignment statements worked from left to right, I wasn't kidding, and this is a simple example. It says the 1 is being added to count, and that's why when read right to left it makes a bit more sense. This way a lot more efficient because it saves you some time, especially if you're dealing with many for loops. Yet another easier way would be to use count++; (also works with --) but it only increases by one, whereas the += will add any number.

While loops

Very similar to for loops, and I perfer them over for loops, but it's your call what you use.

Code:
int count = 1;
while (count < 10)
{
    System.out.print(count);
    count++;
}
That will keep print out count until it's no longer less than ten, so the output would be 123456789.

You can also have nested loops, meaning a loop inside a loop, and here is an example

Code:
int count1, count2, sum;
for(count1 = 1; count1 <= 4; count1++)
{
    for (count2 = 1; count2 <= 5; count2++)
{
    sum = count1 + count2;
    System.out.print(sum + " ");
}
System.out.println();
The final output is

Quote:
2 3 4 5 6
3 4 5 6 7
4 5 6 7 8
5 6 7 8 9
And the same can be done with while loops, and if statements as well.

Allowing user input

User input is what the user types in something when asked by the program. This could be a word or number, doesn't matter, just depends on how the program is written. There are several way of doing this, but the method I use is by adding

Code:
import chn.util.*;
To the very top of the program, that will allow the next command to work, which is added after the equals sign instead of the value like above.

Code:
int num = input.readInt();
That will wait for a user to type in a number, but if you want the user to be aware of that, you may want to use

Code:
int num;
System.out.print("Num = ");
num = input.readInt();
Keep in mind that what goes after input.read varies on the type of variable you're looking at, since it's an int, I would add Int() to it, if it was a double, I would add Double(). These have to be capitalize, sine Java is canse sensitive. For strings; however, you would use input.readToken() instead of String.

Well I believe that I have covered enough for now, I may update this, but chances are I won't unless there's a mistake in there somewhere (other than typos, which I told tend to fix).
  Reply With Quote
 
Powered by vBadvanced CMPS v3.1.0

All times are GMT -7. The time now is 05:43 PM.


vBulletin skin developed by: eXtremepixels
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The contents of this webpage are copyright © 2006-2008 GamingGutter.com. All Rights Reserved.

Page generated in 0.16734099 seconds (100.00% PHP - 0% MySQL) with 17 queries