Guest User

targil 3

a guest
Jul 10th, 2017
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.81 KB | None | 0 0
  1. PERSON CLASS
  2. ===============================================
  3. public class Person {
  4.     private String name; //person name
  5.     private int height; //person height
  6.     private int weight; //person weight
  7.    
  8.     //getters and setters
  9.     public String getName() {
  10.         return name;
  11.     }
  12.     public void setName(String name) {
  13.         this.name = name;
  14.     }
  15.     public int getHeight() {
  16.         return height;
  17.     }
  18.     public void setHeight(int height) {
  19.         this.height = height;
  20.     }
  21.     public int getWeight() {
  22.         return weight;
  23.     }
  24.     public void setWeight(int weight) {
  25.         this.weight = weight;
  26.     }
  27.    
  28.     //show the info
  29.     public void show()
  30.     {
  31.         //zeev: height>179cm weight>xx Kg
  32.         System.out.println(this.name+": height>"+this.height+" weight>"+this.weight);
  33.     }
  34. }
  35.  
  36.  
  37. MAIN TESTER
  38. ===========================
  39. public class Program {
  40.  
  41.     public static Person[] skinny2fat(Person[] persons) {
  42.         Person[] tempArray = copyArray(persons);
  43.         for (int totalPerson=persons.length;totalPerson>=0;totalPerson-=1)
  44.         {
  45.             for (int counter=1;counter<totalPerson;counter+=1)
  46.             {
  47.                 if (tempArray[counter-1].getWeight()>tempArray[counter].getWeight())
  48.                 {
  49.                     Person temp=tempArray[counter-1];
  50.                     tempArray[counter-1]=tempArray[counter];
  51.                     tempArray[counter]=temp;
  52.                 }
  53.             }
  54.         }
  55.         return tempArray;
  56.     }
  57.  
  58.     public static Person[] smaller2bigger(Person[] persons) {
  59.         Person[] tempArray = copyArray(persons);
  60.         for (int totalPerson=persons.length;totalPerson>=0;totalPerson-=1)
  61.         {
  62.             for (int counter=1;counter<totalPerson;counter+=1)
  63.             {
  64.                 if (tempArray[counter-1].getHeight()>tempArray[counter].getHeight())
  65.                 {
  66.                     Person temp=tempArray[counter-1];
  67.                     tempArray[counter-1]=tempArray[counter];
  68.                     tempArray[counter]=temp;
  69.                 }
  70.             }
  71.         }
  72.         return tempArray;
  73.  
  74.     }
  75.  
  76.     private static Person[] copyArray(Person[] copyPersons) {
  77.         Person[] returnArray = new Person[copyPersons.length];
  78.  
  79.         for (int counter = 0; counter < copyPersons.length; counter += 1) {
  80.             returnArray[counter] = new Person();
  81.             returnArray[counter].setName(copyPersons[counter].getName());
  82.             returnArray[counter].setHeight(copyPersons[counter].getHeight());
  83.             returnArray[counter].setWeight(copyPersons[counter].getWeight());
  84.         }
  85.  
  86.         return returnArray;
  87.     }
  88.  
  89.     public static void main(String[] args) {
  90.         // TODO Auto-generated method stub
  91.         Person[] georgian = new Person[3];
  92.         georgian[0] = new Person();
  93.         georgian[0].setName("Zeevik");
  94.         georgian[0].setHeight(178);
  95.         georgian[0].setWeight(78);
  96.  
  97.         georgian[1] = new Person();
  98.         georgian[1].setName("Mamuka");
  99.         georgian[1].setHeight(158);
  100.         georgian[1].setWeight(120);
  101.  
  102.         georgian[2] = new Person();
  103.         georgian[2].setName("Vitaly");
  104.         georgian[2].setHeight(183);
  105.         georgian[2].setWeight(92);
  106.  
  107.         Person[] sortedHeight = smaller2bigger(georgian);
  108.         for (Person item : sortedHeight) {
  109.             item.show();
  110.         }
  111.     }
  112.  
  113. }
Advertisement
Add Comment
Please, Sign In to add comment