logo

Crowdly

Assume the following class Animal and its use are given: package com.comp...

✅ Перевірена відповідь на це питання доступна нижче. Наші рішення, перевірені спільнотою, допомагають краще зрозуміти матеріал.

Assume the following class Animal and its use are given:

package com.company;

import java.util.Comparator;

import java.util.SortedSet;

import java.util.TreeSet;

class Animal implements Comparable<Animal>{

final String kind;

final String name;

final int age;

Animal(String kind, String name, int age) {

this.kind = kind;

this.name = name;

this.age = age;

}

@Override

public int compareTo (Animal other){

int retVal = kind.compareTo(other.kind);

if (retVal == 0) {retVal= name.compareTo(other.name);}

return retVal;

}

/*...*/

static class MyComparator implements Comparator<Animal> {

/*...*/

public int compare( Animal a1, Animal a2){

return a2.age-a1.age;

}

}

}

public static class Main(String[] args){

SortedSet<Animal> animals = new TreeSet<>();

animals.add(new Animal(“Cat”,”Zero”,2));

animals.add(new Animal(“Cat”, “Filou”, 5));

animals.add(new Animal(“Dog”, “Puppy”, 3));

SortedSet<Animal> animals2 = new TreeSet<>(new Animal.MyComparator());

animals2.addAll(animals);

}

 

Which of the following statements are true?

a) The set animals will be ordered as follows: Filou – Puppy - Zero

b) The set animals will be ordered as follows: Filou – Zero – Puppy

c) The set animals2 is ordered as follows: Zero – Puppy - Filou

d) When multiple sorting schemes should be provided, create and use multiple Comparators.

Більше питань подібних до цього

Хочете миттєвий доступ до всіх перевірених відповідей на moodle.jku.at?

Отримайте необмежений доступ до відповідей на екзаменаційні питання - встановіть розширення Crowdly зараз!