اذهب إلى المحتوى

حلا عدنان

الأعضاء
  • المساهمات

    4
  • تاريخ الانضمام

  • تاريخ آخر زيارة

أجوبة بواسطة حلا عدنان

  1. اقتباس

     

    اقتباس
    اقتباس

    You have an array on N size with random numbers. Write a multithreaded program that performs the following operations on this array:

    Thread 1 sorts the even numbers

    Thread 2 sorts the odd numbers

    Thread 3 merg the results (even number in the top part of the array).

    مطلوب هاد السؤال مني حليت Thread1 و Thread 2,هلا Thread 3 كيف بتنحل علما انو الدكتور طالب انو ما لازم تشتغل الا بعد ما Thread1وThread 2يشتغلوا مع بعض ويخلصوا

    
    
    
    import java.util.Scanner;
    
    public class main 
    {
    	public static void main(String[] args)
    	{
    	    
    	    	int length = 0;
    	    Scanner sc=new Scanner(System.in);
    	    System.out.print("Length of array:");
    	    length=sc.nextInt();
    	    int[] array = new int[length];
    	    System.out.println("Enter number of array: ");
    	 
    	    for(int i=0; i<length; i++)
    	    {
    	        array[i]=sc.nextInt();
    	    }
    	    
    	    
    		  
    
    	    ThreadSortEven runnablee=new ThreadSortEven();   
    	    runnablee.setArray(array,length);
    	    Thread thread1 =new Thread(runnablee);    
    	    thread1.start();
    	    		  
    
    	    ThreadSortOdd runnable=new ThreadSortOdd();   
    	    runnable.setArray(array,length);
    	    Thread thread2 =new Thread(runnable);    
    	    thread2.start();
    
    	}
    }
    
    
    class ThreadSortEven implements Runnable  {
    
    
        int[] array;
        int length;
    
    
       public void setArray(int[] array,int length){
           this.array=array;
           this.length=length;
       }
    
    
    	public void run() {
    	
    		    System.out.println("-------------------");
    		    System.out.println("sorted even array :");
    		
    		for(int i=0; i<length; i++)
            {
    			if (array[i]%2==0) 
    			{
    			  
    
    		          System.out.println(array[i]);
            	}
            }	
    	}
    	
    
    }
    
    
    
    
    class ThreadSortOdd implements Runnable  {
    
    
        int[] array;
        int length;
    
    
       public void setArray(int[] array,int length){
           this.array=array;
           this.length=length;
       }
    
    
    	public void run() {
    	
    		    System.out.println("-------------------");
    		    System.out.println("sorted odd array :");
    		
    		for(int i=0; i<length; i++)
            {
    			if (array[i]%2!=0) 
    			{
    			 
    		          System.out.println(array[i]);
            	}
            }	
    	}
    	
    
    }

     

     

     

  2. هاد الكود بياخدinput array وبيعطي output of even numbers بدي اخزن النتيجة في array جديدة كيف ؟؟

    وكمان انا عاملة thread فاذا بدي اعمل كمان thread for odd numbers اعمل كمان class ولا كيف ؟

    اقتباس

    import java.util.Scanner;

    public class main 
    {
        public static void main(String[] args)
        {
              

            threads runnablee=new threads();    
            Thread thread1 =new Thread(runnablee);    
            thread1.start();
        }
    }


    class threads implements Runnable  {


        public void run() {
            int length = 0;
            Scanner sc=new Scanner(System.in);
            System.out.print("Lingth of array:");
            length=sc.nextInt();
            int[] array = new int[length];
            System.out.println("Enter number of array: ");
            for(int i=0; i<length; i++)
            {
                array=sc.nextInt();
            }
            
            for(int i=0; i<length; i++)
            {
                if (array%2==0) 
                {
                      System.out.println(array );

                    
                }
            }    
            
            
        }
        

    }
     

     

×
×
  • أضف...