0

i have got this problem. Hope you will understand it. Imagine a high jump competition. We have a program, that will make a matrix filled with random numbers from selected interval. Each line in the matrix stands for individual results. For instance we have matrix 3x3 and first line contains three attempts by one participant. Second line contains results of three attempts made by second participant and so on... Program should also write the highest jump of each participant and at the end, it should put them on some kind of winners podium. it will sort them from highest to lowest. I struggle with the sorting. Tried to use bubble surt but i wasnt succesful. Here is what i have so far:

import java.util.*;
public class Mat {
    int [][]b;
    int m;
    int n ;
    public Mat (int m, int n) {
        this.m = m;
        this.n = n;
        b = new int [m][n];
    }
    public void fillRandomly(int rangeFrom, int rangeTo) {
        Random rnd = new Random(); //that is instance of random numbers generator
        for(int i = 0; i < m; i++)
            for (int j = 0; j < n; j++)
                b[i][j] = rangeFrom + rnd.nextInt(rangeTo - rangeFor + 1);
    }
    public void printout() {
        System.out.println("\n\n\tMatrix:");
        for(int i=0;i<b.length;i++){
            for(int j = 0; j < b[i].length;j++)
                System.out.printf("%4d",b[i][j]); 
            System.out.println();
        }
    }
    public void maxNumberInLine () {   
        for(int i=0; i < b.length; i++){
            int max = b[i][0];
            for(int j=1; j<b[i].length; j++)
                if(max < b[i][j]) {
                    max = b[i][j] ;
                }
            System.out.println("Highest number of line "+i+ " is :" + max );
        }
    }
}
3
  • well, i though that someone could answer me and also post this short code with a little explanation. i am kind of begineer. But thanks anyway :) Nov 23, 2014 at 13:03
  • 1
    You just have to have patience.
    – user4233758
    Nov 23, 2014 at 13:09

2 Answers 2

0

Try this. Please excuse the errors, but I typed it straight here, no testing. You can get the gist of the solution though.

class Results {
   int order, result;
   //getters and setters

   public int compareTo(Object o) {
      if (o instanceof Results) {
          Results r=(Results) o;
          return(new Integer(result)).compareTo(r.getResult());
      }
      return(0);
   }
}

public int maxNumberInLine (int i) {   
            int max = b[i][0];
            for(int j=1; j<b[i].length; j++)
                if(max < b[i][j]) {
                    max = b[i][j] ;
                }
            return(i);        
    }

public void getPodium() {
    List<Results> results=new ArrayList<Results>();
    for (int i=0; i<b.length; i++) {
        Results r=new Results();
        r.setOrder(i);
        r.setResults(maxNumberInLine(i));
        results.add(r);
    }
    Collections.sort(results);
    //Now the podium is composed of the first three positions of the arraylist.
}
0

Given int[m][n] b populated with data, I would approach it as follows:

for (int[] a : b)
    Arrays.sort(a); // sort each jumper's results
List<Integer> podium = new ArrayList<>(m); // to hold indexes of podium order
for (int i = 0; i < m; i++)
    podium.add(i);
// sort the indexes based on the highest jump of the jumper at that index
Collections.sort(podium, new Comparator<Integer>() {
    public int compare(Integer o1, Integer o2) {
        // compare the highest jumps in reverse order
        return Integer.compare(b[o2][n - 1], b[o1][n - 1]);
    }
});

Now podium contains the indexes if the jumpers in highest to lowest order of their best jumps. The indexes of 1st, 2nd and 3rd are:

for (Integer i : podium.subList(0, 3))
    System.out.println(i);
2
  • looks good but compilator says that he cant do that because of this line: " Collection.sort(podium, new Comparator<Integer>() { " . It says that java.util.comparator is abstract, cannot be instantiated Nov 24, 2014 at 7:43
  • @MarekKošík it compiles and runs OK for me. Note that I had one typo: Collections.sort() (not Collection.sort())
    – Bohemian
    Nov 24, 2014 at 10:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.