Advertisement
pro-themes

Array Sorting

Dec 7th, 2017
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.94 KB | None | 0 0
  1. /**
  2. * Jessica Wong
  3. * Create an ArrayStat class with a private instance variable that is of type int array. Create a constructor that initializes this variable. Write code for four methods:
  4. * 1) Create a sorted copy of the private instance array by using a loop.
  5. * 2) Create a sorted copy of the private instance array by using the Arrays class copy method.
  6. * 3) Find the median of the private instance array by calling on a sorted copy method. DO NOT ALTER THE ORIGINAL ARRAY.
  7. * 4) Find the mode of the private instance array by calling on a sorted copy method. DO NOT ALTER THE ORIGINAL ARRAY.
  8. * TEST YOUR CODE IN THE MAIN CLASS
  9. */
  10.  
  11. import java.util.*;
  12.  
  13. public class Main {
  14.  
  15.     public static void main(String[] args) {
  16.        
  17.     int test[] = {3,5,2,5,6,1,7,1,5,7,6,3,7,8,8,9,2,1,12,4,1,5,23,24};
  18.     ArrayStat obj = new ArrayStat(test);
  19.     System.out.print("Sorted array:\n");
  20.     for (int x = 0; x < test.length; x++)
  21.     {
  22.         System.out.print(obj.SortArray()[x]);
  23.         if (x != test.length-1)
  24.         {
  25.         System.out.print(",");
  26.         }
  27.     }
  28.         System.out.println("");
  29.     for (int y = 0; y < test.length; y++)
  30.     {
  31.         System.out.print(obj.ArraySortLoop()[y]);
  32.         if (y != test.length-1)
  33.         {
  34.         System.out.print(",");
  35.         }
  36.     }
  37.     System.out.println("\nMedian:\n" + obj.theMedian() + "\nMode:\n" + obj.findMode());
  38.   }
  39. }
  40. ----------------------------------------------------------------------
  41. import java.util.*;
  42.  
  43. public class ArrayStat {
  44.    
  45.     private int arr[];
  46.    
  47.     public ArrayStat (int x[]) {
  48.         arr = x;
  49.     }
  50.    
  51.     public int[] ArraySortLoop(){
  52.        
  53.         int arrlen = arr.length;
  54.         int ara[] = new int[arrlen];
  55.         for (int x = 0; x < arrlen; x++)
  56.         {
  57.             ara[x] = arr[x];
  58.         }
  59.         Arrays.sort(ara);
  60.         return ara;
  61.     }
  62.    
  63.     public int[] SortArray() {
  64.         int ara[] = new int[arr.length];
  65.         System.arraycopy(arr,0,ara,0,arr.length);
  66.         Arrays.sort(ara);
  67.         return ara;
  68.     }
  69.    
  70.     public double theMedian() {
  71.         int[] ara = SortArray();
  72.         int length = ara.length;
  73.         if (ara.length%2 == 1)
  74.         {
  75.             return ara[length/2];
  76.         }
  77.         else
  78.         {
  79.             double sum = ara[length/2] + ara[length/2 + 1];
  80.             return sum/2;
  81.         }
  82.     }
  83.     public int findMode() {
  84.         int ara[] = SortArray();
  85.         int count = 0;
  86.         int maxcount = 0;
  87.         int tempmode = ara[0];
  88.         int mode = ara[0];
  89.         for (int x = 0; x < ara.length; x++)
  90.         {
  91.             tempmode = ara[x];
  92.             count = 0;
  93.         for (int y = x; y < ara.length; y++)
  94.         {
  95.             if (tempmode == ara[y])
  96.         {
  97.             count++;
  98.         }
  99.         if (count > maxcount)
  100.             {
  101.         maxcount = count;
  102.         mode = tempmode;
  103.         }
  104.         }
  105.         }
  106.         return mode;  
  107.     }
  108. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement