Results 1 to 17 of 17

Thread: What are they?

  1. #1
    scarymovie is offline Member
    Join Date
    Mar 2009
    Posts
    12
    Rep Power
    0

    Default What are they?

    I am asked to identify the following java programs, whether they are inherently concurrent, potentially concurrent or not concurrent. I need some help on identifying


    please view picture @ img140.imageshack.us/my.php?image=java.jpg


    I have really bad problem defining concurrency in java. Thanks for helping.

  2. #2
    emceenugget is offline Senior Member
    Join Date
    Sep 2008
    Posts
    564
    Rep Power
    7

    Default

    please post only one topic.

    generally speaking, concurrency is the use of threads or processes to allow parallel programming, that is, have multiple commands running at the same time. if you have threads A & B running concurrently (at the same time), then parts of A may execute, then parts of B, then A may finish, then B may finish.

  3. #3
    azzaiel's Avatar
    azzaiel is offline Member
    Join Date
    Feb 2009
    Posts
    57
    Rep Power
    0

    Default

    Answer for problem b

    Java Code:
    		BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
    		
    		System.out.println("Enter the integer: ");
    		
    		String sNumber = null;
    		int number = 0;
    		try {
    			sNumber = in.readLine();
    			if (sNumber != null) {
    				number = Integer.parseInt(sNumber);
    			}
    			
    		} catch (IOException e) {
    			e.printStackTrace();
    		} catch (NumberFormatException e) {
    			e.printStackTrace();
    		}
    		
    		System.out.println("The factors of " + sNumber + " are: ");
    		
    		while (number > 1) {
    			number /= 2;
    			System.out.println(number);
    		}

  4. #4
    azzaiel's Avatar
    azzaiel is offline Member
    Join Date
    Feb 2009
    Posts
    57
    Rep Power
    0

    Default

    using problem B solution as your guide you can solve problem A!! so good luck

  5. #5
    scarymovie is offline Member
    Join Date
    Mar 2009
    Posts
    12
    Rep Power
    0

    Default

    But the 2 java programs don't look like they are concurrent. correct me if i'm wrong.thanks.
    Last edited by scarymovie; 03-02-2009 at 04:44 AM.

  6. #6
    azzaiel's Avatar
    azzaiel is offline Member
    Join Date
    Feb 2009
    Posts
    57
    Rep Power
    0

    Default

    fine here's the code i will spoon feed you for now hahahaha but try to understand the solution i suggest reading java head first if your are really new to java (well actually im also new to java :p )

    Java Code:
    BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
    
    		String tempNumber = null;
    		int number1 = 0;
    		int number2 = 0;
    
    		try {
    			System.out.println("Enter the first integer: ");
    			tempNumber = in.readLine();
    			if (tempNumber != null) {
    				number1 = Integer.parseInt(tempNumber);
    			}
    
    			System.out.println("Enter the second integer: ");
    			tempNumber = in.readLine();
    			if (tempNumber != null) {
    				number2 = Integer.parseInt(tempNumber);
    			}
    
    		} catch (IOException e) {
    			e.printStackTrace();
    		} catch (NumberFormatException e) {
    			e.printStackTrace();
    		}
    
    		int result = 1;
    		int divisor = 0;
    
    		if (number1 > number2) {
    			divisor = number1;
    		} else {
    			divisor = number2;
    		}
    		
    
    		while (result != 0) {
    			result = (number1 % divisor) + (number2 % divisor);
    			divisor -= 1;
    		}
    
    		System.out.println("The Greatest common divsor of " + number1 + " and "
    				+ number2 + " is " + (divisor + 1));

  7. #7
    scarymovie is offline Member
    Join Date
    Mar 2009
    Posts
    12
    Rep Power
    0

    Default

    Quote Originally Posted by azzaiel View Post
    using problem B solution as your guide you can solve problem A!! so good luck
    Azzaiel, thanks. erm. but I the question wants me to identify if each of the program is inherently concurrent, potentially concurrent or not concurrent. I personally feel that A is not concurrent as well as B. at least they dun look concurrent to me.

    Besides that, for program B, I wrote the code:

    import java.util.Scanner;

    class factors {

    public static void main (String[] args) {

    Scanner scan = new Scanner(System.in);
    int number, halfx = 1;

    System.out.print ("Enter the integer: ");
    System.out.flush ();
    number = scan.nextInt();

    while (halfx <= (number/2)) {
    if (number%halfx == 0)
    System.out.println ("the factors of " + number + " are ");
    \\????
    halfx = halfx + 1;
    }
    }
    }



    I am stuck at the \\????. Please advise.

  8. #8
    emceenugget is offline Senior Member
    Join Date
    Sep 2008
    Posts
    564
    Rep Power
    7

    Default

    many loops can be run with parallel threads. here's a basic example for a search that runs through an unsorted list that can be split up into multiple threads:

    Java Code:
    private int[] arr;
    
    int search(int value) {
       for(int i=0; i<arr.length; i++) }
          if(arr[i] == value) {
             return i;
          }
       }
       return -1;
    }
    this can be run concurrently with the loop split into threads a and b as follows (with necessary code changes depending on requirements):

    thread a
    Java Code:
    for(int i=0; i<arr.length/2; i++) {
       if(arr[i] == value) {
          return i;
       }
    }
    thread b
    Java Code:
    for(int i=arr.length/2; i<arr.length; i++) {
       if(arr[i] == value) {
          return i;
       }
    }

  9. #9
    scarymovie is offline Member
    Join Date
    Mar 2009
    Posts
    12
    Rep Power
    0

    Default

    I am still trying to figure out how to complete that program. This is my code,

    import java.util.Scanner;

    class factors
    {

    public static void main (String[] args)
    {

    Scanner scan = new Scanner(System.in);
    int integer, halfx = 1;

    System.out.print ("Enter the integer: ");
    System.out.flush ();
    integer = scan.nextInt();

    System.out.print ("The factors of " + integer + " are:" );

    while (halfx <= (integer/2))
    {
    if (integer%halfx == 0)

    System.out.println ( halfx);
    halfx = 1 + halfx;
    }

    }
    }


    But it didn't work the way I want it. This is what I got,

    Enter the integer: 128
    The factors of 128 are:1
    2
    4
    8
    16
    32
    64


    instead of,

    Enter the integer: 128
    The factors of 128 are:
    64
    32
    16
    8
    4
    2
    1

    Any idea?

  10. #10
    azzaiel's Avatar
    azzaiel is offline Member
    Join Date
    Feb 2009
    Posts
    57
    Rep Power
    0

    Default

    Java Code:
    while (halfx <= (integer/2))
    {
    if (integer%halfx == 0)
    
    System.out.println ( halfx);
    halfx = 1 + halfx;
    }
    
    }
    Replace that with this:

    Java Code:
    integer /= 2;
    		while (integer >= 1)
    		{
    			System.out.println(integer);
    			integer /= 2;
    		}

  11. #11
    scarymovie is offline Member
    Join Date
    Mar 2009
    Posts
    12
    Rep Power
    0

    Default

    Thanks... can you explain please? I am just beginning to learn java from reading and practising. And also, for the System.out.print, why is it appeared to be

    Enter the integer: 128
    The factors of 128 are:64 (notice "are:64")
    32
    16
    8
    4
    2
    1

    How to make the output to be in a new line?

  12. #12
    azzaiel's Avatar
    azzaiel is offline Member
    Join Date
    Feb 2009
    Posts
    57
    Rep Power
    0

    Default

    Java Code:
    int integer, halfx = 1;
    
    while (halfx <= (integer/2)) 
    		{
    		if (integer%halfx == 0)
    
    		System.out.println ( halfx);
    		halfx = 1 + halfx;
    		}
    
    		}
    first of all your loop is wrong
    the condion is while (halfx < = 65) (w/ will loop 65 times)
    and please use proper if statmes w/ curry braces

    if (condition) {
    process
    }

    you are printing halfx w/ will go from 1 to 64

  13. #13
    azzaiel's Avatar
    azzaiel is offline Member
    Join Date
    Feb 2009
    Posts
    57
    Rep Power
    0

    Default

    what do you mean new line?

  14. #14
    scarymovie is offline Member
    Join Date
    Mar 2009
    Posts
    12
    Rep Power
    0

    Default

    Quote Originally Posted by azzaiel View Post
    what do you mean new line?
    I mean the output is shown

    The factors of 128 are:64
    32
    16
    8
    4
    2
    1

    and I want it to be

    The factors of 128 are:
    64
    32
    16
    8
    4
    2
    1

    Notice the 64 starts from a new line?

  15. #15
    azzaiel's Avatar
    azzaiel is offline Member
    Join Date
    Feb 2009
    Posts
    57
    Rep Power
    0

    Default

    System.out.print <-- continues

    System.out.println <-- wil create new line

  16. #16
    scarymovie is offline Member
    Join Date
    Mar 2009
    Posts
    12
    Rep Power
    0

    Default

    Thanks azzaiel...

  17. #17
    scarymovie is offline Member
    Join Date
    Mar 2009
    Posts
    12
    Rep Power
    0

    Default

    azzaiel, there is one more question on concurrency, should I start a new thread or can I continue in this thread?

Posting Permissions

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