PLEASE HELP ME WITH THIS LAB!!!!! /** * Just a container, really, that serves as
ID: 653928 • Letter: P
Question
PLEASE HELP ME WITH THIS LAB!!!!!
/**
* Just a container, really, that serves as a place to house these public methods:
*
* static Integer[] selectSort( Integer[] incoming );
*
* static Integer[] selectSortReverse( Integer[] incoming );
*
* static int findMin( int fromIndex, Integer[] arrayOfInts );
*
* static int findMax( int fromIndex, Integer[] arrayOfInts );
*
* static void swap( int fromPos, int toPos, Integer[] arrayOfInts);
*
* Note: these MUST be public in order for you to pass any of the tests.
*
* @author UMD CD Department
*
*/
public class SelectionSorter {
/**
* Returns a sorted (from smallest to largest) shallow COPY of the incoming
* array of Integers.
* @param incoming
* @return
*/
public static Integer[] selectSort( Integer[] incoming ) {
throw new UnsupportedOperationException("you must implement this method" );
}
/**
* Returns a sorted (from largest to smallest) copy of the incoming array
* of Integers.
* @param incoming
* @return
*/
public static Integer[] selectSortReverse( Integer[] incoming ) {
throw new UnsupportedOperationException("you must implement this method" );
}
/**
* Return the index of the smallest element in the arrayOfInts, beginning
* the search fromIndex;
* @param fromIndex
* @param arrayOfInts
* @return
*/
public static int findMin( int fromIndex, Integer[] arrayOfInts ) {
throw new UnsupportedOperationException("you must implement this method" );
}
/**
* Returns the index of the largest element in the arrayOfIntegers, beginning
* from the fromIndex.
* @param fromIndex
* @param arrayOfIntegers
* @return
*/
public static int findMax( int fromIndex, Integer[] arrayOfIntegers ) {
throw new UnsupportedOperationException("you must implement this method" );
}
/**
* Swaps the contents in the toPos with the fromPos; note, this
* method does not copy the array, but operates on the arrayOfInts
* directly.
* @param fromPos
* @param toPos
* @param arrayOfInts
*/
public static void swap( int fromPos, int toPos, Integer[] arrayOfInts ) {
throw new UnsupportedOperationException("you must implement this method" );
}
}
Explanation / Answer
//Implementation of static methods int SelectionSorter class
//SelectionSorter.java
public class SelectionSorter
{
//The method selectSort that accepts the Integer array
//and returns the sorted elements as return type
public static Integer[] selectSort( Integer[] incoming )
{
for (int i = 0; i < incoming.length - 1; i++)
{
int index = i;
for (int j = i + 1; j < incoming.length; j++)
if (incoming[j].compareTo(incoming[index])<0)
index = j;
swap(index, i, incoming);
}
return incoming;
}
//The method selectSortReverse that accepts the Integer array
//and returns the reversed elements as return type
public static Integer[] selectSortReverse( Integer[] incoming )
{
Integer[] tempReverese=new Integer[incoming.length];
int tempIndex=0;
for (int index = tempReverese.length-1; index >=0; index--)
{
tempReverese[tempIndex]=incoming[index];
tempIndex++;
}
return tempReverese;
}
//Returns the maximum element in the array
public static int findMin( int fromIndex, Integer[] arrayOfInts )
{
int startIndex=fromIndex;
//Assume startIndex is minimum element
int min=arrayOfInts[startIndex];
//Convert to Integer type
Integer intMin=new Integer(min);
//start for loop from index =1
for (int index = 1; index < arrayOfInts.length; index++)
{
if(arrayOfInts[index].compareTo(intMin)<0)
intMin=arrayOfInts[index];
}
return intMin;
}
//Returns the minimum element in the array
public static int findMax( int fromIndex, Integer[] arrayOfIntegers )
{
int startIndex=fromIndex;
//Assume startIndex is maximum element
int max=arrayOfIntegers[startIndex];
//Convert to Integer type
Integer intMax=new Integer(max);
//start for loop from index =1
for (int index = 1; index < arrayOfIntegers.length; index++)
{
if(arrayOfIntegers[index].compareTo(intMax)>0)
intMax=arrayOfIntegers[index];
}
return intMax;
}
//swaps two elements at index fromPos to toPos
public static void swap( int fromPos, int toPos, Integer[] arrayOfInts )
{
int temp=0;
Integer intTemp=new Integer(temp);
intTemp=arrayOfInts[toPos];
arrayOfInts[toPos]=arrayOfInts[fromPos];
arrayOfInts[fromPos]=intTemp;
}
}
----------------------------------------------------------------------------------------------------------------------
/**
* The java program that the calls the static methods
* selctionSort and findMax and findMin and print the elements
* of the Integer array elements before and after sorting.
* */
//SelectionSortTester.java
public class SelectionSortTester
{
public static void main(String[] args)
{
//Craeate an array of Integer type of size 5
Integer[] elements=new Integer[5];
elements[0]=23;
elements[1]=13;
elements[2]=45;
elements[3]=22;
elements[4]=44;
System.out.println("Before sorting");
print(elements);
//calling the static method selectSort with array of Integer wrapper class
SelectionSorter.selectSort(elements);
System.out.println("After sorting");
print(elements);
//calling findMax and print maximum element
System.out.println("Maximum element :"+SelectionSorter.findMax(0, elements));
//calling findMin and print minimum element
System.out.println("Minimum element :"+SelectionSorter.findMin(0, elements));
System.out.println("Reverse of the array");
Integer[] reveseArray=SelectionSorter.selectSortReverse(elements);
print(reveseArray);
}
//Helper method that prints the elements
private static void print(Integer[] elements)
{
for (int index = 0; index < elements.length; index++)
{
System.out.println("Element : "+elements[index]);
}
}
}
-------------------------------------------------------------------------------------------------------------------------------
sample output:
Before sorting
Element : 23
Element : 13
Element : 45
Element : 22
Element : 44
After sorting
Element : 13
Element : 22
Element : 23
Element : 44
Element : 45
Maximum element :45
Minimum element :13
Reverse of the array
Element : 45
Element : 44
Element : 23
Element : 22
Element : 13
Hope this helps you
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.