/******************************************************************************
Welcome to GDB Online.
GDB online is an online compiler and debugger tool for C, C++, Python, Java, PHP, Ruby, Perl,
C#, VB, Swift, Pascal, Fortran, Haskell, Objective-C, Assembly, HTML, CSS, JS, SQLite, Prolog.
Code, Compile, Run and Debug online from anywhere in world.
*******************************************************************************/
package person;
import java.util.Scanner;
public class Main
{
Scanner input = new Scanner(System.in);
public static void main(String[] args) {
Person[] p = new Person[5];
p = details(p);
Person[] sortHight = new Person[p.length];
Person[] sortWeight = new Person[p.length];
p = sortByHight(sortHight, p);
for (int i = 0; i < p.length; i++){
p[i].show();
}
p = sortByWeight(sortWeight, p);
for (int i = 0; i < p.length; i++){
p[i].show();
}
}
public static Person[] details(Person[] p){
for (int i = 0; i < p.length; i++){
p[i].setName((int)(Math.random()*1000));
p[i].setHight((int)(Math.random()*40 + 150));
p[i].setWeight((int)(Math.random()*30 + 50));
p[i].show();
}
return p;
}
public static Person[] sortByHight(Person[] sortHight, Person[] p){
sortHight[0] = p[0];
for (int i = 1; i < sortHight.length; i++){
sortHight[i] = p[i];
for (int j = i; p[i].getHight() < sortHight[j - 1].getHight(); j--){
sortHight[j] = sortHight[j - 1];
sortHight[j - 1] = p[i];
}
}
return sortHight;
}
public static Person[] sortByWeight(Person[] sortWeight, Person[] p){
sortWeight[0] = p[0];
for (int i = 1; i < sortWeight.length; i++){
sortWeight[i] = p[i];
for (int j = i; p[i].getWeight() > sortWeight[j - 1].getWeight(); j--){
sortWeight[j] = sortWeight[j - 1];
sortWeight[j - 1] = p[i];
}
}
return sortWeight;
}
}
package person;
public class Person{
private int name;
private int hight, weight;
public int getName(){
return this.name;
}
public void setName(int n){
this.name = n;
}
public int getHight(){
return this.hight;
}
public void setHight(int h){
this.hight = h;
}
public int getWeight(){
return this.weight;
}
public void setWeight(int w){
this.weight = w;
}
public void show(){
System.out.println("\nname: " + name + "\nhight: " + hight + "\nweight: " + weight);
}
}