View Full Version : Hey Any Java teacher's here?
Valkyrie
September 20th, 2011, 11:35 AM
Hey Well the title says pretty much what i want.
I have lots of free time to learn now (Not on chat lol)
But should I just get reading and find out myself? If so any recommendations?
Jufjufjuf
September 20th, 2011, 05:56 PM
Hey Well the title says pretty much what i want.
I have lots of free time to learn now (Not on chat lol)
But should I just get reading and find out myself? If so any recommendations?
I took a univesity course in Java, it was quite good and I have made an extremely, EXTREMELY basic (incomplete) tutorial to do basic things if you want it.
I found this book (http://www.goodreads.com/book/show/2111833.Head_First_Java) to be a great help!
TheMatrix
September 20th, 2011, 06:59 PM
Why Java? Perl is much easier to learn, IMHO.
See, they even have a bunch of tutorials and stuff: http://www.perl.org/learn.html
But hey, do whatever you want :)
Valkyrie
September 21st, 2011, 09:52 AM
Java offers alot of things and its like the most used so I thought it would be best for learning in my free time also I'm a fast leaner (I think)
Jaf I'll take your really really simple tutorial lol Just to see if I can do it
And I'll check out Perl and see what good things they can make with it lol
Mods! I Own you lol I beat u at 25 posts :P
Jufjufjuf
September 21st, 2011, 04:12 PM
Java offers alot of things and its like the most used so I thought it would be best for learning in my free time also I'm a fast leaner (I think)
Jaf I'll take your really really simple tutorial lol Just to see if I can do it
And I'll check out Perl and see what good things they can make with it lol
Mods! I Own you lol I beat u at 25 posts :P
Here's a link to the book I previously mentioned (http://blog.catzie.net/pdf-download-head-first-java-2nd-edition-by-katy-sierra-and-bert-bates/).
Don't worry this guide holds your hand and doesn't require any previous experience. Programmers, don't bother reading this, it's too easy :P
------------------------------------------------------------------------------------------------
This guide will assume that you have already installed the Java JDK according to Java Sun's instructions, and have set class paths.
Java -Your first Program-
In most languages the first program you will make outputs the words "Hello World". This is what you will learn how to do next.
class ExampleProgram {
public static void main(String[] args){
System.out.println("Hello World");
}
}
Enter that into a text document, save it as "ExampleProgram.java" (note that it is the same name as the one on line 1 but with a .java extension) and compile it using the commands.
cd C:\the\path\to_the\file\
javac ExampleProgram.java
When done, you should have a new file in the directory called ExampleProgram.class. This is what the computer reads. You can now run the .class file using the command.
java ExampleProgram
When executing a .class file you do not need to enter the extension.
What does each line really mean?
Line 1;
class ExampleProgram {
This line basically says that this file is called ExampleProgram.
Line 3;
public static void main(String[] args){
This line is where the main programs code is stored, it is also the first thing the computer looks for and runs in the program.
Line 5;
System.out.println("Hello World");
This line outputs the text hello world.
Note that
System.out.println("text here");
Will make it so that any other text you try to output will go onto a new line.
System.out.print("text here");
Will allow any other output to go on the same line.
Code:
}
}
These just to close the opening brackets made in lines 1 and 3. A simple rule is the every open bracket, should have a closing bracket.
-Storing values-
When storing a value, you must assign it a name, and a type. An example would be;
int hello = 1;
That would assign the name hello to an integer with the value of 1.
Types of values include;
int - Integer
float - Float
double - Double
long - Long
boolean - Boolean
String - String
char - Character
Int
An int is any number without a decimal point. int's do have limits on how large or small (negative) a number can be. Google it if you want to know the exact limit.
Float
A float is any number with a floating point (decimal) as with all types of values it has a limit. Google it.
Double
A double is very much like a float but allows you to have a more exact decimal place.
Long
A long is self explanatory; it cannot have a floating point (decimal). A Long much like an int but can have much larger values.
Boolean
A Boolean can only have two values, true or false.
String
Strings can include text and numbers but cannot be used normally for calculation. When assigning a value to a string you must enclose the value with quotes. EX;
String teststring = "Hello World";
char
A char can only hold one character and cannot be normally used for calculations. When assigning a value to a char you must enclose the value with apostrophes. EX;
char mychar = 'a';
Now what can we do with our new found knowledge of values? Well we can do calculations! EX;
int num1 = 5;
int num2 = 6;
int sum = num1 + num2;
Now looking at this code should be very self explanatory. It assigns the name num1 to an int with the value of 5. Then it names num2 to an int with the value of 6. Last it assigns the name sum to an int with the value of num1 plus num2. This means sum will have a value of 11.
When doing calculations you will use simple symbols, here's a list of the basic ones;
+ add
- subtract
* multiply
/ divide
% the remainder of the numbers divided.
Great! We can do calculations but how do we use them?
class Adding {
public static void main(String[] args){
int num1 = 5;
int num2 = 6;
int sum = num1 + num2;
System.out.println(sum);
}
}
Now save that as Adding.java and run the class file. As you will see it outputs the value of sum, which is the sum of num1 and num2 which is 11. The only new thing here is the way we output.
System.out.println(sum);
As you will notice we don't put quotation marks around sum. We do this because sum is asigned a value, it is an object that can change based on num1 and num2 or whatever we decide to chane the value to.
~Accepting Input~
Accepting Input is VERY simple, the only tricky part is when you want to use the inputs for calculations. First to interact in any way with the user we need to import the IO (Input Output) Class.
//Example
class Calculator{
import java.io.*; //This is the import.
public static void main(String[] args){
Next we need to initiate a “BufferedReader”, this allows the program to input text. When we initiate this we must put a try and catch bracket around it so that if anything goes wrong, the program doesn’t go ballistic. Then we need to start the BufferedReaders Readline command and assign a Strings value to it. Below is an Example.
class Calculator{ //Start Class
import java.io.*; //Import IO classes
public static void main(String[] args){
String num1s = "";// Will be assigned the user’s input.
try{ //Attempts to do the following.
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); // Initiate Buffer Reader
num1s = reader.readline(); //Tells the Buffered Reader to Read input from the user and assign it to the string ‘num1s’
} catch (IOException e) { // ‘Catches’ an input output problem if any.
e.printStackTrace(); //Prints the Error’s debug output
}
System.out.println(“You said” + num1s); // Outputs
}
}
Above should be quite self-explanatory. It read input from the user and outputs it.
Now for the ‘harder’ part. We know that we cannot use strings to perform mathematical calculations. So we must turn the string into an Int. Or whatever variable you decide. For this example I will use an int. To turn a string into an Int we must parse it using parseInt() from the pre-imported Integer class.
num1 = Integer.parseInt(num1s); //num1s is a string.
You must use this command in a try and catch brackets if you don’t want any problems. Also in the previous example when we were inputting strings we had the catch catch (IOException e) { we must add another catch incase the user does not enter a number. Using all you have learned I have put together a basic program that gets the sum of two numbers.
class Calculator{
import java.io.*;
public static void main(String[] args){
//Initiate Variables
int sum = 0;
int num1 = 0;
int num2 = 0;
String num1s = "";
String num2s = "";
try{ //Try
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));//Start input stream
System.put.println("Enter your first number: ");
num1s = reader.readline(); // Read the input store in string num1s
System.out.println("Enter you second nu mber: ");
num2s = reader.readline();
num1 = Integer.parseInt(num1s);
//Parse the strings and turn them into Ints
num2 = Integer.parseInt(num2s);
} catch (IOException e) { //Catch any input errors
System.out.println("An error has occured");
} catch (NumberFormatException e) { Catch any parsing errors
System.out.println("Input by user was not a number.");
}
sum = num1 + num2; //Add the numbers
System.out.println("The sum of " + num1 + " and " + num2 + " is " + sum);
//Output the sum ^^
}
}
Juf
This guide is not complete.
Any problems or questions, feel free to ask. (I can't PM yet.)
TheMatrix
September 21st, 2011, 07:31 PM
Java offers alot of things and its like the most used so I thought it would be best for learning in my free time also I'm a fast leaner (I think)
lolnope.jpg
Mods! I Own you lol I beat u at 25 posts :P
That's off-topic.
Jufjufjuf
September 21st, 2011, 07:49 PM
lolnope.jpg
That's off-topic.
Most used would have to be C/C++. Maybe OP should look into it?
Valkyrie
September 22nd, 2011, 10:37 AM
Yeah I already use C or C++ for some of the programs I make. I just want to make myself more Knowledgeable in the tech design. Thanks Juf for the tutorial, I'm going to try it now. Also I'm going to have a little trip to my local library, Wepii here I go! lol
vBulletin® v3.8.9, Copyright ©2000-2021, vBulletin Solutions, Inc.