Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- PERSON CLASS
- ===============================================
- public class Person {
- private String name; //person name
- private int height; //person height
- private int weight; //person weight
- //getters and setters
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public int getHeight() {
- return height;
- }
- public void setHeight(int height) {
- this.height = height;
- }
- public int getWeight() {
- return weight;
- }
- public void setWeight(int weight) {
- this.weight = weight;
- }
- //show the info
- public void show()
- {
- //zeev: height>179cm weight>xx Kg
- System.out.println(this.name+": height>"+this.height+" weight>"+this.weight);
- }
- }
- MAIN TESTER
- ===========================
- public class Program {
- public static Person[] skinny2fat(Person[] persons) {
- Person[] tempArray = copyArray(persons);
- for (int totalPerson=persons.length;totalPerson>=0;totalPerson-=1)
- {
- for (int counter=1;counter<totalPerson;counter+=1)
- {
- if (tempArray[counter-1].getWeight()>tempArray[counter].getWeight())
- {
- Person temp=tempArray[counter-1];
- tempArray[counter-1]=tempArray[counter];
- tempArray[counter]=temp;
- }
- }
- }
- return tempArray;
- }
- public static Person[] smaller2bigger(Person[] persons) {
- Person[] tempArray = copyArray(persons);
- for (int totalPerson=persons.length;totalPerson>=0;totalPerson-=1)
- {
- for (int counter=1;counter<totalPerson;counter+=1)
- {
- if (tempArray[counter-1].getHeight()>tempArray[counter].getHeight())
- {
- Person temp=tempArray[counter-1];
- tempArray[counter-1]=tempArray[counter];
- tempArray[counter]=temp;
- }
- }
- }
- return tempArray;
- }
- private static Person[] copyArray(Person[] copyPersons) {
- Person[] returnArray = new Person[copyPersons.length];
- for (int counter = 0; counter < copyPersons.length; counter += 1) {
- returnArray[counter] = new Person();
- returnArray[counter].setName(copyPersons[counter].getName());
- returnArray[counter].setHeight(copyPersons[counter].getHeight());
- returnArray[counter].setWeight(copyPersons[counter].getWeight());
- }
- return returnArray;
- }
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- Person[] georgian = new Person[3];
- georgian[0] = new Person();
- georgian[0].setName("Zeevik");
- georgian[0].setHeight(178);
- georgian[0].setWeight(78);
- georgian[1] = new Person();
- georgian[1].setName("Mamuka");
- georgian[1].setHeight(158);
- georgian[1].setWeight(120);
- georgian[2] = new Person();
- georgian[2].setName("Vitaly");
- georgian[2].setHeight(183);
- georgian[2].setWeight(92);
- Person[] sortedHeight = smaller2bigger(georgian);
- for (Person item : sortedHeight) {
- item.show();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment