Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Create a generic class named ArraySorter that implements the SortsArray interface public interface SortsArray { void insertionSort(Comparator compare, E[] data); void bubbleSort(Comparator compare, E[] data);

Create a generic class named ArraySorter that implements the SortsArray interface

public interface SortsArray { void insertionSort(Comparator compare, E[] data); void bubbleSort(Comparator compare, E[] data); void selectionSort(Comparator compare, E[] data); int getNumberOfSwaps(); int getNumberOfComparisons(); double getSortTime(); }

Each method should implement the named simple sort algorithm on the array data. Use the Comparator to determine the order of the items in the array. Sort the arrays in ascending order. The smallest element should be in position 0, largest in data.length - 1.

The methods should keep track of the number of comparisons, number of swaps and the amount of time it took to sort the array. To record the time taken, your code should call System.nanoTime at the beginning of each of the methods and save the value as the start time. It should then call System.nanoTime again at the end, and print the difference between the two times. The difference may be zero if your system clock does not update the time quickly enough, but otherwise should accurately report how many nanoseconds (billionths of a second) it took to execute your code. When the sort methods complete they should report the number of comparison, swaps, and the execution time in nanoseconds.

Requirements

  • Ensure that your implementation has the following characteristics:

    • The BigO for insertion and bubble sort on sorted data must be BigO(n);

    • The number of swaps for all three sorts on sorted data must be 0.

3. Create some Comparators in package edu.ics211.h02

  • Create a FatComparator that implements Comparator deciding order based upon the Cheeses fat percentage values.

  • Create an TypeComparator that implements Comparator deciding order based upon the type of the Cheeses.

If you created them last week you are done. If not create them in package edu.ics211.h02.

Testing

We are going to use the CheeseSorterTest.java JUnit tests to evaluate your homework for correctness.

You may add any more tests if you want to.

Algorithm Analysis

Write up a runtime analysis of each of the sorting methods, giving the big-O of each, and explaining how you got your results. Your analysis should refer to your code by line number. Send your analysis to the TA together with your code. This part counts for 25% of the grade on this assignment. Even if your code doesnt work, you should do this part to the best of your ability.

Part 2

Create a ReadFile class that reads in a file

We are going to continue exploring handling binary data in Java. You will write a class that reads in a file with a specific format.

The file has the following format:

  • An integer, the number of bytes in the String.
  • A byte, the encoding of the String.
    • 1 means the String is encoded using StandardCharsets.US_ASCII
    • 2 means the String is encoded using StandardCharsets.UTF_16LE
    • 3 means the String is encoded using StandardCharsets.UTF_8
    • 4 means the String is encoded using StandardCharsets.UTF_16
  • The bytes that make up the String.

Tasks

1. Create a class named ReadFile that implements the IReadFile interface

The IReadFile interface has one method in it.

public interface IReadFile { /** * Reads in the given file and returns the contents of the file as a string. * 

The file has the following format:

*

  • *
  • An integer, the number of bytes the String has.
  • *
  • A byte, the encoding of the String.
  • *
    1. *
    2. StandardCharsets.US_ASCII
    3. *
    4. StandardCharsets.UTF_16LE
    5. *
    6. StandardCharsets.UTF_8
    7. *
    8. StandardCharsets.UTF_16
    9. *
    *
  • The String as bytes.
  • *

* @param fileName The name of the file. * @return The String that was encoded in the file. * @throws FileNotFoundException If there is a problem with the file name. * @throws IOException If there is a problem reading the file. */ String readFile(String fileName) throws IOException; }

2. Implement the readFile method

What are the steps you need to read in the contents of the file?

Java has a nice class DataInputStream that allows you to read in different types. I recommend you use this class. Also the String class has a nice constructor that takes an array of bytes and a Charset.

You can test your implementation using ReadFileTest. We will be using this JUnit test to grade this part of the assignment. ReadFileTest test against four files.

  • data1.dat
  • data2.dat
  • data3.dat
  • data4.dat

PLEASE show all codes. Thanks

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

Step: 3

blur-text-image

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

Concepts Of Database Management

Authors: Joy L. Starks, Philip J. Pratt, Mary Z. Last

9th Edition

1337093424, 978-1337093422

More Books

Students also viewed these Databases questions