Back Forum Reply New

First Time on java, for newbee

Post Last Edit by psv_jupiter at 18-3-2009 17:43

Post Last Edit by psv_jupiter at 18-3-2009 17:43

Post Last Edit by psv_jupiter at 18-3-2009 17:42

Post Last Edit by psv_jupiter at 18-3-2009 17:41

Post Last Edit by psv_jupiter at 14-3-2009 12:18

Post Last Edit by psv_jupiter at 14-3-2009 12:18

Post Last Edit by psv_jupiter at 14-3-2009 12:17

Post Last Edit by psv_jupiter at 14-3-2009 02:13

This post is for those who never know anything about java programming only.

Thing to take note.
1) To run java program you have to have JDK=Java Development Kit in your operating system.
to download JDK 1.6 click here
2) After you install JDK, you can either use IDE=integrated development environment to do your coding or use simple notepad program to type your code in. For me I highly recommend you use IDE.
There are quite a numbers of IDE,  such as NetBean, Esclipse, JGraps,........

For me, I personally recommend you to use Net Bean. The current most updated version of net bean is 6.7
to dl net bean 6.7 with java, click here

3. Run your first time program
Type this code and save with name "Welcome.java". Please take note that your file name and your class name must be the same. As in this case it is "Welcome".  what is class? class is the file you obtain when you compile your code. In addition to that, the class name should be in what it is liked  "Uppercase Letter every first word".
For e.g a class name might be : "BinarySearchTree". (Thank to ePrasart for reminding me  )

public class Welcome{
public static void main(String[] args){
System.out.println("Welcome to java");
}
}

If you use net bean, to run the program click button run or simply press Shift+F6
you can also compile first before you run , to compile click F9 however if you use netbean, or eclipse your syntax errors will be shown  clearly, no need to compile.

Another simple but yet show quite fun that will be posted here is : Guess.java that guess your birthday. Wanna know how it work? Copy and paste the following code and Run !

/*Author: Jupiter
*KhmerIT forum
*/
import javax.swing.JOptionPane;
public class Guess {
    public static void main(String[] args){
        String[] set=new String[5];
        set[0]="  1  3  5  7\n 9 11 13 15\n17 19 21 23\n25 27 29 31";
        set[1]="  2  3  6  7\n10 11 14 15\n18 19 22 23\n26 27 30 31";
        set[2]="  4  5  6  7\n12 13 14 15\n20 21 22 23\n28 29 30 31";
        set[3]="  8  9 10 11\n12 13 14 15\n24 25 26 27\n28 29 30 31";
        set[4]="16 17 18 19\n20 21 22 23\n24 25 26 27\n28 29 30 31";
        int result=0;
        for(int i=0;i<5;i++){
         int answer=JOptionPane.showConfirmDialog(null, "Is your birthdate in these number ?\n"+set [/**/i]);
         if(answer==JOptionPane.YES_OPTION) result+=(int)(Math.pow(2, i));
         if(answer==JOptionPane.CANCEL_OPTION) System.exit(0);
        }
        JOptionPane.showMessageDialog(null," Your Birday is : "+result+"!");
    }
}



To learn more about java programming visit Tutorial in sun java website. The link is : click here

Guess.zip (551 Bytes)

Guess.zip (626 Bytes)

good review , short tuterial.
Standard rule: class name should be upper camel case.
I hate this though. But it's good to follow the rule.
ePrasart.
Yeah, first letter of  word should be uppercase. . I edit it. Thanks.
Magic Algorithm!
ePrasart.
These would be the number that psv_juptier got from his code above:
I put them into two tables because one doesn't fit the screen.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

2^0=  1
1

3

5

7

9

11

13

15

2^1=
2


2
3


6
7


10
11


14
15

2^2=
4




4
5
6
7




12
13
14
15

2^3=
8








8
9
10
11
12
13
14
15

2^4=16



















16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
2^0=
1


17

19

21

23

25

27

29

31
2^1 =
2



18
19


22
23


26
27


30
31
2^2 =
4





20
21
22
23




28
29
30
31
2^3 =
8









24
25
26
27
28
29
30
31
2^4 =16
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31










To see how this work, let take a look at number 5 (red one) on the first table.
5 is 4 + 1.
So we need to put number 5 at the row of 4 (blue one) and 1 (blue one).
All other number use the same technique.

This remind me the time when I was young.
The fotune teller uses the 52 card to ask me to choose a card without telling her which one.
She ask me a few times just like this program and finally pick up the one that I have chosen.
It was so magic, but actually not.
ePrasart.
Yo you are correct. smart boy. ;) haha.

A simple explanation is from 1 to 31 or 0 to 31 there are 32 number = 2^5. Let represent it in base 2 as ABCDE which A,B,C,D,E is either 1 or 0. And each 5 question in the code ask you whether Each letter is 1 or 0 start from E.   
1 mean yes, 0 mean no.
if ans is yes, you add it with 2 the power of the it rank.

If anybody not clear abt my explanation let take fews example.
Suppose yr birthday is 18
18=16+2=10010 in base 2
The program start by asking you 5 questions to find ABCDE
1> If your bd in these number.. (set0) ?   You enter : No        then  result=0
2>If your bd in these number (set1)   ?  You hit : yes                then result=0+2=2
3>if your bd in these number (set2) ? you click : no              then the resullt is the same 2
4> If your bd in these number (set3) ? you press : No           result the same2
5> If your bd in these number (set4)? you say : Yes             result=2+16
Print your bd

How to obtain set0 ,?
Remember ABCDE
set 0 assume that E is 1.
it is actually the combination of 4 remaining digit ABCD    you have two choice each,1 or 0 so you have total of    2*2*2*2 =16 number
in this case
ABCDE=  (remember E is alway 1)
00001 00011 00101   00111 = 1 3 5 7
01001 01011 01101   01111 = 9 11 13 15
----------------------------
10001 10011 10101   10111 =17 19 21 23
11001 11011 11101   11111 = 25 27 29 31
If you ans yes that mean E=1 is true; other wise E =0

Same thing Set1, assume D is 1 and combination of other remaining 4 digit 16 numbers ....
Any doubt?
Back Forum