Please I need this to be done in Java using Eclypse Provided classes: package we
ID: 3801247 • Letter: P
Question
Please I need this to be done in Java using Eclypse
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 :longExplanation / Answer
public class SelectionSortExample {
public static void selectionSort(int[] arr){
for (int i = 0; i < arr.length - 1; i++)
{
int index = i;
for (int j = i + 1; j < arr.length; j++){
if (arr[j] < arr[index]){
index = j;//searching for lowest index
}
}
int smallerNumber = arr[index];
arr[index] = arr[i];
arr[i] = smallerNumber;
}
}
public static void main(String a[]){
int[] arr1 = {9,14,3,2,43,11,58,22};
System.out.println("Before Selection Sort");
for(int i:arr1){
System.out.print(i+" ");
}
System.out.println();
selectionSort(arr1);//sorting array using selection sort
System.out.println("After Selection Sort");
for(int i:arr1){
System.out.print(i+" ");
}
}
}
package com.java2novice.sorting;
public class MyQuickSort {
private int array[];
private int length;
public void sort(int[] inputArr) {
if (inputArr == null || inputArr.length == 0) {
return;
}
this.array = inputArr;
length = inputArr.length;
quickSort(0, length - 1);
}
private void quickSort(int lowerIndex, int higherIndex) {
int i = lowerIndex;
int j = higherIndex;
// calculate pivot number, I am taking pivot as middle index number
int pivot = array[lowerIndex+(higherIndex-lowerIndex)/2];
// Divide into two arrays
while (i <= j) {
/**
* In each iteration, we will identify a number from left side which
* is greater then the pivot value, and also we will identify a number
* from right side which is less then the pivot value. Once the search
* is done, then we exchange both numbers.
*/
while (array[i] < pivot) {
i++;
}
while (array[j] > pivot) {
j--;
}
if (i <= j) {
exchangeNumbers(i, j);
//move index to next position on both sides
i++;
j--;
}
}
// call quickSort() method recursively
if (lowerIndex < j)
quickSort(lowerIndex, j);
if (i < higherIndex)
quickSort(i, higherIndex);
}
private void exchangeNumbers(int i, int j) {
int temp = array[i];
array[i] = array[j];
array[j] = temp;
}
public static void main(String a[]){
MyQuickSort sorter = new MyQuickSort();
int[] input = {24,18,11,66,65,28,69,89,59,42};
sorter.sort(input);
for(int i:input){
System.out.print(i);
System.out.print(" ");
}
}
}