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

Please I need this to be done in Java using Eclypse, make sure to test it with t

ID: 3801353 • Letter: P

Question

Please I need this to be done in Java using Eclypse, make sure to test it with test harness and JUnit

Provided classes:

package week8;

import java.util.List;

import org.junit.runner.Result;

import org.junit.runner.notification.Failure;

/**

* This class executes the JUnit Test specified from the command line This will

* be used by the reference system for testing your code.

*/

public class TestHarness

{

                public static void main(String[] args)

                {

                                trace("TestHarness");

                                try

                                {

                                                Result result = org.junit.runner.JUnitCore

                                                                                .runClasses(Week10JUnitTest.class);

                                                int runs = result.getRunCount();

                                                int ignores = result.getIgnoreCount();

                                                trace(String.format("Runs: %d", runs));

                                                trace(String.format("Ingores: %d", ignores));

                                                int failCount = result.getFailureCount();

                                                if(failCount > 0)

                                                {

                                                                List<Failure> failures = result.getFailures();

                                                                for(Failure fail : failures)

                                                                {

                                                                                trace("FAILED: " + fail.getMessage());

                                                                }

                                                }

                                                else

                                                {

                                                                trace("SUCCESS");

                                                }

                                }

                                catch(Exception ex)

                                {

                                                trace("Unhandled exception: " + ex.getMessage());

                                }

                }

                private static void trace(String msg)

                {

                                System.out.println(msg);

                }

}

package week8;

import static org.junit.Assert.*;

import java.util.ArrayList;

import java.util.Date;

import java.util.Random;

import org.junit.Test;

public class Week8JUnitTest

{

                /**

                * Pass in invalid guesses and get an InvalidArgumentException

                */

                @Test

                public void testSelectionSort()

                {

                                trace("testSelectionSort");

                                int[] testList = generateRandomIntegerList();

                                SortUtility util = new SortUtility();

                                try

                                {

                                                int[] sortedList = util.sort(testList, SORT_ALGORITHM_TYPE.SELECTION);

                                               

                                                if( !verifySort(sortedList) )

                                                {

                                                                fail("SelectionSort failed");

                                                }

                                               

                                                String msg = String.format("Elapsed time: %d ms", util.getElapsedTime());

                                                trace(msg);

                                }

                                catch(NotImplementedException ex)

                                {

                                                fail("Sort selection not implemented " + ex.getMessage());

                                }

                                catch(Exception ex)

                                {

                                                fail("Unexpected exception " + ex.getMessage());

                                }

                }

               

                /**

                * Pass in invalid guesses and get an InvalidArgumentException

                */

                @Test

                public void testQuickSort()

                {

                                trace("testQuickSort");

                                int[] testList = generateRandomIntegerList();

                                SortUtility util = new SortUtility();

                                try

                                {

                                                int[] sortedList = util.sort(testList, SORT_ALGORITHM_TYPE.QUICK);

                                               

                                                if( !verifySort(sortedList) )

                                                {

                                                                fail("QuickSort failed");

                                                }

                                               

                                                String msg = String.format("Elapsed time: %d ms", util.getElapsedTime());

                                                trace(msg);

                                }

                                catch(NotImplementedException ex)

                                {

                                                fail("Sort quick not implemented " + ex.getMessage());

                                }

                                catch(Exception ex)

                                {

                                                fail("Unexpected exception " + ex.getMessage());

                                }

                }

               

                /**

                * Verifies the list is sorted smallest to largest

                * @param list list to verify

                * @return true if sorted, otherwise false

                */

    private boolean verifySort(int[] list)

    {

        boolean result = true;

        for(int i = 0; i < list.length - 2; i++)

        {

            int nextInt = i+1;

                if(list[i] > list[nextInt])

            {

                String msg = String

                        .format("Unsorted valies at index %d and %d", i, nextInt);

                trace(msg);

                result = false;

                break; // early out

            }

        }

        return result;

    }                         

               

                /**

                * Random integers, must all be unique

                */

                private int[] generateRandomIntegerList()

                {

                                Random rand = new Random();      

        int[] list = new int[LIST_SIZE];

        for(int i = 0; i < LIST_SIZE; i++)

        {

            int val = rand.nextInt(LIST_SIZE);

            list[i] = val;

        }

        return list;

                }

               

                private void trace(String msg)

                {

                                System.out.println(msg);

                }

               

                //private int[] m_list;

                private static int LIST_SIZE = 100000;

}

The Key elements to this assignment are Abstract base classes, modularity, capturing elapsed time, pluggable algorithms. research and implement Selectionsot, and Quicksort algorithms in this assignment. Your task will be to figure out how to accomplish the task assigned. You CANNOT use the Java libraries directly for this assignment. You MUST implement the algorithms yourself. You must provide J comments for the public methods defined. the SORT ALGORITHM TYPE is a separate file and you must implement the supporting classes AbstractSort NolmplementationException. Sortutility and Stopwatch. This is typical software development where the key capability requires a number of infrastructure items to be effectively implemented SortUtility Abstract Sort plemented Exception Notlm m-name string m. mType SORT ALGORITHM TYPE getElapsed Time() :long sortintO intl getsortAlgorithmosORTLALGORITHM-TYPE) Abstractsort Exception (String) sortint0. SORT ALGORITHM TYPE) :void Abstracts ort(String Exception(String. Throwable) «enumerations SORT ALGORITHM-TYPE Selection Sort0 QuickSort0 sort into) :int0 sortint0) int0 StopWatch m startTime :long m. stop Time long getElapsed TimeMilliseconds0 long getStartTime0 :long getstop Time(0 :long

Explanation / Answer

NotImplementedException.java


public class NotImplementedException extends Exception
{
   /**
   * Constructs a new exception with null as its detail message.
   */
    public NotImplementedException(){
    }
  
    /**
     * Constructs a new exception with the specified message for details.
     * @param arg0 the detailed message.
     */
    public NotImplementedException(String arg0)
    {
        super(arg0);
    }

    /**
     * Constructs a new exception with the specified detail message and cause.
     * @param arg0 The detailed messaged.
     * @param arg1 The cause.
     */
    public NotImplementedException(String arg0, Throwable arg1)
    {
        super(arg0, arg1);
    }
}

StopWatch.java


public class StopWatch {
   private long m_startTime;
   private long m_stopTime;
  
   /**
   * The default constructor.
   */
   public StopWatch(){}
  
   /**
   * Start the stop watch.  
   */
   public void start(){
       m_startTime = System.currentTimeMillis();
   }
  
   /**
   * Stop the stop watch.
   */
   public void stop(){
       m_stopTime = System.currentTimeMillis();
   }

   /**
   * This method returns the start time in miliseconds.
   * @return The start time in miliseconds.
   */
   public long getStartTime() {
       return m_startTime;
   }

   /**
   * This method returns the stop time in miliseconds.
   * @return The stop time in miliseconds.
   */
   public long getStopTime() {
       return m_stopTime;
   }
  
   /**
   * This method returns the elapsed time in miliseconds.
   * @return The time elapsed in miliseconds.
   */
   public long getElapsedTimeMiliseconds(){
       return m_stopTime - m_startTime;
   }
  
}

SORT_ALGORITHM_TYPE.java

public enum SORT_ALGORITHM_TYPE {
   SELECTION,
   QUICK;
}


SelectionSort.java


public class SelectionSort extends AbstractSort {

   /**
   * Constructor of the class.
   * @param name The name of the sorting algorithm.
   */
   public SelectionSort(String name) {
       super(name);
   }

   /**
   * This method performs the selection sort.
   * Inspired by: http://mathbits.com/MathBits/Java/arrays/SelectionSort.htm
   * @param The array to sort.
   * @return The sorted array.
   */
   @Override
   public int[] sort(int[] array) {
       for (int i = 0; i < array.length - 1; i++) {

           int first = i;

           for (int j = i + 1; j < array.length; j++) {

               if (array[j] < array[first])
                   first = j;
           }

           int temp = array[first];
           array[first] = array[i];
           array[i] = temp;
       }
      
       return array;
   }
}


SortUtility.java


public class SortUtility {
   private SORT_ALGORITHM_TYPE m_sortAlgorithmType;
   private StopWatch m_timer;

   /**
   * Constructor of SortUtility class.
   * Instantiates an instance of StopWatch().
   */
   public SortUtility() {
       m_timer = new StopWatch();
   }
  
   /**
   * This method will time how long it takes to sort an array of integers
   * and call a method to sort the array.
   * @param array The array to be sorted.
   * @param sortAlgorithmType The sorting algorithm to be used.
   * @return The sorted array.
   * @throws NotImplementedException If the sorting algorithm has not been implemented, NotImplementedException
   * will be thrown.
   */
   public int[] sort(int[] array, SORT_ALGORITHM_TYPE sortAlgorithmType) throws NotImplementedException {
       int[] sortedArray;

       // Start the timer
       m_timer.start();

       // Sort the array
       sortedArray = getSortAlgorithm(sortAlgorithmType).sort(array);

       // Stop the timer
       m_timer.stop();

       return sortedArray;
   }

   /**
   * This method returns the time it took to sort the array.
   * @return The time it took to sort the array.
   */
   public long getElapsedTime() {
       return m_timer.getElapsedTimeMiliseconds();
   }

   /**
   * This method will return an instance of the appropriate sorting algorithm.
   * @param sortAlgorithmType The sorting algorithm chosen for the sort.
   * @return An instance of the chosen sorting algorithm.
   * @throws NotImplementedException If the sorting algorithm has not been implemented, NotImplementedException
   * will be thrown.
   */
   public AbstractSort getSortAlgorithm(SORT_ALGORITHM_TYPE sortAlgorithmType) throws NotImplementedException {
       AbstractSort sortMethod = null;

       switch (sortAlgorithmType) {
       case SELECTION:
           sortMethod = new SelectionSort(sortAlgorithmType.toString());
           break;
       case QUICK:
           sortMethod = new QuickSort(sortAlgorithmType.toString());
           break;
       default:
           throw new NotImplementedException("The selected sorting algorithm has not been implemented.");
       }

       return sortMethod;
   }
}

AbstractSort.java


public abstract class AbstractSort {
   private String m_name;
  
   /**
   * The class constructor.
   * The constructor takes one parameter: the name of the sorting algorithm.
   * @param name The name of the sorting algorithm.
   */
   public AbstractSort(String name){
       m_name = name;
   }
  
   /**
   * Abstract sort method that must be implemented concretely by classes inheriting from this abstract class.
   * @param array The array to sort.
   * @return The sorted array.
   */
   public abstract int[] sort(int[] array);
}


QuickSort.java


public class QuickSort extends AbstractSort {

   /**
   * Constructor of the QuickSort class.
   * @param name The name of the sorting algorithm.
   */
   public QuickSort(String name) {
       super(name);
   }

   /**
   * This method performs the quick sort.
   * Inspired by: http://www.algolist.net/Algorithms/Sorting/Quicksort
   * @param The array to sort.
   * @return The sorted array.
   */
   @Override
   public int[] sort(int[] array) {
       quickSort(array, 0, array.length-1);
      
       return array;
   }

   private void quickSort(int arr[], int left, int right) {
       int index = partition(arr, left, right);
       if (left < index - 1)
           quickSort(arr, left, index - 1);
       if (index < right)
           quickSort(arr, index, right);
   }

   private int partition(int arr[], int left, int right) {

       int i = left, j = right;
       int tmp;
       int pivot = arr[(left + right) / 2];

       while (i <= j) {
           while (arr[i] < pivot)
               i++;
           while (arr[j] > pivot)
               j--;
           if (i <= j) {
               tmp = arr[i];
               arr[i] = arr[j];
               arr[j] = tmp;
               i++;
               j--;
           }
       }

       return i;
   }
}

Week10JUnitTest.java


import static org.junit.Assert.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.Random;

import org.junit.Test;


public class Week10JUnitTest
{
   /**
   * Pass in invalid guesses and get an InvalidArgumentException
   */
   @Test
   public void testException()
   {
       trace("testException");

       int[] testList = generateRandomIntegerList();
       SortUtility util = new SortUtility();
       try
       {
           int[] sortedList = util.sort(testList, SORT_ALGORITHM_TYPE.QUICK);
          
           if( !verifySort(sortedList) )
           {
               fail("Sort failed");
           }
          
           String msg = String.format("Elapsed time: %d ms", util.getElapsedTime());
           trace(msg);
       }
       catch(NotImplementedException ex)
       {
           fail("Sort selection not implemented " + ex.getMessage());
       }
   }
  
   /**
   * Verifies the list is sorted smallest to largest
   * @param list list to verify
   * @return true if sorted, otherwise false
   */
    private boolean verifySort(int[] list)
    {
        boolean result = true;

        for(int i = 0; i < list.length - 2; i++)
        {
            int nextInt = i+1;
           if(list[i] > list[nextInt])
            {
                String msg = String
                        .format("Unsorted valies at index %d and %d", i, nextInt);

                trace(msg);
                result = false;
                break; // early out
            }
        }

        return result;
    }      
  
   /**
   * Random integers, must all be unique
   */
   private int[] generateRandomIntegerList()
   {
       Random rand = new Random();     
        int[] list = new int[LIST_SIZE];

        for(int i = 0; i < LIST_SIZE; i++)
        {
            int val = rand.nextInt(LIST_SIZE);
            list[i] = val;
        }

        return list;
   }
  
   private void trace(String msg)
   {
       System.out.println(msg);
   }
  
   //private int[] m_list;
   private static int LIST_SIZE = 100000;
}

TestHarness.java

import java.util.List;

import org.junit.runner.Result;
import org.junit.runner.notification.Failure;


public class TestHarness
{
   public static void main(String[] args)
   {
       trace("TestHarness");
       try
       {
           Result result = org.junit.runner.JUnitCore
                   .runClasses(Week10JUnitTest.class);
           int runs = result.getRunCount();
           int ignores = result.getIgnoreCount();
           trace(String.format("Runs: %d", runs));
           trace(String.format("Ingores: %d", ignores));
           int failCount = result.getFailureCount();
           if(failCount > 0)
           {
               List<Failure> failures = result.getFailures();
               for(Failure fail : failures)
               {
                   trace("FAILED: " + fail.getMessage());
               }
           }
           else
           {
               trace("SUCCESS");
           }
       }
       catch(Exception ex)
       {
           trace("Unhandled exception: " + ex.getMessage());
       }
   }

   private static void trace(String msg)
   {
       System.out.println(msg);
   }
}