Page 469 - Cs_withBlue_J_C11_Flipbook
P. 469

8.  Write a program to create a package Eleven containing class Sort having the following specifications:
                        Package                         :     Eleven
                        Class name                      :     Sort
                        Data Members
                        int n                           :     Size of array
                        int a[]                         :     Integer array
                        Member Methods
                        Sort(int x, int y[])            :     Constructor to assign the data members
                        int [] bubble()                 :       Sorts  the  array  using  the  bubble  sort  technique  in  ascending  order  and
                                                              returns the sorted array
                        Write another class Second in package Orange_prog to import class Sort of package Eleven and use the method bubble()  to
                       print the second highest and the second lowest element of any array. The class description is given below:
                        Package                         :     Orange_prog
                        Class name                      :     Second
                        Data Members
                        int arr[], size                 :     stores integer array arr[] of length size
                        Member Methods
                        void accept()                   :       Accepts size and then enters given number of elements in array arr[]
                        void print()                    :       Prints the second highest and the second lowest element in array arr[] after
                                                              calling bubble() of class Sort of package Eleven
                   Ans.  package Eleven;
                       public class Sort
                       {
                         int n,a[];
                         public Sort(int x,int y[])
                         {
                             n=x;
                             a=new int[n];
                             for(int i=0;i<n;i++)
                             {
                                a[i]=y[i];
                             }
                          }
                         public int[] bubble()
                         {     //sorting in ascending order using bubble sort technique
                             int i,j,t;
                             for(i=0;i<n-1;i++)
                             {
                                for(j=0;j<n-i-1;j++)
                                {
                                   if(a[j]>a[j+1])
                                   {
                                       t=a[j];
                                       a[j]=a[j+1];
                                       a[j+1]=t;
                                   }
                                }
                             }
                             return a;
                             }
                         }


                                                                                                                       467
                                                                                                            Packages   467
   464   465   466   467   468   469   470   471   472   473   474