How Can I display the contents of void method? - java

I am new to programming and Java as well. I need to write a void method which sorts the array entered, I have the code written but do not know how to display the sorted list from void method. Anyone willing to help. It will be greatly appreciated.
package util.assign4;
import java.util.StringTokenizer;
import javax.swing.JOptionPane;
import util.IO;
public class UtilAssign4 {
public static int[] getData (String input){
StringTokenizer st = new StringTokenizer(input);
int []x = new int[st.countTokens()];
for(int i = 0;st.hasMoreTokens(); i++){
try{
x[i] = Integer.parseInt(st.nextToken());
}
catch(Exception e){
JOptionPane.showMessageDialog(null," Invalid input");
System.exit(1);
}
}
return x;
}
public static int getHighest(int g[]){
int hi = g[0];
for( int k = 1; k <g.length;k++)
if(g[k]> hi) hi = g[k];
return hi;
}
public static int getSmallest(int p[]){
int sm = p[0];
for(int l = 1;l<p.length;l++)
if(p[l] < sm) sm = p[l];
return sm;
}
public static float getAverage(int n[]){
float sum = 0.0f;
for(int y = 0;y <n.length; y++) sum += n[y];
return sum/n.length;
}
public static void getSorted(int grades []){
for(int i = 0; i<grades.length-1;i++){
int largest =i;
for(int j = 0;j<grades.length;j++)
if(grades[j]>grades[largest]) largest = j;
int temp = grades[largest];
grades[largest] = grades[i];
grades[i]=temp;
}
}
public static void main(String[] args) {
String input = JOptionPane.showInputDialog("Enetr one or more grades:");
int [] x = getData(input);
int j = getHighest(x);
int m = getSmallest(x);
float a = getAverage(x);
IO.showMsg("Array you entered:" + input + String.format("\nThe"
+ " higheset grade is:%2d \n"
+ "The Lowest Grade is:%2d \n"+"The average is:%2.2f\n"
+ "The sorted list: ",
j,m,a));
}
}

You are not supposed to print the contents of the array in the sort method. Your requirement (I wager) is to sort the array supplied to the method 'in-place' (which it already looks like you are doing). What this means is that given an array:
int[] grades = new int[] {34, 76, 12, 0, -1};
That when you call:
UtilAssign4.getSorted(grades);
That the array passed into the method is actually sorted inside the method, and as such does not need to be returned (that's why your return type is void). So to summarize, before calling the sort method, your array is unsorted. After the call completes, tbe very same array has now been sorted.
So now you can then print out the sorted array in the calling method (in this case main(String[]):
getSorted(x); // <-- call the sort function, on your array
String msg = String.format("\nThe higheset grade is:%2d \n"
+ "The Lowest Grade is:%2d \nThe average is:%2.2f\n"
+ "The sorted list: %s", j, m, a, Arrays.toString(x));
IO.showMsg(msg);
Note the Arrays.toString(x)? That will take your sorted array, and convert it into a string representation (will look something like this: [76, 34, 12, 0, -1]).

in void method short your array in any field Array that is
public class UtilAssign4 {
private Integer[] shorted = new Integer[100];
public static int[] getData (String input){
.
.
}
and do your stuff with above array in your void method and use this where you want

Related

How to apply a method from my main file in my JUnit test?

Our assignment says we should "write the source code and test code for a function named sumArray that accepts an array of ints and returns the sum of all elements from the array".
I think I've got SumArray.java to return sum OK, but I'm struggling to apply my method to the test input. Any help please? TIA.
SumArray.java
package sumarray;
import java.util.Scanner;
public class SumArray {
private static int n, sum = 0;
public static int sumArray() {
Scanner s = new Scanner(System.in);
System.out.print("Enter no. of elements you want in array:");
n = s.nextInt();
int a[] = new int[n];
System.out.println("Enter the elements. (Press [return] after each one)");
for (int i = 0; i < n; i++) {
a[i] = s.nextInt();
sum = sum + a[i];
}
System.out.println("Sum:" + sum);
return sum;
}
public static void main(String[] args) {
sumArray();
}
}
SumArrayTest.java
package sumarray;
import org.junit.Test;
import static org.junit.Assert.*;
public class SumArrayTest {
public SumArrayTest() {
}
/**
* Test of main method, of class SumArray.
*/
#Test
public void testMain() {
System.out.println("main");
String[] args = null;
SumArray.main(args);
int[] intArray = new int[]{2, 3, 4};
int expectedResult = 9;
// int testResult = sumArray({2, 3, 4});
int testResult = SumArray sumArray(intArray);
assertEquals(expectedResult, testResult);
// fail("The test case is a prototype.");
}
}
Edit: I've tried to implement what's been suggested so far with some changes; really not sure of any of this is right; a lot of it is guesswork TBH.
package sumarray;
import java.util.Scanner;
public class SumArray {
private static int n, sum = 0;
public static int sumArray;
public static int sumArray(int[] arr) {
return sum;
}
public static SumArray input() {
Scanner s = new Scanner(System.in);
System.out.print("Enter no. of elements you want in array:");
n = s.nextInt();
int a[] = new int[n];
System.out.println("Enter the elements. (Press [return] after each one)");
for (int i = 0; i < n; i++) {
a[i] = s.nextInt();
sum = sum + a[i];
}
System.out.println("Sum:" + sum);
return new SumArray();
}
public static void main(String[] args) {
SumArray result = input();
System.out.println(result.sumArray(SumArray));
}
}
package sumarray;
import org.junit.Test;
import static org.junit.Assert.*;
public class SumArrayTest {
public SumArrayTest() {
}
#Test
public void testSumArray() {
System.out.println("main");
String[] args = null;
int[] intArray = new int[]{2, 3, 4};
int expectedResult = 9;
assertEquals(expectedResult, SumArray.sumArray(intArray));
// fail("The test case is a prototype.");
}
}
The only error I'm seeing currently is 'cannot find symbol' for SumArray in main.
package sumarray;
import java.util.Scanner;
public class SumArray {
private static int n, sum = 0;
public static int sumArray() {
Scanner s = new Scanner(System.in);
System.out.print("Enter no. of elements you want in array:");
n = s.nextInt();
int a[] = new int[n];
System.out.println("Enter the elements. (Press [return] after each one)");
for (int i = 0; i < n; i++) {
a[i] = s.nextInt();
sum = sum + a[i];
}
System.out.println("Sum:" + sum);
return sum;
}
public static void main(String[] args) {
sumArray();
}
}
The above is the original code you posted. Now, you say you get the correct output. Yes, here you do:
package sumarray;
import java.util.Scanner;
public class SumArray {
private static int n, sum = 0;
public static int sumArray() {
Scanner s = new Scanner(System.in);
System.out.print("Enter no. of elements you want in array:");
n = s.nextInt();
int a[] = new int[n];
System.out.println("Enter the elements. (Press [return] after each one)");
for (int i = 0; i < n; i++) {
a[i] = s.nextInt();
sum = sum + a[i];
}
System.out.println("Sum:" + sum);
return sum;
}
public static void main(String[] args) {
sumArray(); // this one will return the correct answer
sumArray(); // this one will not
}
}
The second one will return wrong data, because you don't reset the value of sum.
You should split the tasks: sumArray should receive an array, and should return the sum of the elements. Either you should change the name of the method, or change the implementation, that is what Mahmoud told you.
package sumarray;
import java.util.Scanner;
public class SumArray {
private static Scanner scan = new Scanner(System.in); // create this on class level, not every execution of your method
public static int[] buildArray(int elements) {
int[] arr = new int[elements];
for ( int i = 0; i < elements; i++ ) {
System.out.println("Enter element nr: " + (i+1));
arr[i] = scan.nextInt();
}
return arr;
}
public static int sumArray(int[] input) {
int sum = 0; // don't use a class level one. especially not a static one, it's value could be altered by another thread
for ( int in : input ) { // iterate over the array and add the values
sum += in; // this should be in -> each iteration we add the value of in (the element of the array) to sum
}
return sum;
}
public static void main(String[] args) {
System.out.println("Provide the size of the array: ");
int param = scan.nextInt();
int[] array = buildArray(param);
int result = sumArray(array);
System.out.println("The sum of the array is: " + result);
}
}
This approach will land you with far lesser issues. It also doesn't have static variables like n and sum in your class that might lead to wrong results.
The main() method is the entry point into the application, you shouldn't test the main() method. Instead, you should test the sumArray() method and compare the expected Vs. the actual returned value from the method.
As a side note, you can better pass the input array to the sumArray() method as a parameter instead of reading it from System.in within the method body.
So your method signature can look like this:
public static int sumArray(int[] arr). The client code which uses this method, which is the main method in your case (or the unit test) can pass the array without bothering the method how this input array was got.

Creating java program assistance

I have to create a java program with two classes and the challenge is =
"Enter in 10 numbers. Calculate the average and display all numbers greater than the average."
I am fairly new to java and I have no idea on what I am doing and how to send array values from one class to another.
import BreezySwing.KeyboardReader;
import javax.swing.*;
public class Average {
public static void main(String[] args) {
KeyboardReader reader = new KeyboardReader();
System.out.print('\u000C');
AverageTest at = new AverageTest();
int numberArray[];
int i;
numberArray = new int[10];
for (i = 0; i < 10; i++) {
numberArray[i] = reader.readInt("Enter a number: ");
at.setnumber(numberArray);
}
}
}
import javax.swing.*;
import BreezySwing.*;
public class AverageTest
{
private int number[];
private int a;
public void setnumber(int number)
{
number = numberArray;
}
}
import java.util.Scanner;
public class AverageTest {
public static void main(String[] args) {
int[] array = new int[10];
// Try with resources, automatically closes the reader once the work is done
// Read 10 integers from the standard input
try (Scanner reader = new Scanner(System.in);) {
for (int i = 0; i < 2; i++) {
System.out.println("Enter a number: ");
array[i] = reader.nextInt();
}
} catch (Exception e) {
e.printStackTrace();
}
// we have an array with 10 numbers, now create an average object by passing
// this array to the Average class constructor
Average averageObj = new Average(array);
// Compute the average
float average = averageObj.average();
System.out.println("Average: " + average);
System.out.println("Numbers greater than average: ");
// Print out the numbers which are greater than or equal to the average
for (int i = 0; i < array.length; i++) {
if (array[i] >= average) {
System.out.println(array[i]);
}
}
}
}
class Average {
private int[] array;
public Average(int[] array) {
if (array == null || array.length == 0) {
throw new IllegalArgumentException("Array cannot be null or empty");
}
this.array = array;
}
public int[] getArray() {
return array;
}
/**
* Computes the average of the given array and returns it.
*/
public float average() {
int sum = 0;
for (int i = 0; i < array.length; i++) {
sum += array[i];
}
return (float) sum/array.length;
}
}
there are 3 steps about this issue:
1.Enter in 10 numbers.
2.Calculate the average.
3.display all numbers greater than the average.
you have done the step 1,that's great.
and I can see that you are trying to do the step 2.
here's the suggestion of your issue:
if you want to send array values from A class to B,you just need to invoke the method of B in the A correctly.
I think I know what you are trying to do.
the problem of your code that you can't send array values from one class to another is because the method's parameter type is not matching.
the method public void setnumber(int number),the parameter is an int type,and you try to refer it to an int array,this's wrong.
first, you need to change the method's definition to public void setnumber(int[] numberarray),and try to figure out why we have to write like this.
then finish the step 2.
Hope it'll help.

Java Sorting Algorithms

I have a class called ThreeSorts.java
The aim is to generate a random arraylist of size n - this works.
Then display the array - this works.
Then I have to prove that these sorting algorithms work, however when I pass the random generated array into one of the sorts like SortA(a);
and then display the array it does not get sorted the output is the same:
Generated ArrayList : (153),(209),(167),(117),(243),(67),(0),(148),(39),(188),
SortA ArrayList : (153),(209),(167),(117),(243),(67),(0),(148),(39),(188),
ThreeSorts.java:
import java.util.*;
public class ThreeSorts
{
private static ArrayList<Integer> CopyArray(ArrayList<Integer> a)
{
ArrayList<Integer> resa = new ArrayList<Integer>(a.size());
for(int i=0;i<a.size();++i) resa.add(a.get(i));
return(resa);
}
public static ArrayList<Integer> SortA(ArrayList<Integer> a)
{
ArrayList<Integer> array = CopyArray(a);
int n = a.size(),i;
boolean noswaps = false;
while (noswaps == false)
{
noswaps = true;
for(i=0;i<n-1;++i)
{
if (array.get(i) < array.get(i+1))
{
Integer temp = array.get(i);
array.set(i,array.get(i+1));
array.set(i+1,temp);
noswaps = false;
}
}
}
return(array);
}
public static ArrayList<Integer> SortB(ArrayList<Integer> a)
{
ArrayList<Integer> array = CopyArray(a);
Integer[] zero = new Integer[a.size()];
Integer[] one = new Integer[a.size()];
int i,b;
Integer x,p;
//Change from 8 to 32 for whole integers - will run 4 times slower
for(b=0;b<8;++b)
{
int zc = 0;
int oc = 0;
for(i=0;i<array.size();++i)
{
x = array.get(i);
p = 1 << b;
if ((x & p) == 0)
{
zero[zc++] = array.get(i);
}
else
{
one[oc++] = array.get(i);
}
}
for(i=0;i<oc;++i) array.set(i,one[i]);
for(i=0;i<zc;++i) array.set(i+oc,zero[i]);
}
return(array);
}
public static ArrayList<Integer> SortC(ArrayList<Integer> a)
{
ArrayList<Integer> array = CopyArray(a);
SortC(array,0,array.size()-1);
return(array);
}
public static void SortC(ArrayList<Integer> array,int first,int last)
{
if (first < last)
{
int pivot = PivotList(array,first,last);
SortC(array,first,pivot-1);
SortC(array,pivot+1,last);
}
}
private static void Swap(ArrayList<Integer> array,int a,int b)
{
Integer temp = array.get(a);
array.set(a,array.get(b));
array.set(b,temp);
}
private static int PivotList(ArrayList<Integer> array,int first,int last)
{
Integer PivotValue = array.get(first);
int PivotPoint = first;
for(int index=first+1;index<=last;++index)
{
if (array.get(index) > PivotValue)
{
PivotPoint = PivotPoint+1;
Swap(array,PivotPoint,index);
}
}
Swap(array,first,PivotPoint);
return(PivotPoint);
}
/////////////My Code////////////////
public static ArrayList<Integer> randomArrayList(int n)
{
ArrayList<Integer> list = new ArrayList<>();
Random random = new Random();
for (int i = 0; i < n; i++)
{
list.add(random.nextInt(255));
}
return list;
}
private static void showArray(ArrayList<Integer> a) {
for (Iterator<Integer> iter = a.iterator(); iter.hasNext();)
{
Integer x = (Integer)iter.next();
System.out.print(("("+x + ")"));
System.out.print(",");
//System.out.print(GetAge());
//System.out.print(") ");
}
System.out.println();
}
static void test(int n) {
//int n = 13;
ArrayList<Integer> a = randomArrayList(n);
System.out.println("Generated ArrayList : ");
showArray(a);
System.out.println("SortA ArrayList : ");
SortA(a);
showArray(a);
}
}
Test is called in main like this ThreeSorts.test(10);
Why is it not getting sorted even tho the random array is passed and there are no errors?
In your sample code you are only testing SortA which reads:
ArrayList<Integer> array = CopyArray(a);
...
return(array);
so actually it is taking a copy of your array, sorting it, and then returning you the sorted array.
So when you test it, instead of using:
SortA(a);
you need to use
a = SortA(a);
The type of data your function SortA returns is ArrayList<Integer>, which means it returns an array list of integers. You need to change the line SortA(a); to a = SortA(a);: this way a variable will receive the results of this function's work.
You have to set the returned value, otherwise a will not be sorted in test method. Change the way you call SortA(a) to:
static void test(int n) {
//int n = 13;
ArrayList<Integer> a = randomArrayList(n);
System.out.println("Generated ArrayList : ");
showArray(a);
System.out.println("SortA ArrayList : ");
a = SortA(a);
showArray(a);
}
instead of the method SortA(a); just use Collections.sort(a); inside static block. Like
ArrayList<Integer> a = randomArrayList(n);
System.out.println("Generated ArrayList : ");
showArray(a);
System.out.println("SortA ArrayList : ");
Collections.sort(a);
showArray(a);
Thats it.

JAVA - Make a Method that Reads a List

I made a bunch of methods and encountered this part of the question:
"Test your methods in a program and include a method that reads a list, terminated by -999, into an array."
I am not sure what to do on this part and hopefully someone can show me and explain how I can do this. Here is my Code:
(NOTE: the comment showing "4-Termination Method" section is empty. That is where I'm trying to make the code)
import java.util.Scanner;
public class Problem3 {
//1-MAXIMUM METHOD//
public static int max(int[] arr)
{
int tmpMax = arr[0];
for(int i = 1; i < arr.length; i++)
{
if(arr[i] > tmpMax)
{
tmpMax = arr[i];
}
}
return tmpMax;
}
//2-MINIMUM METHOD//
public static int min(int[] arr)
{
int tmpMin = arr[0];
for(int i = 1; i < arr.length; i++)
{
if(arr[i] < tmpMin)
{
tmpMin = arr[i];
}
}
return tmpMin;
}
//3-MIN-MAX METHOD//
public static int[] maxMin(int[] arr)
{
int[] myArray = new int [2];
myArray[0] = min(arr);
myArray[1] = max(arr);
return myArray;
}
//4-TERMINATION METHOD//
public static int termination(int[] arr)
{
}
//5-MAIN-METHOD//
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
int a, b, c, d, e;
System.out.println("Input the Numbers: ");
a = input.nextInt();
b = input.nextInt();
c = input.nextInt();
d = input.nextInt();
e = input.nextInt();
int[] test = {a, b, c, d, e};
System.out.println("The Maximum Number is: " + min(test));
System.out.println("The Minimum Number is: " + max(test));
int [] x = maxMin(test);
System.out.println("Min: " + x[0]);
System.out.println("Max: " + x[1]);
}
}
EDIT #1:
It has been nearly 24 hours so I'll give you some more hints regarding the requirement to "include a method that reads a list..."
You want to use a loop that ends when the user inputs a value of -999. You also have to consider that the requirement is to read the values into an array so that means you will have to resize the array each time through the loop.
So your logic would go something like this:
Get the first integer from System.in and store it in a variable
Initialize an array to hold the input values, initial length 0
Begin looping until the variable from #1 equals -999
a) Resize the array from #2
b) Add the integer to the resized array
b) Read the next integer from System.in and store in the same variable as #1
END EDIT #1
I don't want to give too much away too soon - see how to ask homework questions. So please see my comment regarding the requirement "include a method that reads a list...".
I will give you a short example of how to test your code "in a program":
public class TestProblem3{
public static void main(String[] args){
testMin();
}
public static void testMin(){
int[] data = {1,2,3,4,5};
int minValue = Problem3.min(data);
if( minValue != 1 )
System.err.println("FAILURE expected 1, but actual value is "+minValue);
data = new int[]{-1,2,3,4,5};
minValue = Problem3.min(data);
if( minValue != -1 )
System.err.println("FAILURE expected -1, but actual value is "+minValue);
//More tests here!
}
//More test methods here!
}
If you're really ambitious you could learn about JUnit and/or other testing frameworks (like TestNG)

How to make generic Counting Sort Method?

Okay I am a pretty beginner java coder, and I am doing an assignment where I am stuck. I need to create a generic method (sort) that sorts a Type array according to frequency, basically, I am taking the CountingSort Algorithm and making it a generic method. This is where I am lost. I can't seem to figure out how to do this.
Here is a link to my instructions,
https://classes.cs.siue.edu/pluginfile.php/7068/mod_assign/intro/150mp08.pdf
Code:
Driver Class
package mp08;
public class Main {
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
Lists array = new Lists();
array.populateLists();
System.out.println("Original Int List: \n");
array.sort(Lists.intList);
System.out.println("Sorted Int List: \n");
}
}
Lists Class
package mp08;
import java.util.Arrays;
import java.util.Random;
public class Lists {
public static Integer[] intList;
public static Integer[] sortedintList;
public static Integer[] frequency;
public static Character[] charList;
public static Character[] sortedcharList;
public static int MAX_SIZE = 101;
public static int lengthInt;
public static int lengthChar;
public Lists(){
this.intList = new Integer[MAX_SIZE];
this.sortedintList = new Integer[MAX_SIZE];
this.charList = new Character[MAX_SIZE];
this.sortedcharList = new Character[MAX_SIZE];
this.frequency = new Integer[MAX_SIZE];
this.lengthInt = 0;
this.lengthChar = 0;
}
//Makes random integer for populated lists method.
public int randomInt(int min, int max){
Random rand = new Random();
int randomNum = rand.nextInt((max-min)+1)+min;
return randomNum;
}
//Makes random character for populated lists method.
public char randomChar(){
String alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
int N = alphabet.length();
Random rand = new Random();
char randomLet = alphabet.charAt(rand.nextInt(N));
return randomLet;
}
//Populates intList and charList with random values.
public void populateLists(){
for (int i = 0; i < MAX_SIZE; i++) {
intList[i] = randomInt(1,100);
lengthInt++;
}
for (int i = 0; i < MAX_SIZE; i++) {
charList[i] = randomChar();
lengthChar++;
}
}
//Returns sorted array
public Integer[] sorted(){
return intList;
}
public static <T> void sort(T[] array) {
// array to be sorted in, this array is necessary
// when we sort object datatypes, if we don't,
// we can sort directly into the input array
Integer[] aux = new Integer[array.length];
// find the smallest and the largest value
int min = 1;
int max = 101;
// init array of frequencies
int[] counts = new int[max - min + 1];
// init the frequencies
for (int i = 0; i < array.length; i++) {
counts[array[i] - min]++;
}
// recalculate the array - create the array of occurence
counts[0]--;
for (int i = 1; i < counts.length; i++) {
counts[i] = counts[i] + counts[i-1];
}
/*
Sort the array right to the left
1) Look up in the array of occurences the last occurence of the given value
2) Place it into the sorted array
3) Decrement the index of the last occurence of the given value
4) Continue with the previous value of the input array (goto set1),
terminate if all values were already sorted
*/
for (int i = array.length - 1; i >= 0; i--) {
aux[counts[array[i] - min]--] = array[i];
}
}
public static void main(String[] args) {
Integer [] unsorted = {5,3,0,2,4,1,0,5,2,3,1,4};
System.out.println("Before: " + Arrays.toString(unsorted));
Integer [] sorted = sort(unsorted);
System.out.println("After: " + Arrays.toString(sorted));
}
}
I obviously have not finished my driver class yet and I would appreciate any help I can get!
There's no generic way for any Comparable type to get its ordinal number. Sometimes such numbers do not exist at all (for example, String is Comparable, but you cannot map any String to the integer number). I can propose two solutions.
First one is to store counts not in the array, but in TreeMap instead creating new entries on demand (using Java-8 syntax for brevity):
public static <T extends Comparable<T>> void sort(T[] array) {
Map<T, Integer> counts = new TreeMap<>();
for(T t : array) {
counts.merge(t, 1, Integer::sum);
}
int i=0;
for(Map.Entry<T, Integer> entry : counts.entrySet()) {
for(int j=0; j<entry.getValue(); j++)
array[i++] = entry.getKey();
}
}
public static void main(String[] args) {
Integer[] data = { 5, 3, 0, 2, 4, 1, 0, 5, 2, 3, 1, 4 };
System.out.println("Before: " + Arrays.toString(data));
sort(data);
System.out.println("After: " + Arrays.toString(data));
Character[] chars = { 'A', 'Z', 'B', 'D', 'F' };
System.out.println("Before: " + Arrays.toString(chars));
sort(chars);
System.out.println("After: " + Arrays.toString(chars));
}
Such solution looks clean, but probably not very optimal (though its advantage is that it does not care whether all numbers are from 1 to 100 or not).
Another possible solution is to create some additional interface which defines ordering for given type:
public interface Ordering<T> {
int toOrdinal(T obj);
T toObject(int ordinal);
}
public class IntegerOrdering implements Ordering<Integer> {
#Override
public int toOrdinal(Integer obj) {
return obj;
}
#Override
public Integer toObject(int ordinal) {
return ordinal;
}
}
public class CharacterOrdering implements Ordering<Character> {
#Override
public int toOrdinal(Character obj) {
return obj;
}
#Override
public Character toObject(int ordinal) {
return (char)ordinal;
}
}
Now you may make your sort method accepting the ordering parameter:
public static <T> void sort(T[] array, Ordering<T> ordering) { ... }
Every time you need to get counts array index by T object, just call ordering.toOrdinal(object). Every time you need to get object by array index, just use ordering.toObject(index). So, for example, instead of
counts[array[i] - min]++;
Use
counts[ordering.toOrdinal(array[i]) - min]++;
And call the sorting method like this:
sort(characterArray, new CharacterOrdering());
sort(integerArray, new IntegerOrdering());

Categories