The Cats class creates a list of n Cat objects each with the same name except on
ID: 3756667 • Letter: T
Question
The Cats class creates a list of n Cat objects each with the same name except one. We need to implement all the necessary methods, in the class Cats and Cat to detect the 2 Cat objects with the same name. You may implement other methods as you see fit:
Cat.java:
public class Cat implements Comparable<Cat> {
String name;
int catID;
Cat(String name, int catID) {
this.name = name;
this.catID = catID;
}
Cat(int d) {
this("C" + d, d);
}
@Override
public String toString() {
return name + ": " + catID;
}
Cats.java:
import java.util.Collections;
import java.util.List;
import java.util.Random;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
public class Cats {
List<cat> L;
Cats(int n){
L = IntStream.rangeClosed(1, n).boxed().map(i->new Cat(i)).collect(Collectors.toList());
int d = new Random().nextInt(L.size()-1)+1;
L.add(new Cat(d));
Collections.shuffle(L);
}
Cats(){
this(5);
}
List<Cat> getSortedList(){
return L.stream().sorted().collect(Collectors.toList());
}
@Override
public String toString() {
return L.toString() + " " + getSortedList().toString();
}
}
Explanation / Answer
Hello there, I did not understand your question very clearly, but what I understood is that you needed to implement compareTo() method in Cat class, and create a list of cats in Cats class, with all of them having same name except for one cat. Also implemented a method to check if both cats have same names using the compareTo() method. Then created a main method, created an object of Cats, fetched the list of cats, displayed it, iterated through each combination of cats, displayed if their names are same or not. Let me know if you have any doubts or if you need to change anything. If you are satisfied with the solution, please rate the answer. Thanks
// Cat.java
public class Cat implements Comparable<Cat> {
String name;
int catID;
Cat(String name, int catID) {
this.name = name;
this.catID = catID;
}
Cat(int d) {
this("C" + d, d);
}
@Override
public String toString() {
return name + ": " + catID;
}
@Override
public int compareTo(Cat o) {
//comparing name of this cat and the other,
//will return 0 if both names are same
return name.compareTo(o.name);
}
}
// Cats.java
import java.util.Collections;
import java.util.List;
import java.util.Random;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
public class Cats {
List<Cat> L;
Cats(int n) {
//modified to create n-1 cats with same name, and one cat with different
//name
L = IntStream.rangeClosed(1, n).boxed().map(i -> new Cat("Cat", i)).collect(Collectors.toList());
int d = new Random().nextInt(L.size() - 1) + 1;
L.add(new Cat("Kitty", d));
Collections.shuffle(L);
}
Cats() {
this(5);
}
List<Cat> getSortedList() {
return L.stream().sorted().collect(Collectors.toList());
}
@Override
public String toString() {
return L.toString() + " " + getSortedList().toString();
}
/**
* this method returns true if two cats have same name
* @param c1 - cat 1
* @param c2 - cat2
*/
public boolean hasSameName(Cat c1, Cat c2) {
//two cats have same name if the compareTo method returns 0
return c1.compareTo(c2) == 0;
}
public static void main(String[] args) {
//creating cats
Cats cats = new Cats();
//displaying unsorted and sorted list of cats using toString() method
System.out.println(cats);
//getting cats list
List<Cat> catsList = cats.getSortedList();
//looping through all pair of cats, checking if their names are same
for (int i = 0; i < catsList.size(); i++) {
for (int j = 0; j < catsList.size(); j++) {
if (i != j) {
if (cats.hasSameName(catsList.get(i), catsList.get(j))) {
System.out.println(catsList.get(i) + " and "
+ catsList.get(j) + " have same name");
} else {
System.out.println(catsList.get(i) + " and "
+ catsList.get(j) + " have different names");
}
}
}
}
}
}
//OUTPUT
[Cat: 5, Cat: 1, Cat: 3, Kitty: 1, Cat: 2, Cat: 4]
[Cat: 5, Cat: 1, Cat: 3, Cat: 2, Cat: 4, Kitty: 1]
Cat: 5 and Cat: 1 have same name
Cat: 5 and Cat: 3 have same name
Cat: 5 and Cat: 2 have same name
Cat: 5 and Cat: 4 have same name
Cat: 5 and Kitty: 1 have different names
Cat: 1 and Cat: 5 have same name
Cat: 1 and Cat: 3 have same name
Cat: 1 and Cat: 2 have same name
Cat: 1 and Cat: 4 have same name
Cat: 1 and Kitty: 1 have different names
Cat: 3 and Cat: 5 have same name
Cat: 3 and Cat: 1 have same name
Cat: 3 and Cat: 2 have same name
Cat: 3 and Cat: 4 have same name
Cat: 3 and Kitty: 1 have different names
Cat: 2 and Cat: 5 have same name
Cat: 2 and Cat: 1 have same name
Cat: 2 and Cat: 3 have same name
Cat: 2 and Cat: 4 have same name
Cat: 2 and Kitty: 1 have different names
Cat: 4 and Cat: 5 have same name
Cat: 4 and Cat: 1 have same name
Cat: 4 and Cat: 3 have same name
Cat: 4 and Cat: 2 have same name
Cat: 4 and Kitty: 1 have different names
Kitty: 1 and Cat: 5 have different names
Kitty: 1 and Cat: 1 have different names
Kitty: 1 and Cat: 3 have different names
Kitty: 1 and Cat: 2 have different names
Kitty: 1 and Cat: 4 have different names