1

I am working on a java code where it should prompt the user to input the grades of N amount of students and it should output the highest grade, My code is working well but I want to give the user ability to enter the content of the array and I want to create a data type called Student for example for the array.

public class Test3 {
   static double grades[] = {85, 99.9, 78, 90, 98};
       
   static double largest() {
      int i;
      double max = grades[0]; 
      for (i = 1; i < grades.length; i++)
         if (grades[i] > max)
            max = grades[i];
        
      return max;
   }
   public static void main(String[] args) 
   {
      System.out.println("Highest grade is :  " + largest());
   }
}

Any help would be really appreciated.

1

2 Answers 2

0

First, you should create the Student class:

    public class Student {

        private double grade;

        // empty constructor
        public Student() {}
        
        // all args constructor
        public Student(double grade) {
            this.grade = grade;
        }

        public double getGrade() {
            return grade;
        }

        public void setGrade(double grade) {
            this.grade = grade;
        }

        @Override
        public String toString() {
            return "Student{" +
                    "grade=" + grade +
                    '}';
        }
    }

To avoid repetitive code, you can use the Lombok library. It creates for you the constructors, getters, setters, toString(), and much more.

After, you can create a method to input students grades.

    public static Student[] inputStudens() {
        Scanner sc = new Scanner(System.in);
        System.out.println("How many students?");
        int N = sc.nextInt();

        Student[] students = new Student[N];
        for (int i=0; i<N; i++) {
            System.out.println("What's the grade for student " + i + ":");
            double grade = sc.nextDouble();
            Student student = new Student(grade);
            students[i] = student;
        }

        return students;
    }

And... using your already made max finder, but returning the Student instead.

    public static Student maxGrade(Student[] students) {
        int max = 0;
        for (int i = 1; i < students.length; i++) {
            if (students[i].getGrade() > students[max].getGrade()) {
                max = i;
            }
        }
        return students[max];
    }

Now we just need to call these methods in main().

    public static void main(String[] args) {
        Student[] students = inputStudens();
        Student student = maxGrade(students);
        System.out.println("Student with highest grade is: " + student);
    }
Sign up to request clarification or add additional context in comments.

Comments

0

You can create an object and use that object to invoke the array to store value. For example:

test3obj= new Test3(); // creating an object.

Use test3obj to invoke the array to store values like:

test3obj.grades[i]=(object of scanner class).nextDouble();

Object of scanner class:

Scanner obj=new Scanner(System.in); 

1 Comment

to store multiple values make use of a loop like a for loop.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.