Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Given the following class public class Cat { private String name; private String

ID: 3781409 • Letter: G

Question

Given the following class

public class Cat {
private String name;
private String breed;
private String registrationNumber;
  
public Cat(String name, String breed, String registrationNumber) {
this.name = name;
this.breed = breed;
this.registrationNumber = registrationNumber;
}
  
public String getName() {
return this.name;
}
  
public String getBreed() {
return this.breed;
}
  
public String getRegistrationNumber() {
return this.registrationNumber;
}
}

Create a class the satisfies the following requirements.

· Maintains a list of cats in alphabetical order, first by name, then by breed.

· Provides a method for adding new cats.

· Provides a method for searching by registration number.

· Provides a method for search by Name.

· Employs the most efficient search techniques available.

· Constructor that accepts an initial list of cats.

BONUS: What simple construct could be done to improve the Cat class.

NOTE: If you use any libraries or frameworks to help you, please list them at the top of your answer along with a link.

BONUS 2: Write unit test(s) for your new class.

Explanation / Answer

Cat.java

package cat;

import java.util.ArrayList;
import java.util.List;

public class Cat {
   private String name;
private String breed;
private String registrationNumber;
private List<Cat> cats=new ArrayList<Cat>();//list of cats.

public List<Cat> getCats() {
       return cats;
   }

   public void setCats(List<Cat> cats1) {
       cats = cats1;
   }

public Cat(String name, String breed, String registrationNumber) {
this.name = name;
this.breed = breed;
this.registrationNumber = registrationNumber;
}

public String getName() {
return this.name;
}

public String getBreed() {
return this.breed;
}

public String getRegistrationNumber() {
return this.registrationNumber;
}

}

catMan.java

package cat;


import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class catMan {

   //constructor to accept initial list of cats
   private List<Cat>cats1;
   public catMan(List<Cat> catlist){
       cats1=catlist;
   }
  
  
//method to add cat  
   public void addCat(String name,String breed,String regno){
       Cat c=new Cat(name,breed,regno);
       cats1.add(c);
      
       //sort by name and then by breed
       Collections.sort(cats1, new Comparator<Cat>() {
           public int compare(Cat c1, Cat c2) {
   int cmp = c1.getName().compareToIgnoreCase(c2.getName());
   if (cmp != 0)
   return cmp;
   return c1.getBreed().compareToIgnoreCase(c2.getBreed());
   }
   });
       c.setCats(cats1);
   }
  
   //method to search by registration number.
   public void seracByReg(String regno){
      
       for(int i=0;i<cats1.size();i++){
           Cat c=cats1.get(i);
           if(regno.equals(c.getRegistrationNumber())){
               System.out.println("Name : "+c.getName());
               System.out.println("Breed : "+c.getBreed());
               System.out.println("Registration Number : "+c.getRegistrationNumber());
               break;
           }
       }
   }
  
   //ethod to search by name.
public void seracByName(String name){
      
       for(int i=0;i<cats1.size();i++){
           Cat c=cats1.get(i);
           if(name.equals(c.getName())){
               System.out.println("Name : "+c.getName());
               System.out.println("Breed : "+c.getBreed());
               System.out.println("Registration Number : "+c.getRegistrationNumber());
               break;
           }
       }
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote