Page 1 of 2 12 LastLast
Results 1 to 20 of 22

Thread: i need help

  1. #1
    leen's Avatar
    leen is offline Member
    Join Date
    Mar 2011
    Posts
    16
    Rep Power
    0

    Question i need help

    hi i need to help in explain this program
    line by line
    please



    Java Code:
    import java.lang.*;
     class RES {
     public static void main (String[] args){
     	 	A thra=new A();
     	B Obj=new B();
     	
     	System.out.println("End of main Thread ");
     }	
     	
     	}
     	class A extends Thread {
     		A(){super();
     		start();}
     		public void run (){
     			for (int i=0;i<=5;i++){
     				System.out.println("A ="+i);
     		}System.out.println("Exit from A");
    }
    }
    
    class B implements Runnable {
    Thread t;
    
    B(){t=new Thread (this,"B");
    t.start();}
     		public void run (){
     			
    for (int i=0;i<=5;i++){
    System.out.println("B ="+i);}
    System.out.println("Exit from B");
    }
    }

    output
    A =0
    End of main Thread
    A =1
    A =2
    B =0
    A =3
    B =1
    A =4
    B =2
    A =5
    B =3
    Exit from A
    B =4
    B =5
    Exit from B






    I CAN UNDERSTAND ANY THING
    :(:(:([/COLOR][/COLOR]
    Last edited by leen; 04-09-2011 at 03:15 PM.

  2. #2
    leen's Avatar
    leen is offline Member
    Join Date
    Mar 2011
    Posts
    16
    Rep Power
    0

    Wink

    the first line
    comment
    ithink that'

    but
    Java Code:
    Java.lang.*;
    for any type :confused::confused:
    Last edited by leen; 04-09-2011 at 03:16 PM.

  3. #3
    leen's Avatar
    leen is offline Member
    Join Date
    Mar 2011
    Posts
    16
    Rep Power
    0

    Smile

    Java Code:
    class RES {
    THE Name of class
    :rolleyes:
    Last edited by leen; 04-09-2011 at 03:16 PM.

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,508
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by leen View Post
    the first line
    comment
    ithink that'

    but
    Java Code:
    Java.lang.*;
    for any type :confused::confused:
    There is no need to import anything from the java.lang package, it is done implicitly for you and please don't use those nasty colours nor the huge font in your posts; it is hardly readable. Oh, and don't post the same question twice.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    leen's Avatar
    leen is offline Member
    Join Date
    Mar 2011
    Posts
    16
    Rep Power
    0

    Default

    please if any one is false
    correctD
    Last edited by leen; 04-09-2011 at 03:16 PM.

  6. #6
    leen's Avatar
    leen is offline Member
    Join Date
    Mar 2011
    Posts
    16
    Rep Power
    0

    Default

    There is no need to import anything from the java.lang package, it is done implicitly for you and please don't use those nasty colours nor the huge font in your posts; it is hardly readable. Oh, and don't post the same question twice.

    kind regards,
    ok
    thanx

    no problem i Chang the colour
    Last edited by leen; 04-09-2011 at 02:50 PM.

  7. #7
    leen's Avatar
    leen is offline Member
    Join Date
    Mar 2011
    Posts
    16
    Rep Power
    0

    Default

    Java Code:
     public static void main (String[] args){
    that is method or what:p:p

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    As the original question is not an "advanced java" type question, thread has been moved to the "new to java" forum.

  9. #9
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,508
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by leen View Post
    Java Code:
     public static void main (String[] args){
    that is method or what:p:p
    Please read a few tutorials first before you start asking these preliminary questions. This forum isn't well suited for tutoring.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  10. #10
    leen's Avatar
    leen is offline Member
    Join Date
    Mar 2011
    Posts
    16
    Rep Power
    0

    Default

    As the original question is not an "advanced java" type question, thread has been moved to the "new to java" forum.
    ok
    thankx

  11. #11
    leen's Avatar
    leen is offline Member
    Join Date
    Mar 2011
    Posts
    16
    Rep Power
    0

    Default

    Please read a few tutorials first before you start asking these preliminary questions. This forum isn't well suited for tutoring.


    _____
    okay :mad::mad:
    no problem

  12. #12
    leen's Avatar
    leen is offline Member
    Join Date
    Mar 2011
    Posts
    16
    Rep Power
    0

    Default

    i cant understand any things in this forums??????????????

  13. #13
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by leen View Post
    i cant understand any things in this forums??????????????
    You will understand them once you get a firm understanding of the basics of Java. I second Jos's recommendation that you first study the introduction to Java tutorials. I suggest that you start here: Trail: Learning the Java Language (The Java™ Tutorials)

  14. #14
    leen's Avatar
    leen is offline Member
    Join Date
    Mar 2011
    Posts
    16
    Rep Power
    0

    Default

    You will understand them once you get a firm understanding of the basics of Java. I second Jos's recommendation that you first study the introduction to Java tutorials. I suggest that you start here: Trail: Learning the Java Language (The Java™ Tutorials)
    thanks a lot

  15. #15
    leen's Avatar
    leen is offline Member
    Join Date
    Mar 2011
    Posts
    16
    Rep Power
    0

    Default

    The output of program

    How the program works
    In order to give this result
    A =0
    End of main Thread
    A =1
    A =2
    B =0
    A =3
    B =1
    A =4
    B =2
    A =5
    B =3
    Exit from A
    B =4
    B =5
    Exit from B
    l
    I did not understand anything in Java:(
    Therefore, future lost:eek:

  16. #16
    leen's Avatar
    leen is offline Member
    Join Date
    Mar 2011
    Posts
    16
    Rep Power
    0

    Default

    oh
    thank for u member
    for help??????????????????????????


    good luck
    I did not understand anything in Java:(
    Therefore, future lost:eek:

  17. #17
    noahwhygodwhy is offline Member
    Join Date
    Mar 2011
    Posts
    10
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    As the original question is not an "advanced java" type question, thread has been moved to the "new to java" forum.
    i was wondering about that

  18. #18
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Stop asking questions for EVERY step. People here will only help you once you give it a shot. Try your best to explain the program and see what you come up with.

  19. #19
    Maximus-EVG is offline Member
    Join Date
    Apr 2011
    Location
    Canada!
    Posts
    30
    Rep Power
    0

    Default

    I think I'll be ripped to pieces by every man here if I actually explain everything in the above program, and in this case it wont help you to understand. But I can give you some hints on HOW to understand.

    1. The program looks pretty complicated.
    2. Looks like there are three parts to it. Lets identify them...
    2.1 The program is composed of 3 classes, class RES, class class A extends Thread, and class B implements Runnable. So what the hell do they do?!
    3. To analyze,make the program simpler. Comment out everything from class A. (to comment out, add // in front of the whole blue block.
    4. Run the program again. Crap...compile error! If you use NetBeans/Eclipse, go to error line. If you run from command line, find the line of the error.
    5. Looks like its line 7. Something is happening on this line thats causing the compiler error.




    Java Code:
    import java.lang.*;
    
    [COLOR="Red"]class RES {
    
        public static void main(String[] args) {
            A thra = new A();
            B Obj = new B();
    
            System.out.println("End of main Thread ");
        }
    }[/COLOR]
    
    [COLOR="Blue"]class A extends Thread {
    
        A() {
            super();
            start();
        }
    
        public void run() {
            for (int i = 0; i <= 5; i++) {
                System.out.println("A =" + i);
            }
            System.out.println("Exit from A");
        }
    }[/COLOR]
    
    [COLOR="SeaGreen"]class B implements Runnable {
    
        Thread t;
    
        B() {
            t = new Thread(this, "B");
            t.start();
        }
    
        public void run() {
    
            for (int i = 0; i <= 5; i++) {
                System.out.println("B =" + i);
            }
            System.out.println("Exit from B");
        }
    }[/COLOR]
    6. By now you've read the Java Path chapter which someone already recommended to you, so you know what A thra = new A(); means.
    Or maybe not. It looks pretty complicated after all!!

    Java Code:
    [COLOR="Red"]A thra[/COLOR] [COLOR="Lime"]=[/COLOR] [COLOR="Orange"]new A()[/COLOR];
    So lets do what we did before and divide it up. First of all, its a Java statement, as it ends in a semicolon :)
    6.1Look at the red part. A thra. A reference called thra of type A is being declared.
    6.2Look at the orange part. A new object of type A is being created.
    6.3Look at the green part. A link is being established between the reference of part A and the new object of part A. But who cares?! We just commented out the whole class A so the compiler can't find it.
    6.4 So comment that line out as well, and run the program. What do we get?
    Java Code:
    End of main Thread 
    B =0
    B =1
    B =2
    B =3
    B =4
    B =5
    Exit from B
    7. So 5 lines are being printed. Each time the number is incremented by 1. Any idea where that happens (usually in a loop). Is there a loop left in the code which we didn't comment out?
    8. Its here, highlighted in red:
    Java Code:
    class B implements Runnable {
    
        Thread t;
    
        B() {
            t = new Thread(this, "B");
            t.start();
        }
    
        public void run() {
    
    [COLOR="Red"]        for (int i = 0; i <= 5; i++) {
                System.out.println("B =" + i);
            }[/COLOR]
            System.out.println("Exit from B");
        }
    }
    9. Hmm so whats happening? Clearly the loop is running, because we see output. Why is it running?! Well its in a run() method!! What does that mean?
    10. READ THIS. If you skip this, you wont understand whats happening next:
    Runnable (Java Platform SE 6)
    At least read the introductory paragraph. Do you see a connection now?
    11. class B implements Runnable. Is there a connection to what you just read in the link above? In short - class B is made to behave like a thread. Actually it has a thread in it! Thread t;
    12. Ok look here:
    Java Code:
        B() {
            t = new Thread(this, "B");
            t.start();
        }
    13. What's this block of code? If youve read the Java Path you should know. It looks like a method. Is it a method? It has no return type...hmm that is it does not go something like public returnObjectType methodName(ArgumentObjectType argumentToThisMethod);
    So its a constructor. What do contructors do? (Im not answering it this time!)
    14. You've somehow found out what constructors do. Is it connected to whats happening in the:
    Java Code:
    class RES {
    
        public static void main(String[] args) {
            A thra = new A();
            B Obj = new B();
    
            System.out.println("End of main Thread ");
        }
    }
    ??
    16. Look back at #12. Think back of #6. So looks like a new Thread object is being created, with this as the argument. This means the current instance of class B. Essentially Thread t now threads class B.
    17. Reread #10. Your class implements Runnable. What happens when you implement Runnable? Whats the point of doing it? If you implement Runnable then you must declare some specific method in your class. Hint: this method runs on t.start();
    18. Now you know what class B does. You should also know what class RES does by now. Its up to you to figure out what class A does.
    To read on threads, read here:
    Introduction to Java threads - JavaWorld

  20. #20
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    I know you started by saying you will probably have us yelling at you, but, be careful not to give him too much. There is a very good chance this is a homework assignment. I didn't read your entire post yet, and perhaps you didn't give him everything, but be careful not too.

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •