Results 1 to 15 of 15
  1. #1
    alwan2009 is offline Member
    Join Date
    Mar 2012
    Posts
    7
    Rep Power
    0

    Default factorial java thread problem

    hi
    I want help for code java:

    Create a class called Factorial that extents Thread Java class Add run method that prints thread number and the current Factorial evaluation


    In main method to the class and create three threads objects and run these threads.


    Note:

    Use volatile static variable, join method found the thread class and the join method needs an interrupted exception to work properly.

  2. #2
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default Re: factorial java thread problem

    So your project has outline this blank class:

    Java Code:
    class Factorial extends Thread {
    
        @Override
        public void run() {
            //prints thread number and the current Factorial evaluation 
            ...
        }
    
        public static void main(String[] args) {
            //creates and runs three threads
            ...
        }
    
    }
    In addition, you need to have something static and something volatile.

    Regarding static and volatile:
    The 'static' keyword makes the field or method declared 'static' shared by all objects of that class.
    The 'volatile' keyword makes the field or method declared 'volatile' shared by all threads of that object.

    Read:
    Development Chaos Theory » Blog Archive » How (not) to write Factorial in Java.
    Lesson: Concurrency (The Java™ Tutorials > Essential Classes)
    Thread (Java 2 Platform SE v1.4.2)

  3. #3
    alwan2009 is offline Member
    Join Date
    Mar 2012
    Posts
    7
    Rep Power
    0

    Default Re: factorial java thread problem

    thnx man
    Last edited by alwan2009; 03-17-2012 at 04:11 PM.

  4. #4
    alwan2009 is offline Member
    Join Date
    Mar 2012
    Posts
    7
    Rep Power
    0

    Default Re: factorial java thread problem

    I have problem with this code can you please correct it :
    class factorial extends Thread {
    static volatile int n ;
    public void run() {
    if (n == 0) return 1;
    return n * factorial(n-1); {

    }
    }
    public static void main(String[] args) {
    Count p = new Count();
    Count q = new Count();
    Count r = new Count();
    p.start ();
    q.start ();
    r.start ();
    try {
    p.join ();
    q.join ();
    r.join ():
    }
    catch (InterruptedException e) { }
    System.out.println ("The value of n is " + n);
    }
    }
    Last edited by alwan2009; 03-17-2012 at 04:13 PM.

  5. #5
    noobplus's Avatar
    noobplus is offline Senior Member
    Join Date
    Feb 2012
    Location
    chennai,south of India(south east of asia)
    Posts
    232
    Rep Power
    3

    Default Re: factorial java thread problem

    Quote Originally Posted by alwan2009 View Post
    I have problem with this code can you please correct it :
    class factorial extends Thread {
    static volatile int n ;
    public void run() {
    if (n == 0) return 1;
    return n * factorial(n-1); {

    }
    }
    public static void main(String[] args) {
    Count p = new Count();
    Count q = new Count();
    Count r = new Count();
    p.start ();
    q.start ();
    r.start ();
    try {
    p.join ();
    q.join ();
    r.join ():
    }
    catch (InterruptedException e) { }
    System.out.println ("The value of n is " + n);
    }
    }
    Java Code:
    public void run()
    your void method returning values

  6. #6
    noobplus's Avatar
    noobplus is offline Senior Member
    Join Date
    Feb 2012
    Location
    chennai,south of India(south east of asia)
    Posts
    232
    Rep Power
    3

    Default Re: factorial java thread problem

    Quote Originally Posted by alwan2009 View Post
    I have problem with this code can you please correct it :
    class factorial extends Thread {
    static volatile int n ;
    public void run() {
    if (n == 0) return 1;
    return n * factorial(n-1); {

    }
    }
    public static void main(String[] args) {
    Count p = new Count();
    Count q = new Count();
    Count r = new Count();
    p.start ();
    q.start ();
    r.start ();
    try {
    p.join ();
    q.join ();
    r.join ():
    }
    catch (InterruptedException e) { }
    System.out.println ("The value of n is " + n);
    }
    }
    Java Code:
    public void run() //"thread.run()" cannot return a value, and is always a void method
    {
    if (n == 0) System.out.println( "1");
    System.out.println( n * factorial(n-1)); //factorial() has same name as class, so ive added a new constructor of same type "factorial(int)"
    
    
    }
    private int factorial(int i) {
    		return i*(i-1);
    }
    regards
    dhilip

  7. #7
    alwan2009 is offline Member
    Join Date
    Mar 2012
    Posts
    7
    Rep Power
    0

    Default Re: factorial java thread problem

    thnx so it will be like this :
    class factorial extends Thread {
    static volatile int n ;
    public void run()
    {
    if (n == 0) System.out.println( "1");
    System.out.println( n * factorial(n-1)); //factorial() has same name as class, so ive added a new constructor of same type "factorial(int)"
    }
    private int factorial(int i) {
    return i*(i-1);

    }
    }
    public static void main(String[] args) {
    Count p = new Count();
    Count q = new Count();
    Count r = new Count();
    p.start ();
    q.start ();
    r.start ();
    try {
    p.join ();
    q.join ();
    r.join ():
    }
    catch (InterruptedException e) { }
    System.out.println ("The value of n is " + n);
    }
    }

  8. #8
    noobplus's Avatar
    noobplus is offline Senior Member
    Join Date
    Feb 2012
    Location
    chennai,south of India(south east of asia)
    Posts
    232
    Rep Power
    3

    Default Re: factorial java thread problem

    Quote Originally Posted by alwan2009 View Post
    thnx so it will be like this :
    class factorial extends Thread {
    static volatile int n ;
    public void run()
    {
    if (n == 0) System.out.println( "1");
    System.out.println( n * factorial(n-1)); //factorial() has same name as class, so ive added a new constructor of same type "factorial(int)"
    }
    private int factorial(int i) {
    return i*(i-1);

    }
    }
    public static void main(String[] args) {
    Count p = new Count();
    Count q = new Count();
    Count r = new Count();
    p.start ();
    q.start ();
    r.start ();
    try {
    p.join ();
    q.join ();
    r.join ():
    }
    catch (InterruptedException e) { }
    System.out.println ("The value of n is " + n);
    }
    }
    what is the packaged used by "Count()"? I mean what import we need?
    Could u give me the full package used by count
    (count is not a java package class)

    regards
    dhilip

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

    Default Re: factorial java thread problem

    Quote Originally Posted by alwan2009 View Post
    thnx so it will be like this :
    Java Code:
    private int factorial(int i) {       
     return i*(i-1); 
    }
    That factorial implementation just plain wrong; don't trust everything they write on the net. (the rest of the code is rubbish too).

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  10. #10
    alwan2009 is offline Member
    Join Date
    Mar 2012
    Posts
    7
    Rep Power
    0

    Default Re: factorial java thread problem

    this is for :Listing 2.4. A Java program for the counting algorithm
    1 class Count extends Thread {
    2 static volatile int n = 0;
    3 public void run() {
    4 int temp;
    5 for (int i = 0; i < 10; i ++) {
    6 temp = n;
    7 n = temp + 1;
    8 }
    9 }
    10 public static void main(String[] args) {
    11 Count p = new Count();
    12 Count q = new Count();
    13 p.start ();
    14 q.start ();
    15 try {
    16 p.join ();
    17 q.join ();
    18 }
    19 catch (InterruptedException e) { }
    20 System.out.println ("The value of n is " + n);
    21 }
    22 }


    I need for factorial help plz

  11. #11
    noobplus's Avatar
    noobplus is offline Senior Member
    Join Date
    Feb 2012
    Location
    chennai,south of India(south east of asia)
    Posts
    232
    Rep Power
    3

    Default Re: factorial java thread problem

    Quote Originally Posted by JosAH View Post
    That factorial implementation just plain wrong; don't trust everything they write on the net. (the rest of the code is rubbish too).

    kind regards,

    Jos
    sorry
    I was creating constructor and not factorial
    my Constructor had return type sorry for that..

    Java Code:
    int fact=1;
    public factorial(int i) {      
    for(int j=i;j>=1;j--) {
    	
    	
    	fact=fact*j;
    	
    	
    }
    System.out.println(fact);
    
    }
    regards
    dhilip

  12. #12
    noobplus's Avatar
    noobplus is offline Senior Member
    Join Date
    Feb 2012
    Location
    chennai,south of India(south east of asia)
    Posts
    232
    Rep Power
    3

    Default Re: factorial java thread problem

    [ spoonfeeding removed ]
    Last edited by JosAH; 03-17-2012 at 06:21 PM.

  13. #13
    alwan2009 is offline Member
    Join Date
    Mar 2012
    Posts
    7
    Rep Power
    0

    Default Re: factorial java thread problem

    upppppppppppppppppp

  14. #14
    alwan2009 is offline Member
    Join Date
    Mar 2012
    Posts
    7
    Rep Power
    0

    Default Re: factorial java thread problem

    upppppppppppppppppppppp

  15. #15
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default Re: factorial java thread problem

    There's a few problems with this:
    Java Code:
    private int factorial(int i) {
            return i*(i-1);
    }
    1. If the input is 0, it should return 1 (in maths, 0! is 1).

    2. If the input is a negative number, what will happen?

    This should only work for positive numbers so you can either convert a negative int to positive one or simply reject it.

    3. This is not a factorial calculation!!!! 5! = 5x4x3x2x1 = 120
    If I put 5 in your program I only get 5*4 = 20.

Similar Threads

  1. Factorial in java
    By spidey32 in forum New To Java
    Replies: 18
    Last Post: 04-09-2011, 01:04 PM
  2. factorial sum in java
    By java157 in forum New To Java
    Replies: 9
    Last Post: 03-17-2011, 10:07 AM
  3. Factorial
    By Anindo in forum New To Java
    Replies: 4
    Last Post: 07-28-2009, 09:46 PM
  4. Problem in Thread wakeup of Java NIO Client
    By sranil in forum Networking
    Replies: 0
    Last Post: 04-05-2009, 07:42 PM

Posting Permissions

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