# Data Structure and Design Search : Square

• Hi Guys,

Question:
You are given an information on Cat object such as name,height and weight. For eg following:

String[] names = {"a","b","c","d","e","f","g","h"};
Integer[] height = {31,24,67,12,45,21,31,12};
Integer[] weight = {120,124,160,130,175,120,124,142};

You have to write a method which takes the following input and return the list of Cat objects which satisfies the input criteria. For eg

searchCriteria could be : HEIGHT or WEIGHT
searchValue could be : Integer value of either HEIGHT or WEIGHT
symbol could be : "<" , ">", or "="

public List<Cat> getCatNames(List<Cat> cats,String searchCriteria,Integer searchValue, String symbol){

}

If I call above getCatNames function as (cats, "HEIGHT",43,"<"), i should get following List<Cat>:

[Cat{name='g', height=31, weight=124},
Cat{name='a', height=31, weight=120},
Cat{name='b', height=24, weight=124},
Cat{name='f', height=21, weight=120},
Cat{name='h', height=12, weight=142},
Cat{name='d', height=12, weight=130}]

Another example:

If I call above getCatNames function as (cats, "WEIGHT",120,"="), i should get following List<Cat>:

[Cat{name='a', height=31, weight=120},
Cat{name='f', height=21, weight=120}]

You have to write a working code for it.

• This is actually an interesting question. I am not sure whether the design is optimal or not but I manage to have a working solution within 40 minutes. I enjoyed solution questions from square.

``````public class Cats{
private String name;
private int HEIGHT;
private int WEIGHT;

public Cats(String name,int height, int weight){
this.name = name;
this.HEIGHT = height;
this.WEIGHT = weight;
}

private static ArrayList<Cats> initializeCats(String[] names, Integer[] height2,Integer[] weight2){
ArrayList<Cats> cats = new ArrayList<Cats>();
for(int i=0; i<names.length;i++){
try{
}catch(Exception e){
System.out.println("the inputs should have the same length");
}
}
return cats;
}
public static List<Cats> getCatNames(List<Cats> cats,String searchCriteria,int searchValue, String symbol){

ArrayList<Cats> list = new ArrayList<Cats>();

//condition 1 by symbol
switch(symbol){
case "<" :
if(searchCriteria.equals("WEIGHT")){
for(int i=0; i<cats.size();i++){
if( cats.get(i).getWeight() < searchValue ){
}
}
}else if(searchCriteria.equals("HEIGHT")){
for(int i=0; i<cats.size();i++){
if( cats.get(i).getHeight() < searchValue ){
}
}
}
break;
case ">" :
if(searchCriteria.equals("WEIGHT")){
for(int i=0; i<cats.size();i++){
if( cats.get(i).getWeight() > searchValue ){
}
}
}else if(searchCriteria.equals("HEIGHT")){
for(int i=0; i<cats.size();i++){
if( cats.get(i).getHeight() > searchValue ){
}
}
}
break;
case "=" :
if(searchCriteria.equals("WEIGHT")){
for(int i=0; i<cats.size();i++){
if( cats.get(i).getWeight() == searchValue ){
}
}
}else if(searchCriteria.equals("HEIGHT")){
for(int i=0; i<cats.size();i++){
if( cats.get(i).getHeight() == searchValue ){
}
}
}
break;
}

return list;
}
public String getName(){
return this.name;
}
public int getHeight(){
return this.HEIGHT;
}
public int getWeight(){
return this.WEIGHT;
}

public String toString(){
return "name =" + this.name + " height =" + this.HEIGHT + " weight =" + this.WEIGHT;
}

public static void main(String[] args){
String[] names = {"a","b","c","d","e","f","g","h"};
Integer[] height = {31,24,67,12,45,21,31,12};
Integer[] weight = {120,124,160,130,175,120,124,142};

ArrayList<Cats> cats = initializeCats(names,height,weight);

List<Cats> result = getCatNames(cats,"WEIGHT",120,"=");
for(int i=0; i<result.size();i++){
System.out.println(result.get(i).toString());
}
}

}``````

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.