Advertisement
eranseg

Person

Aug 21st, 2019
205
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.09 KB | None | 0 0
  1. /////////////////// Main Class //////////////////////////////
  2. public class PersonView {
  3.     public static void main(String[] args) {
  4.         Person[] persons = new Person[5];
  5.         initPersonsArray(persons);
  6.         printPersons(persons);
  7.         sort(persons, 1);
  8.         printPersons(persons);
  9.         sort(persons, 0);
  10.         printPersons(persons);
  11.     }
  12.  
  13.     private static void initPersonsArray(Person[] pArray) {
  14.         for(int i = 0; i < pArray.length; i++) {
  15.             pArray[i] = new Person();
  16.             pArray[i].setName("Person"+i);
  17.             pArray[i].setHeight(Math.random()*20+175);
  18.             pArray[i].setWeight(Math.random()*20+80);
  19.         }
  20.     }
  21.  
  22.     private static void printPersons(Person[] pArray) {
  23.         for(int i = 0; i < pArray.length; i++) {
  24.             pArray[i].show();
  25.         }
  26.         System.out.println();
  27.     }
  28.  
  29.     private static void sort(Person[] p, int by) {
  30.         if(by == 1) {
  31.             sortByHeight(p);
  32.         }else {
  33.             sortByWeight(p);
  34.         }
  35.     }
  36.  
  37.     private static void sortByHeight(Person[] pArray) {
  38.         Person pUtil;
  39.         for(int i = 1; i < pArray.length; i += 1) {
  40.             int j = i;
  41.             while(j > 0) {
  42.                 if(pArray[j].getHeight() < pArray[j-1].getHeight()) {
  43.                     pUtil = pArray[j];
  44.                     pArray[j] = pArray[j-1];
  45.                     pArray[j-1] = pUtil;
  46.                 }
  47.                 j -= 1;
  48.             }
  49.         }
  50.     }
  51.  
  52.     private static void sortByWeight(Person[] pArray) {
  53.         Person pUtil;
  54.         for(int i = 1; i < pArray.length; i += 1) {
  55.             int j = i;
  56.             while(j > 0) {
  57.                 if(pArray[j].getWeight() < pArray[j-1].getWeight()) {
  58.                     pUtil = pArray[j];
  59.                     pArray[j] = pArray[j-1];
  60.                     pArray[j-1] = pUtil;
  61.                 }
  62.                 j -= 1;
  63.             }
  64.         }
  65.     }
  66. }
  67.  
  68. ////////////////////////////////// Person Class //////////////////////////////////
  69. public class Person {
  70.  
  71.     // Declaring the class properties
  72.     String name;
  73.     double height;
  74.     double weight;
  75.  
  76.     // Implementing the methods
  77.  
  78.     public String getName() {
  79.         return name;
  80.     }
  81.  
  82.     public boolean setName(String name) {
  83.         this.name = name;
  84.         return true;
  85.     }
  86.  
  87.     public double getHeight() {
  88.         return height;
  89.     }
  90.  
  91.     public boolean setHeight(double height) {
  92.         if(height <= 0) {
  93.             System.out.println("Height must be greater then 0!");
  94.             return false;
  95.         }
  96.         this.height = height;
  97.         return true;
  98.     }
  99.  
  100.     public double getWeight() {
  101.         return weight;
  102.     }
  103.  
  104.     public boolean setWeight(double weight) {
  105.         if(weight <= 0) {
  106.             System.out.println("Weight must be greater then 0!");
  107.             return false;
  108.         }
  109.         this.weight = weight;
  110.         return true;
  111.     }
  112.  
  113.     public void show() {
  114.         System.out.printf("The person's name is %s; He's height is %f and he's weight is %f\n", name, height, weight);
  115.     }
  116. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement