-1

I need help with a given assignment to me in our class. We are given an assignment where we are supposed to make a student record with the student's id, first name, last name, course, year level, our prelim grade, midterm grade, final grade, tentative and final grade. The problem revolves around the algorithm I should use to sort the elements in the following arrays in parallel order. Here is a part of my code.

System.out.print("Number of students to record? ");
    count = Integer.parseInt(keyboard.nextLine());

id = new String[count];
    names = new String[count];
    course = new String[count];
    yearLevel = new int[count];
    pGrade = new byte[count];
    mGrade = new byte[count];
    tFGrade = new byte[count];
    fGrade = new byte[count];`
        showData(id, names, course, yearLevel, pGrade, mGrade, tFGrade, fGrade); // Invoke the method for displaying the array elements

// Show the students in sorted order
    System.out.println("Sorted Data");
    showData(id, names, course, yearLevel, pGrade, mGrade, tFGrade, fGrade);
}
public static void populateArrays(String[] id, String[] n, String[] c, int[] y, byte[] p, byte[] m, byte[] t, byte[] f) {
    for (int index = 0; index < n.length; index++) {
        System.out.print("Enter the id number of student " + (index + 1) + ":");
        id[index] = keyboard.nextLine();
        System.out.print("Enter the name of student " + (index + 1) + ":");
        n[index] = keyboard.nextLine();
        System.out.print("Enter the course of student " + (index + 1) + ":");
        c[index] = keyboard.nextLine();
        System.out.print("Enter the year level of student " + (index + 1) + ":");
        y[index] = Integer.parseInt(keyboard.nextLine());
        System.out.print("Enter the prelim grade of student " + (index + 1) + ":");
        p[index] = Byte.parseByte(keyboard.nextLine());
        System.out.print("Enter the midterm grade of student " + (index + 1) + ":");
        m[index] = Byte.parseByte(keyboard.nextLine());
        System.out.print("Enter the tentative final grade of student " + (index + 1) + ":");
        t[index] = Byte.parseByte(keyboard.nextLine());

// compute the final grade of student as the average of prelim, midterm and tentative final grade
        f[index] = (byte) ((1.0 * p[index] + 1.0 * m[index] + 1.0 * t[index]) / 3.0 + 0.5);
    }
    return;
}

This is the part I need help. I don't know what algorithm I should use to sort the element of the arrays. And if so, how should I put all 8 arrays in one sort algorithm? Here is the method.

public static void sortDataBasedOnNames(String[] id, String[] n, String[] c, int[] yLevel, byte[] p, byte[] m, byte[] t,
                                        byte[] f) {

} // end of sortBasedOnNames method

Any help or advice will be greatly appreciated. As you can tell, I am all new to this and really trying my best to wrapped my head on how to solve this algorithm.

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Burrito
  • 19
  • 3

1 Answers1

1

I think that by saying "parallel sorting" the array, you are referring to sort them all together following a certain constraint and not to the "parallel programming sorting approach" since you have said that you are new to this.

The problem is that you have to choose a field by which to start sorting all the arrays. Furthermore, you can solve the problem easily just by doing the following steps:

  1. Creating a class: Student
  2. Insert all the students' data
  3. Call sorting method (based on the particular field).

For instance, if you want to sort the students' array based on their ID, you can just specify it in the sorting method.

In order to achieve step 3, you can do something like this:

  1. Implement a Comparator and pass your array along with the comparator to the sort method which takes it as the second parameter.
  2. Implement the Comparable interface in the class your objects are from and pass your array to the sort method which takes only one parameter.

Otherwise, you can do that using a "lambda expression". A lambda expression is a short block of code that takes in parameters and returns a value. Lambda expressions are similar to methods, but they do not need a name and they can be implemented right in the body of a method.

I'm gonna attach you some useful link where you can find also the code to do that: with more and more approaches:

  1. How to sort an array of objects in Java?
  2. Sorting array of objects by field
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
D. Caruso
  • 163
  • 1
  • 3
  • 11