-2

Can anybody explain how I get the highest value from all the objects I have created in my main class?

In this example I have created 2 students objects in my main class and added some course names and grades.

I have made 2 static arrays to store the information from the different objects but it doesn't return the object with the highest grade.

How do I store the highest grade from the student objects created?

public class CMate {
    private static String[] studentName = new String[2];
    private static int[][] gradeAssigned = new int[2][3];
    private static int higGrade = 0;
    private String name;
    private int cpr;
    private String[] courseName = new String[3];
    private int[] grade = new int[3];
    private int numberOfCourse;
    private int numberOfGrades;
    private static int counter = 0;

    CMate(String name, int cpr) {
        // building constructor
        System.out.println("Creating object nr " + counter);
        this.name = name;
        this.cpr = cpr;
        // insert name into array   
        studentName[counter] = name;
        for (int i = 0; i < numberOfCourse; i++) {
            gradeAssigned[counter][i] = grade[i];
        }
        counter++;
    }

    public void addcourseName(String nameOfCourse) {
        courseName[numberOfCourse] = nameOfCourse;
        numberOfCourse++;
    }

    public void addGrade(int gradeNr) {
        grade[numberOfGrades] = gradeNr;
        numberOfGrades++;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getCpr() {
        return cpr;
    }

    public void setCpr(int cpr) {
        this.cpr = cpr;
    }

    public String[] getCourseName() {
        return courseName;
    }

    public void setCourseName(String[] courseName) {
        this.courseName = courseName;
    }

    public int[] getGrade() {
        return grade;
    }

    public void setGrade(int[] grade) {
        this.grade = grade;
    }

    public String toString() {
        String studentInfo = "";
        System.out.println("------------------------------\n student " + name + " CPR " + cpr);

        for (int i = 0; i < numberOfCourse; i++) {
            studentInfo += " Course " + courseName[i] + "Grade  " + grade[i];
        }
        return studentInfo;
    }

    public double averageGrade() {
        double total = 0;

        for (int i = 0; i < numberOfGrades; i++) {
            total += grade[i];
        }
        return (total / numberOfGrades);
    }

    public void higestGrade() {
        for (int i = 0; i < studentName.length; i++) {
            if (grade[i] > higGrade) {
                higGrade = grade[i];
            }
        }
    }

    public void higestObjectGrade() {
        System.out.println(higGrade);
    }
}
DavidPostill
  • 7,734
  • 9
  • 41
  • 60
Rangerguy
  • 69
  • 8
  • possible duplicate of [Sort an array of objects in Java](http://stackoverflow.com/questions/14953934/sort-an-array-of-objects-in-java) – Chetan Kinger May 25 '15 at 15:57
  • But i want to find the higest grade that has been assigned among all the objects created (Students); – Rangerguy May 25 '15 at 15:59
  • **after sort** the array has values in ascending /descending order . it will be either 0 or nth element – Srinath Ganesh May 25 '15 at 17:19
  • or do as follows , loop the array *gradeAssigned* .. have an variable maxFound= gradeAssigned[0][0] (first element)... and in loop check if any is bigger than maxFound then update it – Srinath Ganesh May 25 '15 at 17:23

1 Answers1

0

I don't fully understand your code, but here's a simplified version, including a static field to keep all students, and a static function to return the highest grade:

class Student {

  public final static List<Student> allCreatedStudents = new ArrayList<Student>();

  String name;
  int[] grades = new int[3];

  public Student(String name){
    this.name = name;
    allCreatedStudents.add(this); // Every time a student is created, he is recorded in the static list
  }

  public void setGrade(int grade, int index){
    this.grades[index] = grade;
  }

  public static int getHighestGrade(){
    int highestGrade = 0;
    for(Student s : allCreatedStudents){  // Loop through all students
      for(int i=0; i<s.grades.length; i++){  // Loop through all grades
        if(s.grades[i]>highestGrade)
          highestGrade = s.grades[i];
      }
    }
    return highestGrade;
  }

}
Eric Leibenguth
  • 4,167
  • 3
  • 24
  • 51
  • from the code of @Rangerguy it can be **assumed** that he has no knowledge if java.util.Collection . so i feel sticking to array will be more understandable to him – Srinath Ganesh May 25 '15 at 17:37