0

I am tasked with creating a program to list the grades of students that is entered then find the average, highest mark, lowest mark and then list how many students are a level 0, 1, 2, 3 and 4. I have all the other parts done except the last part. All of the marks are entered into an arrayList and then are sorted.

How do I access all of the elements of the arrayList one after the other so that all of the marks are entered into the correct category? Here is what I have so far (I already initialized the arrayList within the package, and I know that the get statement at the end isn't finished, that's what I need help with):

EDIT: I figured that i should add that the categories that i wish each element be sorted into is an array, so I will have to at some point return five arrays.

public U3A6_Marks_WillCampbellUI() {
    initComponents();
}


ArrayList <Integer> marks = new ArrayList();


private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    // TODO add your handling code here:
    Collections.addAll(marks, Integer.parseInt(jTextField3.getText()));
    StringBuilder text = new StringBuilder();
    for (Integer mark: marks) {
        text.append(mark.toString()).append('\n');
}
    jTextArea1.setText(text.toString());
    jTextField3.setText("");
}                                        

private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    // TODO add your handling code here:
    Collections.sort(marks);
    StringBuilder text = new StringBuilder();
    for (Integer mark: marks) {
        text.append(mark.toString()).append('\n');
}
    jTextArea1.setText(text.toString());
}                                        

/**
 * @param args the command line arguments
 */
private double average(List <Integer> marks) {
    Integer sum = 0;
    if(!marks.isEmpty()) {
    for (Integer mark : marks) {
         sum += mark;
}
 return sum.doubleValue() / marks.size();
}
 return sum;
}
private int highest(int[] marks) {
     int largest = marks[0];  
     for(int i = 1; i < marks.length; i++){  
     if(marks[i] > largest){  
          largest = marks[i];  
    }  
}  
    return largest;
}
private int lowest(int[] marks) {
    int smallest = marks[0];
    for(int i = 0; i > marks.length; i++) {
        if(marks[i] < smallest){
            smallest = marks[i];
        }
    }
    return smallest;
}
private int sorted() {
    int a = marks.get();
}
  • 6
    `U3A6_Marks_WillCampbellUI` is the worst class name I have seen in a while. Not trying to be harsh, but you should read Java's naming conventions. – Sotirios Delimanolis Nov 27 '13 at 15:44
  • It's how my teacher wants them. – user3042393 Nov 27 '13 at 15:58
  • So you want to learn how to sort the marks collection? What the kind order you want to use? – André Nov 27 '13 at 16:09
  • no, marks is already sorted. I need to know how to get the elements of the arrayList put into the proper categories without using five "if" statements for a single element. If there was a way to make it so that it would start at element zero then go to the next element, and so on and sort it into the proper category. – user3042393 Nov 27 '13 at 16:18
  • I already see two for-each loops over `marks` in your code so you know how to do that. Why is a for loop not the answer? – Radiodef Nov 27 '13 at 16:40
  • Yes! There is! You can use a for statement too and check where the element belongs to! [More on for and lists](http://stackoverflow.com/questions/18410035/ways-to-iterate-over-a-list-in-java) Also, if you know that the element 0 belongs. You can do `marks.get(0);` – André Nov 27 '13 at 16:41

1 Answers1

0

It sounds to me like you want a Map from the level (0,1,2,3,4) to the list of grades in that level.

The following code should do that for you:

public Map<Integer, List<Integer>> categorise() {
    final Map<Integer, List<Integer>> categorisedMarks = new HashMap<Integer, List<Integer>>();

    categorisedMarks.put(0, getMarksInRange(0, 20) );
    categorisedMarks.put(1, getMarksInRange(21, 40) );
    categorisedMarks.put(2, getMarksInRange(41, 60) );
    categorisedMarks.put(3, getMarksInRange(61, 80) );
    categorisedMarks.put(4, getMarksInRange(81, 100) );

    return categorisedMarks;
}

private List<Integer> getMarksInRange(final int lowerBonud, final int upperBound) {
    final List<Integer> marksInRange = new ArrayList<Integer>();

    for (final Integer mark : this.marks) {
        if(mark >= lowerBonud && mark <= upperBound) {
            marksInRange.add(mark);
        }
        else if (mark > upperBound) {
            break;
        }
    }

    return marksInRange;
}

You'll end up with a map, that contain 5 entries. Each entry will have the list of grades that exist within the ranges provided.

Dan Temple
  • 2,736
  • 2
  • 22
  • 39