Wednesday, February 20, 2019

Lesson 1-Variable, data type, loop

Download Java to your computer, download Eclipse or Android Studio, if use Eclipse, you must choose Java 8.
Open Eclipse, go to File > New > Java Project
On pop up window, enter project name, finish.

Double click to project name, right click to src folder, New > Class
On pop up window, name it first, finish, screen will look like this

Copy these lines in to class bracket.
public static void main(String args[])
   {     
     String name="Hello World !";       
     System.out.println(name);
   }
Choose Run or press green button, if it show this pop up.

Check to Always save resources before launching, next time it will not show again.
Look in to Console screen below, it ‘s our first Java programme.

If not see Console, go to Window > Show View > Console to show it.

No need to see what public, static, void. They are Java keywords, just ignore them. The line Sysem.out.println to show some text to screen.
Short key for this command, type sys and press Ctrl+Space, pop up window show Commands, press Enter to choose.

Just look at the line: String name="Hello World !";
String mean data type of variabel name, it is all characters we type from keyboard.
Now change name to nam, Eclipse show an error.

The text name call variable, the line String name=”Hello World !”; declare a variable. When you change to nam, Eclipse doesn’t recognize the word name because we have to declare it before use.
Change name to nam, red mark will disappear.
Now change to String nam=”Nguyen Van A”, add this line
Int age=20;
Change System.out.prinlt to System.out.println( nam + " is "+ age + " years old!");
Run to see result.

Look at above picture, the line System.out.println( nam) now look different, it became comment.
If we have long and complex code, comment make us easy to remember what we has done yesterday.
The word int before age declare a integer number.
Int, String are data types, String is text, int mean number.
When we code String a=”8” it is a text, int a=8 it is number.
To change text to number, we use this
int a=Integer.parseInt(String);
On the contrary, to change number to text
String b=String.valueOf(number);
Now delete one bracket at last, an error show, add bracket to, it disappear. Programme must have close brackets the same with open. When see red mark, move cursor to it, Eclipse will show an notification.

Continue
String sex="Male";       
     if(sex.equals("Male")){
System.out.println( name + " is a son ");
}
We compare String sex with “Male”, to compare two strings, we must use .equals(), not == . If use ==, it still run but sometime it become error, result not true but you can’t find where. So always use .equals when compare to strings.
Make the line System.out.println( nam + " is "+ age + " years old!"); become comment.
Type int a=12; int b = 15;
age=a+b;
System.out.println(age);

Now screen show 27. On above it set age=20 but now it become 27 because we assign new value to it, age=a+b.
Computer read code from up to down, if we assign new value to variable, old value will lose.
Type continue to below age=a+b;
if(a>b){
               age=10;
          }
          else{
               age=100;
          }
Run to see number 100.
Now delete else command, run, Console show 27.
We see the different of if has else and no else.
If has else, when if not true, command go to else, value will be 100. I no else, age still remain value 27, value of a+b.

Continue to type.
for(int i=0;i<age;i++){
               if(i>10)
               System.out.println(i);   
          }        
     }   
Screen Console show number from 11 to 26.

For loop use to repeat a command, here we print number many time . Here we print number from 11-26, it is one command, run many time.
Change if to if(i%2==0), run, it will print even numbers from 0 to 27. % character mean divide and get remainder.

Continue.
double c=12.5;
double d=c/7;
System.out.println(d);

double mean decimal number.
Copy to above print command, error show.
DecimalFormat f = new DecimalFormat("#.##");

Press Ctrl+Shift+O to import library, it will show on top of programme. We don’t need to remember libraries, just import them when needed.

Copy to continue.
String round=f.format(d);
System.out.println(round);

Console print 1.71, DecimalFormat use to round decimal number. Change number repeat # character to 3,4 to get 3,4 number after comma.
Excercise

Print odd number from 1 to 100.

No comments:

Post a Comment