Advertisement
mmayoub

classes, Person tester, Exercise 03 slide 48

Jul 8th, 2017
190
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.80 KB | None | 0 0
  1. package class170706;
  2.  
  3. import java.util.Arrays;
  4. import java.util.Random;
  5.  
  6. public class PersonTester {
  7.  
  8.     public static void main(String[] args) {
  9.         // create an object for random numbers
  10.         Random rnd = new Random();
  11.  
  12.         // create a persons array
  13.         Person[] personsArr = new Person[10];
  14.  
  15.         // create each person in the array with random height and weight
  16.  
  17.         System.out.println("source data in the array");
  18.         for (int i = 0; i < personsArr.length; i += 1) {
  19.             // create a person with default values
  20.             personsArr[i] = new Person();
  21.  
  22.             // update each person properties
  23.             personsArr[i].setName("Person-" + i);
  24.             personsArr[i].setHeight(1 + rnd.nextInt(250));
  25.             personsArr[i].setWeight(1 + 150 * rnd.nextDouble());
  26.  
  27.             System.out.printf("[%d] : %s\n", i, personsArr[i]);
  28.         }
  29.  
  30.         // sorting array by weight, using insert sort algorithm
  31.         sortByWeight(personsArr);
  32.         System.out.println("\narray sorted by weight:");
  33.         for (int i = 0; i < personsArr.length; i += 1) {
  34.             System.out.printf("[%d] : %s\n", i, personsArr[i]);
  35.         }
  36.  
  37.         // sorting array by height, using bubble sort algorith
  38.         sortByHeight(personsArr);
  39.         System.out.println("\narray sorted by height:");
  40.         for (int i = 0; i < personsArr.length; i += 1) {
  41.             System.out.printf("[%d] : %s\n", i, personsArr[i]);
  42.         }
  43.  
  44.         // sorting arra by name, using Arrays.sort method
  45.         sortByName(personsArr);
  46.         System.out.println("\narray sorted by name:");
  47.         for (int i = 0; i < personsArr.length; i += 1) {
  48.             System.out.printf("[%d] : %s\n", i, personsArr[i]);
  49.         }
  50.  
  51.     }
  52.  
  53.     // sorting persons array by weight, using insert sort
  54.     public static void sortByWeight(Person[] personsArray) {
  55.         int startIndex = 0; // first element to checked. elements before that
  56.                             // are already sorted
  57.  
  58.         while (startIndex < personsArray.length - 1) {
  59.             // initialize minimum weight and and its index
  60.             int minIndex = startIndex;
  61.             double minWeight = personsArray[startIndex].getWeight();
  62.  
  63.             // start scanning in the array
  64.             for (int i = startIndex + 1; i < personsArray.length; i += 1) {
  65.                 // find the minimum weight and its index
  66.                 if (personsArray[i].getWeight() < minWeight) {
  67.                     minIndex = i;
  68.                     minWeight = personsArray[i].getWeight();
  69.                 }
  70.             }
  71.  
  72.             // put the person with minimum weight at startIndex
  73.             if (minIndex != startIndex) {
  74.                 Person tempPerson = personsArray[startIndex];
  75.                 personsArray[startIndex] = personsArray[minIndex];
  76.                 personsArray[minIndex] = tempPerson;
  77.             }
  78.             // now start searching from the next cell
  79.             startIndex += 1;
  80.         }
  81.     }
  82.  
  83.     // sorting persons array by height, using bubble sort
  84.     public static void sortByHeight(Person[] personsArray) {
  85.         boolean isOrderd; // true when the array is sorted
  86.         int lastIndex = personsArray.length - 1; // last active index in the
  87.                                                     // array
  88.  
  89.         // start switching unordered elements
  90.         do {
  91.             isOrderd = true; // initialization
  92.  
  93.             // scan all active cells in the array (0 to lastIndex)
  94.             for (int i = 0; i < lastIndex; i += 1) {
  95.                 // if persons are not orderd
  96.                 if (personsArray[i].getHeight() > personsArray[i + 1]
  97.                         .getHeight()) {
  98.                     // switch them
  99.                     Person tempPerson = personsArray[i];
  100.                     personsArray[i] = personsArray[i + 1];
  101.                     personsArray[i + 1] = tempPerson;
  102.  
  103.                     // mark array as unordered to make another round
  104.                     isOrderd = false;
  105.                 }
  106.             }
  107.             // last index now containing the right element
  108.             // stop before it next time (mark it as unActive)
  109.             lastIndex -= 1;
  110.         } while (!isOrderd); // loop until the array is ordered
  111.     }
  112.  
  113.     // sorting persons array by name, using Array.sort method
  114.     public static void sortByName(Person[] personsArray) {
  115.         Arrays.sort(personsArray, (person1, person2) -> person1.getName()
  116.                 .compareTo(person2.getName())); // compare two persons by
  117.                                                 // comparing their names
  118.  
  119.     }
  120.  
  121. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement