0

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;
    }

} 
andolsi zied
  • 3,553
  • 2
  • 32
  • 43
Bengan
  • 29
  • 1
  • 3
  • "that won't work at the same time" -- can you explain what you mean? Do you get an error message? – bradimus Nov 14 '16 at 17:49
  • BTW, your `copy` method probably does not do what you think it does. – bradimus Nov 14 '16 at 17:50
  • In the first class where I run the program, if I run the program with both the methods they give the same result, as in they're both descending. Could it be that the copy class is the source of the problem? – Bengan Nov 14 '16 at 17:52
  • 1
    That is the problem. `test1` and `test2` are the same array. See http://stackoverflow.com/questions/5785745/make-copy-of-array-java – bradimus Nov 14 '16 at 17:54
  • And http://stackoverflow.com/questions/40480/is-java-pass-by-reference-or-pass-by-value – bradimus Nov 14 '16 at 17:55
  • I can now run both of the methods without getting the same result. My sortDesc method isn't giving me the same result anymore though, and it's descending correctly. I used return array = Arrays.copyOf(array, array.length); in the copy method. *EDIT* Nvm, I figured it out. I had to sort it in ascending order first, then do descending with the method. Thanks for all the help! – Bengan Nov 14 '16 at 18:09

0 Answers0