Question: Here is what is given GumpyCat.java public class GrumpyCat extends Cat { // a grumpy cat IS A cat /* Attributes *********************************************/ // It inherits



Here is what is given
GumpyCat.java
public class GrumpyCat extends Cat { // a grumpy cat IS A cat /* Attributes *********************************************/ // It inherits the attributes of Cat // Here we are defining a new attribute private int levelOfGrumpiness; /* Constructors */ public GrumpyCat() { super(); } public GrumpyCat(String breed, String name, int age) { super(breed, name, age); } // This second constructor is an overload of the first one (polymorphism) public GrumpyCat(int grump) { super(); levelOfGrumpiness = grump; } /* Getters & Setters ****************************************/ /** * @return the levelOfGrumpiness */ public int getLevelOfGrumpiness() { return levelOfGrumpiness; }
/** * @param levelOfGrumpiness the levelOfGrumpiness to set */ public void setLevelOfGrumpiness(int levelOfGrumpiness) { this.levelOfGrumpiness = levelOfGrumpiness; } /* Other methods ********************************************/ /* MEOW * is used from the parent class */ /* PLAYS * is overridden (polymorphism) */ public void plays() { // over ride of plays from class Cat + polymorphism if (levelOfGrumpiness >= 2) System.out.println("it does not play!!!"); else System.out.println("it only sometimes plays!!!"); }
}
-----------------------------------------------------------------------------
CudlyCat.java public class CuddlyCat extends Cat {
public CuddlyCat() { super(); } public CuddlyCat(String breed, String name, int age) { super(breed, name, age); } public void plays() { System.out.println("It always plays"); } }
-----------------------------------------------------------------------------
CatShelter.java
import java.io.BufferedReader; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException;
public class CatShelter {
/* YOU HAVE TO COMPLETE THIS METHOD: */ /* Method called readCatsIntoArray; you have to implement it * Parameter: String filename (the file that contains the information about cats); * Returns: an array of Cats. */ public static Cat[] readCatsIntoArray(String filename) throws FileNotFoundException, IOException { Cat[] Shelter;
FileReader fr = new FileReader(filename); BufferedReader textReader = new BufferedReader(fr);
/* We read the size of the Shelter as an integer on the first line of the file */ if (textReader.ready()) { Shelter = new Cat[Integer.valueOf(textReader.readLine())]; } else { textReader.close(); return new Cat[0]; }
// COMPLETE CODE IN THE FOR LOOP, INCLUDING EXCEPTIONS IN CASE FILE IS NOT CORRECTLY FORMATTED for (int i = 0; i
textReader.close(); return Shelter; } /* THIS METHOD IS GIVEN TO YOU: YOU SHOULD NOT MODIFY IT */ /* Method called sortByCategory; it is given to you but you have to implement its parts (see below) * Parameter: an array of objects of type Cat; * Returns nothing (your method modifies the input array in place since its * reference address is passed to the method). * This method uses: * - Insertion sort to sort the Cat category; * - Bubble sort to sort the GrumpyCat category; and * - Selection sort to sort the CuddlyCat category. * As well as a method to separate different types of cats: catCategories (see below). * You will have to implement each of the 4 above methods, but sortByCategory will be provided to you. */ public static void sortByCategory(Cat[] Shelter) { int[] catIndices = catCategory(Shelter); insertionSortRangeByAgeName(Shelter, 0, catIndices[0]-1); bubbleSortRangeByAgeName(Shelter, catIndices[0], catIndices[1]-1); selectionSortRangeByAgeName(Shelter, catIndices[1], Shelter.length-1); } /* YOU HAVE TO COMPLETE THIS METHOD: */ /* Method catCategory: * Takes an array of Cat objects; * Modifies this array in such a way that all generic cats come first, * followed by grumpy cats, and then all cuddly cats are at the end of the array. * Returns the index of the first grumpy cat as well as the index of * the first cuddly cat, bundled into an array of 2 ints. * Note: you are guaranteed to have at least one cat in each category */ public static int[] catCategory(Cat[] Shelter) { int[] indices = new int[2]; // COMPLETE CODE HERE return indices; } /* YOU HAVE TO COMPLETE THIS METHOD: */ /* Method insertionSortRangeByAgeName: * Takes an array C of Cat objects, a start index, an end index; * Sorts the elements of array C between index start and index end * (included), by age and then by name, following the insertion sort algorithm; * Does not return anything (C is modified in place). */ public static void insertionSortRangeByAgeName(Cat[] Shelter, int start, int end) { // COMPLETE CODE HERE } /* YOU HAVE TO COMPLETE THIS METHOD: */ /* Method selectionSortRangeByAgeName: * Takes an array C of Cat objects, a start index, an end index; * Sorts the elements of array C between index start and index end (included), * by age and then by name, following the selection sort algorithm; * Does not return anything (C is modified in place). */ public static void selectionSortRangeByAgeName(Cat[] Shelter, int start, int end) { // COMPLETE CODE HERE }
/* YOU HAVE TO COMPLETE THIS METHOD: */ /* Method bubbleSortRangeByAgeName: * Takes an array C of Cat objects, a start index, an end index; * Sorts the elements of array C between index start and index end (included), * by age and then by name, following the bubble sort algorithm; * Does not return anything (C is modified in place). */ public static void bubbleSortRangeByAgeName(Cat[] Shelter, int start, int end) { // COMPLETE CODE HERE }
}
-----------------------------------------------------------------------------
Cat.java
public class Cat {
/* Attributes **********************************************/ private String breed; private String name; private int age; /* Constructors */ public Cat() {} public Cat(int num) { age = num; } public Cat(String breed) { this.breed = breed; } public Cat(String breed, String name, int age) { this.breed = breed; this.name = name; this.age = age; } /* Getters & Setters ****************************************/ /** * @return the breed */ public String getBreed() { return breed; }
/** * @param breed the breed to set */ public void setBreed(String breed) { this.breed = breed; }
/** * @return the name */ public String getName() { return name; }
/** * @param name the name to set */ public void setName(String name) { this.name = name; }
/** * @return the age */ public int getAge() { return age; }
/** * @param age the age to set */ public void setAge(int age) { this.age = age; } /* Other methods ********************************************/ /* MEOW */ public void meows() { System.out.println("meow"); } /* PLAYS */ public void plays() { System.out.println("It really depends on the cat"); } } -----------------------------------------------------------------------------

What problem will you be addressing in this lab? You are in charge of a cat shelter and you have to store your cats' profiles. When a potential adopter comes for a cat, you need to produce the cat(s) best suited to the potential adopters (generic cat, grumpy cat, cuddly cat). You decide to design code to handle your work at the shelter. You will store your cats' profiles in an array of cats, and you will have to make sure that your array of cats is always sorted by type. Here is what your code has to do: Read cats' information from a file into an array: Method called readCatsintoArray; you have to implement it Parameter: String filename the file that contains the information about cats); Returns: an array of Cats. Sort your array of cats as follows: The first cats in your array are generic cats (known neither as grumpy nor as cuddly); After the generic cats, in your array, you find the grumpy cats; o Finally, after the grumpy cats, come the cuddly cats. Each category of cats is sorted in increasing order of their age. When two cats are the same age, the cats whose name comes first in alphabetical order will come first. Method called sortByCategory; it is given to you but you have to implement its parts (see below) Parameter: an array of objects of type Cat; Returns nothing (your method modifies the input array in place since its reference - address - is passed to the method). o This method is expected to use: Insertion sort to sort the Cat category; Bubble sort to sort the GrumpyCat category, and Selection sort to sort the CuddlyCat category. 0 o For this lab, we give you the class files for Cat, Grumpy Cat, and CuddlyCat. We also give you an example of a file, myCatShelter.txt, that contains cat profiles. It will be one of the files that we will use to test your code. What are you asked to do? Here is what you have to do (write code within CatShelter.java): Complete the body of the method readCats Into Array where prompted within CatShelter; o We provide you with sample code to read from a file (ReadWrite.java): please use it. Write a method catCategories that: Takes an array of Cat objects; o Modifies this array in such a way that all generic cats come first, followed by grumpy cats, and then all cuddly cats are at the end of the array. Returns the index of the first grumpy cat as well as the index of the first cuddly cat, bundled into an array of 2 ints. Write an insertion SortRangeByAgeName that: o Takes an array of Cat objects, a start index, an end index; Sorts the elements of array C between index start and index end (included), by age and then by name, following the insertion sort algorithm; o Does not return anything (C is modified in place). Writes an selectionSortRangeByAgeName that: o Takes an array of Cat objects, a start index, an end index; o Sorts the elements of array C between index start and index end (included), by age and then by name, following the selection sort algorithm; Does not return anything (C is modified in place). o Writes an bubbleSortRangeByAgeName that: o Takes an array of Cat objects, a start index, an end index; Sorts the elements of array C between index start and index end (included), by age and then by name, following the bubble sort algorithm; o Does not return anything (C is modified in place). You are expected to provide 2 test cases per method you have to write. CatHerd.txt 110 Cat Felix European 4 Grumpy Cat Carpet Mainsoon 5 Grumpy Cat Chipie European 8 Cat Chat Persian 3 Grumpy Cat Theo Egyptian 3 Cuddly Cat Mistigri Gris 1 Cat Chien Blue 4 Cuddly Cat What problem will you be addressing in this lab? You are in charge of a cat shelter and you have to store your cats' profiles. When a potential adopter comes for a cat, you need to produce the cat(s) best suited to the potential adopters (generic cat, grumpy cat, cuddly cat). You decide to design code to handle your work at the shelter. You will store your cats' profiles in an array of cats, and you will have to make sure that your array of cats is always sorted by type. Here is what your code has to do: Read cats' information from a file into an array: Method called readCatsintoArray; you have to implement it Parameter: String filename the file that contains the information about cats); Returns: an array of Cats. Sort your array of cats as follows: The first cats in your array are generic cats (known neither as grumpy nor as cuddly); After the generic cats, in your array, you find the grumpy cats; o Finally, after the grumpy cats, come the cuddly cats. Each category of cats is sorted in increasing order of their age. When two cats are the same age, the cats whose name comes first in alphabetical order will come first. Method called sortByCategory; it is given to you but you have to implement its parts (see below) Parameter: an array of objects of type Cat; Returns nothing (your method modifies the input array in place since its reference - address - is passed to the method). o This method is expected to use: Insertion sort to sort the Cat category; Bubble sort to sort the GrumpyCat category, and Selection sort to sort the CuddlyCat category. 0 o For this lab, we give you the class files for Cat, Grumpy Cat, and CuddlyCat. We also give you an example of a file, myCatShelter.txt, that contains cat profiles. It will be one of the files that we will use to test your code. What are you asked to do? Here is what you have to do (write code within CatShelter.java): Complete the body of the method readCats Into Array where prompted within CatShelter; o We provide you with sample code to read from a file (ReadWrite.java): please use it. Write a method catCategories that: Takes an array of Cat objects; o Modifies this array in such a way that all generic cats come first, followed by grumpy cats, and then all cuddly cats are at the end of the array. Returns the index of the first grumpy cat as well as the index of the first cuddly cat, bundled into an array of 2 ints. Write an insertion SortRangeByAgeName that: o Takes an array of Cat objects, a start index, an end index; Sorts the elements of array C between index start and index end (included), by age and then by name, following the insertion sort algorithm; o Does not return anything (C is modified in place). Writes an selectionSortRangeByAgeName that: o Takes an array of Cat objects, a start index, an end index; o Sorts the elements of array C between index start and index end (included), by age and then by name, following the selection sort algorithm; Does not return anything (C is modified in place). o Writes an bubbleSortRangeByAgeName that: o Takes an array of Cat objects, a start index, an end index; Sorts the elements of array C between index start and index end (included), by age and then by name, following the bubble sort algorithm; o Does not return anything (C is modified in place). You are expected to provide 2 test cases per method you have to write. CatHerd.txt 110 Cat Felix European 4 Grumpy Cat Carpet Mainsoon 5 Grumpy Cat Chipie European 8 Cat Chat Persian 3 Grumpy Cat Theo Egyptian 3 Cuddly Cat Mistigri Gris 1 Cat Chien Blue 4 Cuddly Cat
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
