Question
Hi, I'm passing all the tests for the selectionSort part of this assignment, but instructor says there is still compiler error. Please help me rearrange
Hi, I'm passing all the tests for the selectionSort part of this assignment, but instructor says there is still compiler error. Please help me rearrange so that the tests don't fail and it still compiles.
Instructor Message:
You have a SelectionSort class set up in Assignment04 class.
Errors are:
Assignment04.java:157: error: Illegal static declaration in inner class Assignment04.SelectionSort static int recFindMin(int a[], int i, int j) ^ modifier 'static' is only allowed in constant variable declarations Assignment04.java:185: error: Illegal static declaration in inner class Assignment04.SelectionSort static void recurSelectionSort(int a[], int index) ^ modifier 'static' is only allowed in constant variable declarations Assignment04.java:243: error: Illegal static declaration in inner class Assignment04.SelectionSort static void selectionSort(int a[]) ^ modifier 'static' is only allowed in constant variable declarations Assignment04.java:255: error: Illegal static declaration in inner class Assignment04.SelectionSort public static void main(String args[]) ^ modifier 'static' is only allowed in constant variable declarations 4 errors
import java.util.*;
import java.io.*;
/**
*
* @author
*/
public class Assignment04 {
// Problem #1 Recursive productOfEvens()
private static int productOfEvens(int n) {
if (n < 1)
throw new IllegalArgumentException("Value less than 1 not supported");
else if (n == 1)
return 2;
else
return productOfEvens(n - 1) * (n * 2);
}
// Problem #2 Recursive countDigitMatches()
public static int countDigitMatches(int n1, int n2) {
if (n1 < 0 || n2 < 0) {
throw new IllegalArgumentException();
} else if (n1 == 0 && n2 == 0) {
return 1;
} else {
int result = 0;
if ((n1 % 10) == (n2 % 10)) {
result++;
}
if ((n1 / 10) == 0 || (n2 / 10) == 0) {
return result;
}
return result + countDigitMatches((n1 / 10), (n2 / 10));
}
}
// Problem #3 Recursive generatePattern()
public static String generatePattern(int n) {
String ret = "";
if(n < 1) {
throw new IllegalArgumentException("Passed value is not greater than or equal to 1");
}
if(n == 1) {
ret = "1";
return ret;
} else if (n == 2) {
ret = "11";
return ret;
} else {
int num = (n + 1) / 2;
return num+generatePattern(n - 2)+num;
}
}
// Problem #4 Recursive Find Minimum
private static int recFindMin(int[] array, int current, int minLocation) {
/*
* Terminating condition.
*/
if( (current < 0) || (current == (array.length))) {
return minLocation;
}
/*
* Check if minLocation is -1.
* If yes, assign to first element of whole array
*/
if(minLocation == -1) {
minLocation = current;
}
/*
* check if current first element is smaller than minimum element so far.
*/
if(array[current] < array[minLocation]) {
/*
* Current element is smaller, so update minLocation index.
*/
minLocation = current;
}
/*
* Evaluate further remaining array.
*/
return recFindMin(array, current + 1, minLocation);
}
public static int findMin(int[] a) {
return recFindMin(a, 0, -1);
}
public class SelectionSort {
// method to Return minimum index
static int recFindMin(int a[], int i, int j)
{
//base case
if (i == j)
{
return i;
}
int k = recFindMin(a, i + 1, j);
return (a[i] < a[k]) ? i : k;
}
//recursive method for selection sort
static void recurSelectionSort(int a[], int index)
{
//array length
int n = a.length;
// base case
if (index == n)
{
return;
}
// find minimum
int min = recFindMin(a, index, n - 1);
// Swapping when index and minimum index are not same
if (min != index)
{
// swap
int temp = a[min];
a[min] = a[index];
a[index] = temp;
}
// Recursively calling selection sort function
recurSelectionSort(a, index + 1);
}
//wrapper method for selectionsort
static void selectionSort(int a[])
{
// Calling function
recurSelectionSort(a, 0);
}
// Driver method
public static void main(String args[])
{
int arr[] = {2, 1, 4, 5, 3, 7, 6};
//sort
selectionSort(arr);
//printing sorted array
for (int i = 0; i < arr.length; i++)
{
System.out.print(arr[i] + " ");
}
}
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
int total = 0;
int expectedTotal = 75;
PrintStream out = System.out;
total += testProductOfEvens(out);
total += testCountDigitMatches(out);
total += testGeneratePattern(out);
total += testFindMin(out);
total += testSelectionSort(out);
System.out.printf(" You earned %d out of %d points for CS 143 Assignment 4 ", total, expectedTotal);
}
/**
* Runs all tests for productOfEvens() method
*
* @param outputStream - output stream, used to print into the screen
* @return number of points the problem is worth if all the tests ran
* successfully. If any of the tests failed the method returns a 0.
*/
public static int testProductOfEvens(PrintStream outputStream) {
int expectedCount = 3;
int count = 0;
int pointValue = 15;
outputStream.println(" ----Tests for productOfEvens()----");
// Test #1
if (productOfEvens(1) == 2 && productOfEvens(3) == 48 && productOfEvens(5) == 3840) {
outputStream.printf("%-80s%-10s ", "TEST SET 01 productOfEvens() - regular functionality", "PASSED");
count++;
} else
outputStream.printf("%-80s%-10s ", "TEST SET 01 productOfEvens() - regular functionality", "FAILED");
// Test #2
try {
int ignoreMe = productOfEvens(0);
outputStream.printf("%-80s%-10s ", "TEST SET 01 productOfEvens() - IllegalArgumentException test #1",
"FAILED");
} catch (IllegalArgumentException e) {
outputStream.printf("%-80s%-10s ", "TEST SET 01 productOfEvens() - IllegalArgumentException test #1",
"PASSED");
count++;
}
// Test #3
try {
int ignoreMe = productOfEvens(-1);
outputStream.printf("%-80s%-10s ", "TEST SET 01 productOfEvens() - IllegalArgumentException test #2",
"FAILED");
} catch (IllegalArgumentException e) {
outputStream.printf("%-80s%-10s ", "TEST SET 01 productOfEvens() - IllegalArgumentException test #2",
"PASSED");
count++;
}
int val = 0;
if (count == expectedCount)
val = pointValue;
outputStream.printf(" You earned %d points for productOfEvens() ", val);
return val;
}
/**
* Runs all tests for countDigitMatches() method
*
* @param outputStream - output stream, used to print into the screen
* @return number of points the problem is worth if all the tests ran
* successfully. If any of the tests failed the method returns a 0.
*/
public static int testCountDigitMatches(PrintStream outputStream) {
int expectedCount = 3;
int count = 0;
int pointValue = 15;
outputStream.println(" ----Tests for countDigitMatches()----");
// Test #1
if (countDigitMatches(3, 3) == 1 && countDigitMatches(3, 4) == 0 && countDigitMatches(123456, 1234) == 0
&& countDigitMatches(33333, 3454) == 1) {
outputStream.printf("%-80s%-10s ", "TEST SET 02 countDigitMatches() - regular functionality", "PASSED");
count++;
} else
outputStream.printf("%-80s%-10s ", "TEST SET 02 countDigitMatches() - regular functionality", "FAILED");
// Test #2
try {
int ignoreMe = countDigitMatches(-1, 3);
outputStream.printf("%-80s%-10s ", "TEST SET 02 countDigitMatches() - IllegalArgumentException test #1",
"FAILED");
} catch (IllegalArgumentException e) {
outputStream.printf("%-80s%-10s ", "TEST SET 02 countDigitMatches() - IllegalArgumentException test #1",
"PASSED");
count++;
}
// Test #3
try {
int ignoreMe = countDigitMatches(3, -3);
outputStream.printf("%-80s%-10s ", "TEST SET 02 countDigitMatches() - IllegalArgumentException test #2",
"FAILED");
} catch (IllegalArgumentException e) {
outputStream.printf("%-80s%-10s ", "TEST SET 02 countDigitMatches() - IllegalArgumentException test #2",
"PASSED");
count++;
}
int val = 0;
if (count == expectedCount)
val = pointValue;
outputStream.printf(" You earned %d points for countDigitMatches() ", val);
return val;
}
/**
* Runs all tests for generatePattern() method
*
* @param outputStream - output stream, used to print into the screen
* @return number of points the problem is worth if all the tests ran
* successfully. If any of the tests failed the method returns a 0.
*/
public static int testGeneratePattern(PrintStream outputStream) {
int expectedCount = 3;
int count = 0;
int pointValue = 15;
outputStream.println(" ----Tests for generatePattern()----");
// Test #1
if (generatePattern(1).equals("1") && generatePattern(2).equals("11") && generatePattern(3).equals("212")
&& generatePattern(7).equals("4321234") && generatePattern(8).equals("43211234")) {
outputStream.printf("%-80s%-10s ", "TEST SET 03 generatePattern() - regular functionality", "PASSED");
count++;
} else
outputStream.printf("%-80s%-10s ", "TEST SET 03 generatePattern() - regular functionality", "FAILED");
// Test #2
try {
String ignoreMe = generatePattern(0);
outputStream.printf("%-80s%-10s ", "TEST SET 03 generatePattern() - IllegalArgumentException test #1",
"FAILED");
} catch (IllegalArgumentException e) {
outputStream.printf("%-80s%-10s ", "TEST SET 03 generatePattern() - IllegalArgumentException test #1",
"PASSED");
count++;
}
// Test #3
try {
String ignoreMe = generatePattern(-5);
outputStream.printf("%-80s%-10s ", "TEST SET 03 generatePattern() - IllegalArgumentException test #2",
"FAILED");
} catch (IllegalArgumentException e) {
outputStream.printf("%-80s%-10s ", "TEST SET 03 generatePattern() - IllegalArgumentException test #2",
"PASSED");
count++;
}
int val = 0;
if (count == expectedCount)
val = pointValue;
outputStream.printf(" You earned %d points for generatePattern() ", val);
return val;
}
/**
* Runs all tests for findMin() method
*
* @param outputStream - output stream, used to print into the screen
* @return number of points the problem is worth if all the tests ran
* successfully. If any of the tests failed the method returns a 0.
*/
public static int testFindMin(PrintStream outputStream) {
int expectedCount = 1;
int count = 0;
int pointValue = 15;
outputStream.println(" ----Tests for findMin()----");
int[] a1 = { 1 };
int[] a2 = { 1, 2 };
int[] a3 = { 2, 1 };
int[] a4 = { 2, 1, 4, 3, 1, 5, -6 };
int[] a5 = { 2, 1, 4, 3, -4, 5, 6 };
// Test #1 and ONLY
if (findMin(a1) == 0 && findMin(a2) == 0 && findMin(a3) == 1 && findMin(a4) == 6 && findMin(a5) == 4) {
outputStream.printf("%-80s%-10s ", "TEST SET 04 findMin() - regular functionality", "PASSED");
count++;
} else
outputStream.printf("%-80s%-10s ", "TEST SET 04 findMin() - regular functionality", "FAILED");
int val = 0;
if (count == expectedCount)
val = pointValue;
outputStream.printf(" You earned %d points for findMin() ", val);
return val;
}
/**
* Runs all tests for selectionSort() method
*
* @param outputStream - output stream, used to print into the screen
* @return number of points the problem is worth if all the tests ran
* successfully. If any of the tests failed the method returns a 0.
*/
public static int testSelectionSort(PrintStream outputStream) {
int expectedCount = 1;
int count = 0;
int pointValue = 15;
outputStream.println(" ----Tests for selectionSort()----");
int[] a1 = { 1 };
int[] a2 = { 1, 2 };
int[] a3 = { 2, 1 };
int[] a4 = { 2, 1, 4, 3, 1, 5, -6 };
int[] a5 = { 2, 1, 4, 3, -4, 5, 6 };
// Test #1 and ONLY
SelectionSort.selectionSort(a1);
SelectionSort.selectionSort(a2);
SelectionSort.selectionSort(a3);
SelectionSort.selectionSort(a4);
SelectionSort.selectionSort(a5);
if (isSorted(a1) && isSorted(a2) && isSorted(a3) && isSorted(a4) && isSorted(a5)) {
outputStream.printf("%-80s%-10s ", "TEST SET 05 selectionSort() - regular functionality", "PASSED");
count++;
} else
outputStream.printf("%-80s%-10s ", "TEST SET 05 selectionSort() - regular functionality", "FAILED");
int val = 0;
if (count == expectedCount)
val = pointValue;
outputStream.printf(" You earned %d points for selectionSort() ", val);
return val;
}
/**
* Checks if array is sorted
*
* @param array to be examined
* @return true if array is sorted, false if it is not
*/
public static boolean isSorted(int[] array) {
for (int i = 0; i < array.length - 1; i++) {
if (array[i] > array[i + 1])
return false;
}
return true;
}
}
SelectionSort
import java.util.*;
import java.io.*;
public class SelectionSort {
// method to Return minimum index
static int recFindMin(int a[], int i, int j)
{
//base case
if (i == j)
{
return i;
}
int k = recFindMin(a, i + 1, j);
return (a[i] < a[k]) ? i : k;
}
//recursive method for selection sort
static void recurSelectionSort(int a[], int index)
{
//array length
int n = a.length;
// base case
if (index == n)
{
return;
}
// find minimum
int min = recFindMin(a, index, n - 1);
// Swapping when index nd minimum index are not same
if (min != index)
{
// swap
int temp = a[min];
a[min] = a[index];
a[index] = temp;
}
// Recursively calling selection sort function
recurSelectionSort(a, index + 1);
}
//wrapper method for selectionsort
static void selectionSort(int a[])
{
// Calling function
recurSelectionSort(a, 0);
}
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started