Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Complete the body of the method readCatsIntoArray where prompted within CatShelter; o Weprovideyouwithsamplecodetoreadfromafile(ReadWrite.java):pleaseuseit. Write a method catCategories that: o TakesanarrayofCatobjects; o Modifiesthisarrayinsuchawaythatallgenericcatscomefirst,followedbygrumpycats,andthenall cuddly cats are

  • Complete the body of the method readCatsIntoArray where prompted within CatShelter;

    o Weprovideyouwithsamplecodetoreadfromafile(ReadWrite.java):pleaseuseit.

  • Write a method catCategories that:

o TakesanarrayofCatobjects; o Modifiesthisarrayinsuchawaythatallgenericcatscomefirst,followedbygrumpycats,andthenall

cuddly cats are at the end of the array. o Returnstheindexofthefirstgrumpycataswellastheindexofthefirstcuddlycat,bundledintoan

array of 2 ints.

  • Write an insertionSortRangeByAgeName that:

    o TakesanarrayCofCatobjects,astartindex,anendindex; o SortstheelementsofarrayCbetweenindexstartandindexend(included),byageandthenbyname,

    following the insertion sort algorithm; o Doesnotreturnanything(Cismodifiedinplace).

  • Writes an selectionSortRangeByAgeName that: o TakesanarrayCofCatobjects,astartindex,anendindex; o SortstheelementsofarrayCbetweenindexstartandindexend(included),byageandthenbyname,

    following the selection sort algorithm; o Doesnotreturnanything(Cismodifiedinplace).

  • Writes an bubbleSortRangeByAgeName that: o TakesanarrayCofCatobjects,astartindex,anendindex; o SortstheelementsofarrayCbetweenindexstartandindexend(included),byageandthenbyname,

    following the bubble sort algorithm; o Doesnotreturnanything(Cismodifiedinplace).

    You are expected to provide 2 test cases per method you have to write.

Here is the code

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 < Shelter.length; i++) { // read each cat profile into the array of cats // First read the type of cat: Cat, Grumpy Cat, Cuddly Cat // Then read the information in the following order: name, breed, age } 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 } } 

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

SQL Server Query Performance Tuning

Authors: Sajal Dam, Grant Fritchey

4th Edition

1430267429, 9781430267423

More Books

Students also viewed these Databases questions

Question

Please make it fast 4 2 1 .

Answered: 1 week ago