0
\$\begingroup\$

What I have to do is to sort the array list and then rank them in descending order.

I have to write a method to sort and display the names according to their final scores decreasingly.

The output should be in the form:

Rank_number1 Name1 new_final_grade1 new_final_score1

e.g.

1 Winnie A 94.4351629426243 

(Rank_number should be from 1 to 26)

Must use this as the method signature:

public static String[] sort_decreasingly(String[] Names, double[] final_score, 
                                         char[], final_grade); 

I have only used selection sort to sort the array list in order, but I don't know how to "link" the three array list together. or I should say I don't know how to swap the three arrays because the

public class assessment_score{

    //Main
    public static void main (String args[]){
        System.out.println(highestAssessment(initial_names(), initial_assessments())+" has the maximum assessment score.");
        System.out.println("Average exam score: "+average_of_exams(initial_exams()));
        //final_scores(initial_names(), initial_assessments(), initial_exams());
        final_grades(initial_names(), final_scores(initial_names(), initial_assessments(), initial_exams()));
        //rescore(initial_names(), final_scores(initial_names(), initial_assessments(), initial_exams()));
        regrade(initial_names(), rescore(initial_names(), final_scores(initial_names(), initial_assessments(), initial_exams())));      

    }

    static String [] initial_names(){
        String [] names = {"Jimmy", "Alex", "Jane", "Suzie", "Phil", "Elena", "James", "Tomas", "George", "Daniel", "Matt", "May", "Aaron", "Peggy", "Joanna", "Victoria", "Hamilton", "Winnie", "Brian", "Neda", "Elma", "Emma", "Nancy", "Karen", "Kenneth", "Richard"};
        return names;
    }

    static double [] initial_assessments(){
        double [] assessments = {90.0, 78.0, 63.5, 75.8, 47.6, 100.0, 47.0, 56.0, 81.0, 97.0, 34.9, 81.3, 77.5, 61.2, 75.0, 57.4, 31.1, 95.3, 84.2, 57.4, 79.5, 73.1, 35.5, 17.9, 87.4, 45.3};

        return assessments;
    }

    static double [] initial_exams(){
        double [] exams = {73.0, 62.5, 62.5, 78.0, 69.0, 58.0, 48.0, 53.5, 71.0, 56.5, 45.5, 70.0, 68.0, 69.0, 83.0, 98.0, 68.0, 80.0, 78.5, 58.0, 73.5, 73.0, 62.5, 62.5, 78.0, 69};

        return exams;
    }

    public static double[] final_scores(String[] Names, double[] Assessments, double[] Exams){
        double [] scores = new double [Assessments.length];
        for (int i = 0; i < scores.length; i++){
            scores [i] = Assessments [i] * 0.6 + Exams [i] * 0.4;

        }
        for (int i = 0; i < scores.length; i++){
            System.out.println(Names[i]+" has score: "+scores[i]);
        }

        return scores;
    }

    public static char[] final_grades(String[] Names, double[] final_score){
        char [] grade = new char [Names.length];
        for (int i = 0; i < grade.length; i++){
            if (final_score[i] >= 80)
            grade[i] = 'A';

                else if (final_score[i] >= 65)
                    grade[i] = 'B';

                else if (final_score[i] >= 50)
                    grade[i] = 'C';

                else if (final_score[i] >= 40)
                    grade[i] = 'D';

                else grade[i] = 'F';
        }

        for (int i = 0; i < grade.length; i++){
            System.out.println(Names[i]+" has grade: "+grade[i]);
        }

        return grade;   
    }

    public static double[] rescore(String[] Names, double[] final_score){
        double [] new_score = new double [Names.length];

        for (int i = 0; i < new_score.length; i++){
             new_score[i] = Math.sqrt(final_score[i]) * 10.0;
        }

        for (int i = 0; i < new_score.length; i++){
            System.out.println(Names[i]+" has a new score: "+new_score[i]);
        }

        return new_score;
    }

    public static char[] regrade(String[] Names, double[] new_final_score){
        char [] grade = new char [Names.length];
        for (int i = 0; i < grade.length; i++){
            if (new_final_score[i] >= 80)
            grade[i] = 'A';

                else if (new_final_score[i] >= 65)
                    grade[i] = 'B';

                else if (new_final_score[i] >= 50)
                    grade[i] = 'C';

                else if (new_final_score[i] >= 40)
                    grade[i] = 'D';

                else grade[i] = 'F';
        }

        for (int i = 0; i < grade.length; i++){
            System.out.println(Names[i]+" has new grade: "+grade[i]);
        }

        return grade;   
    }

    public static int[] sort_decreasingly(String[] Names, double[] final_score, char[] final_grade){
        int [] sort = new int [Names.length];

        int temp;
        int first = 0;

        for (int i = sort.length - 1; i > 0; i--){
            for (int j = 1; j<=i ; j++){
                if (sort[j] > sort[first]){
                first = j;
                }
                temp = sort[first];
                sort[first] = sort[i];
                sort[i] = temp;
            }

        }
    return sort;
    }

}
\$\endgroup\$

closed as off-topic by Eric Stein, SuperBiasedMan, IEatBagels, RobH, janos Nov 5 '15 at 15:11

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions containing broken code or asking for advice about code not yet written are off-topic, as the code is not ready for review. After the question has been edited to contain working code, we will consider reopening it." – Eric Stein, SuperBiasedMan, IEatBagels, RobH, janos
If this question can be reworded to fit the rules in the help center, please edit the question.

  • 1
    \$\begingroup\$ Your description seems to be cut off, please edit it to complete it. Also, is the code you posted working as intended? \$\endgroup\$ – Mat Nov 5 '15 at 12:24
2
\$\begingroup\$

First of all you should use an object-oriented approach, profiting that you are using Java.

You could develop a class Student, which contains a Name, a Score and a Grade. Then you only should have to sort an array of Students. It would be simpler and would take you out of trouble. Another basic approach is to use an:

import java.util.Collections;
import java.util.ArrayList;

ArrayList<Student> students = new ArrayList<>();
// initialize the ArrayList ... 

and invoke the method:

Collections.sort(students);

inherited from Collections. This means that you have to specify the concrete way to sort. This is to add an implementation of Comparable to your Student class and develop the compareTo() method.

class Student implements Comparable<Student> {
    private String Name;
    private double finalScore;
    private char finalGrade;
    // ... some constructor, getters and setters
    @Override
    public int compareTo(Student st){
        return (finalScore > st.finalScore) ? 1 :
               (finalScore < st.finalScore) ? -1 : 0;
    }

This will permit the Collections.sort(ArrayList AL); method to sort properly. I suppose that you want to sort by placing first the best finalScored student. Else, you only have to modify the returning result of the Student.compareTo(Student st); method. Whether it returns 1 or a positive integer it means that the actual Student should be placed before when sorted. Whether it returns a -1 or a negative integer it means that will be placed after. If it returns 0 it means that both have the same sorting value.

// --------------------

If you want to continue with your three arrays, you can otherwise swap all the arrays contents together. That is:

int temp1, array1[];
char temp2, array2[];
// ...initialization...

if(conditionToSwap){
    //swapping the related value to sort:
    temp1 = array1[i];
    array1[i] = array1[j];
    array1[j] = temp1;

    temp2 = array2[i];
    array2[i] = array2[j];
    array2[j] = temp2;
}

That is, to swap all the arrays at the same time, using the same indexes.

But in the end I don't know which criteria do you want to use for sorting students. By final_grade? In that case, you should sort all the three arrays, by evaluating the double numbers on that only array.

PS: Constructor and getters/setters example:

public Student(String Name, double finalScore, char finalGrade){
    this.Name = Name;
    this.finalScore = finalScore;
    this.finalGrade = finalGrade;
}

public String getName(){ return Name; }

public double getFinalScore(){ return finalScore; }

public char getFinalGrade(){ return finalGrade; }

// I don't like to use setters if it's not necessary. It helps to avoid modifying non-changeable data.

Hope it helps. Óscar.

\$\endgroup\$

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