I'm stuck on an assignment where I'm supposed to write two methods that sort arrays in ascending and descending order. They both work individually when I add block comments over one of the methods, but when I use them both at the same time I get the same result from both of the methods. The first class I've been given and I'm not supposed to change anything in it. The second class I've written and it's the sortAsc and sortDesc methods that won't work at the same time. Any tips are greatly appreciated!
Exercise4a class :
public class Exercise4a {
public static int[] parseString( String str ) {
String[] nbr = str.split( "," ); // Ex: strängen "13,4,7,9" blir arrayen {"13","4","7","9"}
int[] res = new int[ nbr.length ];
for( int i = 0; i < nbr.length; i++ ) {
res[ i ] = Integer.parseInt( nbr[i] );
}
return res;
}
public static void main(String[] args) {
String message = "";
String str = JOptionPane.showInputDialog( null, "Mata in heltal separerade med komma-tecken", "1,5,3,4,6,5,3,5,9,7,6");
int[] test1 = parseString( str );
int[] test2;
message += "Vektor: " + IntegerArrays.toString( test1 ) + "\n";
message += "Störst: " + IntegerArrays.max( test1 ) + "\n";
message += "Minst: " + IntegerArrays.min( test1 ) + "\n";
message += "Summa: " + IntegerArrays.sum( test1 ) + "\n";
message += "Medelvärde: " + String.format( "%1.2f", IntegerArrays.average( test1 ) ) + "\n";
message += "Range: " + IntegerArrays.range(test1) + "\n";
test2 = IntegerArrays.copy(test1);
message += "test2: " + IntegerArrays.toString( test2 ) + "\n";
IntegerArrays.sortAsc(test1);
IntegerArrays.sortDesc(test2);
message += "test1: " + IntegerArrays.toString( test1 ) + "\n";
message += "test2: " + IntegerArrays.toString( test2 ) + "\n";
JOptionPane.showMessageDialog( null, message );
}
}
IntegerArrays class :
import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.Collections;
public class IntegerArrays {
public static String toString(int[] array){
String res = "{";
for(int i = 0; i < array.length; i++){
res += array[i];
if(i < array.length-1){
res += ",";
}
}res += "}";
return res;
}
public static int max(int[] array){
int max = Integer.MIN_VALUE;
for(int i = 0; i < array.length; i++){
if(array[i] > max){
max = array[i];
}
}return max;
}
public static int min(int[] array){
int min = Integer.MAX_VALUE;
for(int i = 0; i < array.length; i++){
if(array[i] < min){
min = array[i];
}
}return min;
}
public static int sum(int[] array){
int sum = 0;
for(int i = 0; i < array.length; i++){
sum += array[i];
}
return sum;
}
public static double average(int[] array){
double average = 0;
int counter = 0;
for(int i = 0; i < array.length; i++){
average += array[i];
counter++;
}
return average/counter;
}
public static int range(int[] array){
int max = Integer.MIN_VALUE, min = Integer.MAX_VALUE;
for(int i = 0; i < array.length; i++){
if(array[i] <= min ){
min = array[i];
}
else if(array[i] >= max ){
max = array[i];
}
}return max - min;
}
public static int[] sortAsc(int[] array){
// for(int i = 0; i < array.length; i++){
// for(int j = 0; j < array.length; j++){
// if(array[i] > array[j]){
// int temp;
// temp = array[i];
// array[i] = array[j];
// array[j] = temp;
//
// }
// }
// }return array;
Arrays.sort(array);
return array;
}
public static int[] sortDesc(int[] array){
for(int i = 0; i < array.length/2; i++){
int temp = array[i];
array[i] = array[array.length - i - 1];
array[array.length - i - 1] = temp;
}
return array;
}
public static int[] copy(int[] array){
int[] newArray = array;
return newArray;
}
}