Step 1.. Let's Start :)
The first thing we have to do when we want to write a program is to have some tools.. It's easy to do it.. So we need the java library , without it is imposible to compile a program.. The second thing we need is a good IDE.. There are many IDEs tha you can use like JeCreator , NetBeans , Eclipse , etc.. I'm used to NetBeans so if you choose to download an other there will be some differences about some steps ( but NOT on the code , it is the same for all IDEs ).. Where to download them?? Our friend Google will do that for as.. The java library is JDK and you can find it here http://www.oracle.com/index.html then click on downloads -> java SE -> download JDK.. It's a big archive so don't worry.. To download NetBeans is also easy.. So go here http://netbeans.org/downloads/ and choose one from Java SE , Java or All.. You have to be carefull , you can install NetBeans after finishing the JDK install , otherwise there will be no install.. That's we have to do on the frst step..
Step 2.. Some theory.... :/
So , while we wait to finish the install of JDK and of the IDE let's read some theory things that we will need.. First of all we have to understand the way that we have to talk to our computer. As I say many times "computer is the stubiest thing in the world, it does whatever you say to him" BUT you must learn the language that he speaks.. It's easy... First of all we speak with a simple way.. This that we say is just a sentence nothing less..
When the sentence ends then we must put the ';' symbol with witch the computer will understand that a sentence comes to its end.. Simple?? I hope so....
So we have some types of data :
Integer numbers :ex 1,2,-3,-10,2000.. when we want to create an integer number then we say that :
int a = 3 ; we save temporally the number '3' in the variable 'a'. So when we need again number three we will call variable a .
Float numbers : ex 1.0 , 1.4 , -5.6 ... when we want a float number then we just say : float a = 5.7f ; We use the f at the end because if we don't computer will think that is a double number.. For a comma we use '.' the dot , if you use comma(,) then we will have a red line under it that will say we do something wrong... DO NOT FORGET we must speak the java language as it is and not as we like it... :p
Double numbers : it's like the float number BUT it is much more accurate on the result.. When we need a number which has many digits after the comma then we need a double number.. How to create one ?? simple... double a = 5.7865 ;
Char : each character is a char... ex 'a' , '4' , ';' , '!' .. Whatever you see on your keyboard can be a char.. The only thing you have to do is to put it inside ' '.. We create it like that char a = '6' ; Be carefull variable a IS NOT a number, it is a character... :) you want some more to understand it ?? char a = 'b' ; I can name a variable the way i like(there are name that are not allowed , i suggest you to use simple names)..
String : Don't think strange... A string is many characters together.. We create them vary simple String a = "hello" ; Yes, we must write it with a big S an we must put the sentence in a " ".. Something more , String is not only a word but a sentence too.. ex String a = "Is java an easy language???? xaxa" ;
For now we will not use any other data types... We must first use them , play with them by writting programs... So are we ready to create out first program??
Step 3.. My first program :)
So, I think it's about time to begin writing in the IDE.. As I already told I use NetBeans , so excperience will not be the same with the other IDEs ( the code is the same , creating projects will not).. So let's open the IDE... Go to File -> New Project.. We will see a new window... In the left column you will see a folder named Java.. Choose it... Now on the right you will see what programs you can make.. We will use the "java Class Library".. Click next.. The first thing that you have to do is to give a name in your project.. Let's name it "HelloWorldApp".. Our project is ready.. On the left column you have some folders.. The folder "Source Packages" will contain all the source file.. Now it's empty, so let's create one... :)
Select again File-> New File.. A new window appears.. Select carefully the project that the file will join... We select the HelloWorldApp project... From the left column we select the java folder an from the right column we select the Java MAIN Class.. Each project must have at least one MAIN class.. When we test our program the computer will read the main class.. But be careful.. We can create many main classes in our project.. Each time we run it we select ONLY ONE.. So we have to understand that each program uses ONE main class.. So we choose Java Main Class and we press next... Now we have to give a name.. So let's call it HelloWorld. The project name can be the same with the main class name.. Click finish... Now we have some code in our HelloWorld Class...
public class HelloWord {
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here <-- That's the second way to write comments BUT only for one line }
}
System.out.print( " Hello World... " ) ;
What's that my God??????
Don't panic.. System.out.print( " my text " ) ; is one order to show something on the screen... It's not difficult..
Now press the green button witch is like play button.. You will see a windows witch tells you select the main class.. As we said before eatch time we can use only one main class... Select the HelloWorld and click OK..
On the bottom of our screen now we see this :
run:
Hello World...BUILD SUCCESSFUL (total time: 0 seconds)
I DON'T LIKE IT.... :p so let's make a small change... Let's write System.out.println("Hello World...") ;
Let's see the difference(click the run button)...
run:
Hello World...
BUILD SUCCESSFUL (total time: 0 seconds)
Now you understand that the 'ln' that we added changed the line... Is it any other way to change the line??
-Yes there is... Lets erase the 'ln' and write System.out.print("Hello World...\n") ;
Output.... The same we had before.. So if we want to write many lines there is no need to write many times the System.out.println order.... When we want to change the line we will use '\n' and everything will be ok... :)
That's all for now... :-) You can see the video with the HelloWorldApp (just change the resolution to hd)..
Step 4.. Let's play with numbers..
Programming is all about numbers... The logic that we have to follow is the same.. So here we will say some thing about them.. When we want to use numbers we have to be careful that the numbers are the same ex :
int a = 5 ;
float b = 5.7f ;
int c = a + b ; <-- this is wrong.. The accuracy of float is bigger that the integer.. So the result has to be float..
float c = a + b ; <-- it's ok!!
The same is for double numbers with float numbers.. the result has to be double..
Is it possible not to do that?? ex: is possible to do this : int c = a + b ;
YES it is.. We have to write it like that : int c = a + (int)b ; The result will be an integer number... Java knows that if we do something like that b will not be 5.7 but only 5.. We can play with numbers but personally I don't find a reason to create a big number and then force it to get smaller.. As I said before (int)b = 5 but b = 5.7 , if we want to use it again ,but this time as a float, then it will be 5.7 .. With that I want to say that (int) do not save 5 in the place of 5.7..
We can use these symbols : + , - , / , * , = ... These symbols that we already know... But we have to be careful.. When we have a math exam we write 5(3-6) = -15 .. Computer doesn't know that 5(3.. is a way to multiply 5 and 3 , so in java and other programming languages we have to write 5*(3-6) and the result will be the same... So let's make something big , something like 5-3*7.. Java knows what to do first so the result will be -16 and not 14.. :) If we want to have a 14 the we must write it like that (5-3)*7.. Also in java we can't write (5-3)*7 without saving somewhere, if we try it we will have a red line under it... So we have to say that
int a = (5-3)*7 ; OR System.out.print( (5-3)*7 ) ; if we want only to see it.. I think that we are ok with + , - , * but what about / ???
When we make a division we have to be careful.. Where are we supposed to save the result?? will it be an integer , a float or a double?? Personally I avoid float numbers.. So if we wont the result we will save it in a double and we will not have any problems.. ex double a = 5/7 ; Try to write this : int a = 5/7 ; There isn't any red line under it.. So is it write???? And the answer is : YES IT IS BUT the result is 0 !!!! So lets see that :
public static void main(String[] args) {
int a = 5/2 ; ------> a = 2
double b = 5%2 ; -------> b = 1
double c = 5.0/2.0 ; ------ > c =2.5
System.out.println(a+" "+b+" "+c);
}
So we can see that % give as the rest of a division.. I can't say that we will use this symbol too much , it depends if we want to create a calculator or a logistic program... So that's what we can say about numbers , open your IDE and try to test them.. If you just read this , then forget programming , only if you use your IDE you can learn.. Programming is not history!!!
Step 5.. System.out.print What?!?!
This is going to be a small step... When we make a program we want to see the output.. Is not good to create something that you cant see..! So our primary goal is to print something to our screen... That is possible with System.out.print() order...
But what are we supposed to put inside the ()??
As simple as we want it... We can put whatever we need... ex :
int a = 5 ;
System.out.print(a); ---> will print 5 !
String test = "hello" ;
System.out.print(test); ---> will print hello
But what if I need to print many sentences?????
int a = 2 ;
int b = 10 ;
System.out.print("In these "+ a + " houses live "+b+" persons!");--> will print In these 2 houses live 10 persons!
So we can see that when we want to unite many sentences there is no need to put many System.out.print() orders... With only some of them we can do our program.. Also in the () we can put whatever we want.. If we want a variable we can put it... If we want to add , divide etc something we can do it inside it... ex :
System.out,print(a+b); --> will print 12 AND NOT 210!!!! That is a good trap!! So be careful...
Let's see that one :
public static void main(String[] args) {
int a = 5 ;
int b = 3 ;
int c = 1 ;
System.out.println(a+b+" and "+c+a);
}
What you thing is going to be the result???
1) 53 and 15
2) 53 and 6
3) 8 and 15
4)8 and 6
To right is No3.. Why??? This is something that we can learn through practice.. BUT my advice is to avoid doing something like this in a System.out.print() order.. We should prefer "save" temporally the result in a variable and the print it!!
Step 6... ++ , -- , <= , == , >= , != , += , -= , true and false....
As you can see it's about time to say some words about the symbols you see above..
arithmetics :
++ : add 1
-- : minus 1
+= : adds the right member to the left,and saves it to the left..
-= : removes the right member from the left,and saves it to the left..
boolean :
<= : smaller or equal
>= : bigger or equal
== : equal
!= : different
true : believe me it's true
false : believe me it's false
So I have already told you about integers , floats , doubles , chars and Strings but there is also one more type that we will need to create a program.. That type of variable is called boolean and we create it like that :
boolean a = true ;
boolean b = false ;
We can say also that when a variable is 0 then it is false but if it isn't it's true.. The use of the boolean variables we will find it out when we speak about orders like if-else , while , for (this will be next step!!!)
So let's take a closer look at arithmetics...
public static void main(String[] args) {
int a = 1 ;
System.out.println("a :"+a);
int b = ++a ;
System.out.println("a :"+a+"---- b :"+b);
int c = a++ ;
System.out.println("a :"+a+"---- c :"+c);
int d = --a;
System.out.println("a :"+a+"---- d :"+d);
int e = a-- ;
System.out.println("a :"+a+"---- e :"+e);
int f = 6 ;
System.out.println("a :"+a+"---- f :"+f);
int g = 6 ;
System.out.println("a :"+a+"---- g :"+g);
f+=a ;
System.out.println("a :"+a+"---- f :"+f);
g-=a ;
System.out.println("a :"+a+"---- g :"+g);
}
What do you think that the program above is going to print?!?!?
int a = 1 ; // ok we know what it does , a = 1
int b = ++a ; // first we raise a , so a = a+1 which means a =2 and then we put it in b , so b = 2
int c = a++ ; // first we save a in c , which means c = a = 2 and then we raise a , so a = a+ 1 = 3
int d = --a ; // first we low a , so a = a-1 = 2 and then we but it in d , so d = 2
int e = a-- ; // first we save a in e , so e = a = 2 and then we low a , a=a-1 -> a = 1
f+=a ; // this means that f = f +a , so a = 1 and f = 7
g-=a ; // this is g = g -a , so a = 1 and g = 5
Lets see the output now.... :
a :1
a :2---- b :2
a :3---- c :2
a :2---- d :2
a :1---- e :2
a :1---- f :6
a :1---- g :6
a :1---- f :7
a :1---- g :5
OK!! This is a little difficult to understand in a big program , so we need some practice on that.. Never the less, I'm not any kind of mathematician , and I don't like them very much , which means that I avoid to use them in my programs!!
About the boolean that I mentioned before I will show you examples in next step.. So tuned on!!
Step 7.. Arrays in java world..!!
So , before we talk about the connection of boolean variables with if-else , while , do..while etc orders we must say how we use the arrays... He think that creating an array is simple and yes it is but we have to be careful how we use them.. There are many exceptions that we have to deal with and all the problems are not about some structure failure but a logical misunderstood..
What is an array or matrix???
It's a collection of variables...
What kind of variables???
Whatever, It depends only on us.. We can create an array of integers or floats or chars or string or whatever exists in java..
How do I create one??
Simple ex : int array[] = new int[5];
So we first say what kind of array we have , we are not able to have an array which contains more than one type of variable , so everything has to be the same type..
type name [] = new type[size] ; size>1 (if size=1 there is no reason to make an array , a simple variable has size 1)
What's the big idea with the arrays??? We can group variables of the same type in a context that we can read very easy...
BUT there only a small problem with the arrays... System has to know their size.. If we try to read something that is out of the bounds of the array then we will deal an exception!!
There is an other way to create an array when we don't know the size...
ex : int array = { 2 , 6 , 89 , -1 , 0 } ; In the first example we create an array of a specific size and when we want we put the index , in the second way we avoid to say the size but we insert the index of the array , so in the second ex the size is 5 !!
One more way....
int a = 10 ;
int array[] ;
array = new int[a];
That's ok too!!!
So, I create one array let's say int array[] = new int[5] ; How do I insert the variables in the array?!?!?!
Let's see , first we must get used at counting from 0 and not from 1... The seats in the array are 0 , 1, 2, ... , (size-1).. How to insert a variable??? array[0] = 7 ; array[1] = -1 ; etc...
How to print a variable from an array?! System.out.print(array[0]);
I think now we understood how an array is working (in a very general state)...
What will happen if in the example I said before we write array[5] = 7 ; ???
If you just write it nothing is going to happen BUT if you want to run the code then you will see something like that :
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 5
What is that?? Is this that we told before , when we play with arrays we may not see any errors (array[5]=7 ; is not wrong at the structure ) BUT we must take care about logical errors.. The array we have can see 5 seats which take numbers from 0 to 4 , at each seat we can put an integer that we like but we can't place more than that..
So can I see the size of an array that I don't know?? Let's see... ex : int array[] = { 3 , 7 , 0 , -3 , 6 , 4 , 2 } ;
We can see then and say that size = 7 , but what will we do if the size is more than 1.000.000....????
name.length ;shows us the size of the array , so if we write System.out.print(array.length()); the we will see the size of the array!!! At first we have to learn use the arrays with standar types like integers , doubles etc and then we will learn to use object arrays... Think about creating a library for your school , it's nothing more than a good array!!!
Step 8.. If-else , || , &&... and the booleans!!
So, this is a very nice chapter.. The orders that you see above are some orders that we see in every java program.. In a few words these orders are our everyday life.. So let's see what they do...
if :
syntax-- if(true) {do what i say; }
if(false) { you can't see me ; }
First of all it's the first time that we see {} inside the program.. { -> tells that something is starting and }->something is finishing..
if(true) : means that if this is true then do the {..} that follows.
if(false):means that if this is not true then the system can't see what is inside {...}..
Ex: int a = 5 ;
if(a<6) { System.out.ptint("number is smaller than 6");
if(a<0 { System.out.ptint("number is smaller than 0");
This that we will see in the screen is the first one...
Can I say if(a=6) ????
This is not right!! That's the reason that we have '=='.. If we won't to write it right we must do if(a==5){...}.
So things like == , <= , >= , != have a very big use because we need them to do tests in the code...
So now it's about time to mention that == , <= , >= , != are some kind of boolean .. I really don't know if in the java world this that I say is wrong but we have to thing that these are something that take price true or false, one each time.. So :
== : if the right member is exactly the same with the left one this is true , else it is false
<=: if the right member is bigger or the same with the left one this is true , else it is false
< : if the right member is bigger than the left one this is true , else it is false
>= : if the right member is smaller or the same with the left one this is true , else it is false
> : if the right member is smaller than the left one this is true , else it is false
Now I think that we understand it much more better... So I can use as many if as i like , I can use if inside an if... ex :
int a = 5
if (a < 10) {
//TODO code
if(a==5){
//TODO code
}
}
OK, but I want something more than that , I won't something to execute automatically some code if this that I say is wrong..!!
if(true or false){...} else {...}
If the parenthesis inside if is true else will not execute BUT if is false then else will execute..
ex:
int a =5;
if(a<=4){
System.out.print("4 is bigger or equal with"+a);
}
else {
System.out.print(a+" is bigger than 4");
}
Can I use else without if??
No,It's a syntax error , there will be a red line that will say "else without if" or something like that.. And If we think it why to use an else order without an if?!?! :/
Can I do many if...else??
Yes, as much as we want , and also we can do it like that:
if(boolean){...}
else if(boolean){...}
else if(boolean){....}
I don't suggest it because if we write some code today we must be able to understand tomorrow what we wrote.. I think that simple orders can do whatever we want if we use them with a clever way...
I forgot to mention what are || and &&... These are logical operators so
|| : means OR ex:
int a = 5 ;
if( (a< 5) || (a==5)) {do something} this is like writing if(a<=5) and we want to tell that if right ore left part is true ( just one of them,not both) then do what I say.
&& : means AND ex:
int a= 5;
if((a>0) && (a!=5)) {...} we want to tell that if a is bigger than 0 AND a is not 5 do what I say, we can see that this takes price false and if will not execute the code it would have inside..
So that's for now , I hope till night(Greece time) to upload the video....!!
5 comments:
practice your English boy first. ;)
agree with the above
these is so basic code..
get into some deep things..
do u really think that we dont know these?
I may not know good english(I'm ok with that),but everyone can understand what I'm saying, that's the point... :)
And about the second comment, I'll write about trees , stacks , list etc in a few weeks.. First we learn to walk,then we run....!!
+= : add 1
-= : minus 1
are u sure about this? :p GOD!!!
It was late at night, the analysis was right but this was wrong... Thanks for mentioning..
Post a Comment