-1

here's my problem. I've got 3 classes: one called Pets, one Pet and one PetActions.

In my Pet-class I've three variables: String name, int weight, int height and a constructor.

In my PetAction-Class I've got my main methode. In it I'm creating 5 Pet-Objects

Pet cat = new Pet ("cat", 4, 25);
Pet dog = new Pet ("dog", 24, 58);
Pet bird = new Pet("bird" , 0.3, 18);
Pet snake = new Pet("snake", 2, 150);
Pet rabbit = new Pet ("rabbit", 2, 40);

These I'm packing in a Pet- Array

Array[] pets = {cat, dog, bird, rabbit, snake};

Which I'm using to create a Pets-Object

Pets pets = new Pets(pets);

My Pets- Class looks like this:

public class Pets{

    Pet[] pets;

    public Pets(Pet[] pets){
        this.pets = pets;
    }   

    public int position(Pet pet) {
        Pet[] sortedPets;

        // search in sortedPets pet



        return position;
    }

    public Verein getPetFromString(String s) {



        return ;
    }

Now, In my PetAction - class, I'm asking the user the position of which animal he wants to know (They are supposed to be sorted from lightes to heaviest, if two weight the same it checks the realtion between weight and height), and here comes my problem. I just don't understand how this is supposed to work properly. I know I have to use Comparable and Comporator (at least that's what I read online), but I don't manage to properly take an example and proceed with it. Maybe also due to the language barrier. Can somone help me please? (:

Seyhmus Gokcen
  • 248
  • 4
  • 10
PaperHat
  • 1
  • 1

2 Answers2

0

Option 1 : Using a custom comparator

If you want to sort your Pet array pets you can use Array.sort() and provide a Comparator:

Arrays.sort(pets, Comparator.comparing(Pet::getWeight).thenComparing(Pet::getHeight));

I assumed you have getters for weight and height.
This sort your array from lightest to heaviest and from smallest to tallest if two pets have the same weight.

You could call it in your Pets constructor:

public Pets(Pet[] pets){
    Arrays.sort(pets, ...);
    this.pets = pets;
}

Option 2: Implementing Comparable interface

Alternatively, you can modify your class to implement Comparable and implement the method CompareTo():

public class Pet implements Comparable<Pet>{

    ...

    @Override
    public int compareTo(Pet o) {
        int result = Integer.compare(this.height, o.height);
        if(result != 0) return result;
        else return Integer.compare(this.weight, o.weight);
    } 
}

Then you can simply call:

Arrays.sort(pets)
Ricola
  • 2,621
  • 12
  • 22
  • Thanks, but do I really need getters? In my Pet class the variables are public, so shouldn't I be able to use them directly? Because if not, didn't I need to create a Pet-Object in my Pets-Class with the methode first in order to be able to use it? I'm kinda confused – PaperHat Jan 19 '19 at 11:41
  • You don't necessarily need them, even if it is well advised to do it for [encapsulation](https://en.wikipedia.org/wiki/Encapsulation_(computer_programming)). If you don't want to create getters, you can create your comparator with lambdas: `Arrays.sort(pets, Comparator.comparing((Pet pet) -> pet.weigth).thenComparing(pet -> pet.height));` – Ricola Jan 19 '19 at 11:50
  • Okey, thanks. And how am I able to search in the array for the string the user entered and than return the corrosponding Pet- object , so I ca use this object in the comparisonpart? – PaperHat Jan 19 '19 at 12:03
  • Please have a look at this question : https://stackoverflow.com/questions/3384203/finding-an-element-in-an-array-in-java Note that since your array is sorted, you can use `Arrays.binarySearch()`. – Ricola Jan 19 '19 at 12:47
  • But since I need that Object in order to sort my array and give out the position it's not sorted yet, or is it? – PaperHat Jan 19 '19 at 12:52
0

First you implement Comparable interface to your pet class and add a compare to method to your Pet class which returns a integer(+ve if this pet greater than other pet) negative otherwise.

class Pet implements Comparable{
    String name;
    int weight;
    int height; 
    public int compareTo(Pet otherPet){  
        if(weight > otherPet.weight){
            return 1;
        }
        else if(weight = otherPet.weight){
            if(height > otherPet.height){
                return 1;
            }
        }
        else{
         return -1;
        }
    }
}

Implementing that interface lets you use this line which sorts your array

Collections.sort(Pets);
Sanath Kumar
  • 163
  • 1
  • 12