0

I've been searching every where but couldn't find an answer.

I'm trying to make an ArrayList to store 10 grades in a user defined class so I can then call it in the Client class and input the grades with a Scanner(System.in) and then output them all together but I don't know if its possible to do it and I've searched everywhere for an answer. Please Help.

This is what I have so far of my user defined class

import java.util.ArrayList;

public class Student {

private String student;
private String courseName;
private double grade;
private double avg;
private double total;
private double max;
private double min;

//Constructor w/o arguments
public Student() {
    this.student = "";
    this.courseName = "";
    this.grade = 0.0;
}

//Constructor with arguments
public Student(String student, String courseName, double grade, double avg, double total, double max, double min) {
    this.student = student;
    this.courseName = courseName;
    this.grade = grade;
}

//Getters
public String getStudent(){
    return this.student;
}
public String getCourseName() {
    return this.courseName;
}


//Setters
public void setStudent(String student) {
    this.student = student;
}
public void setCourseName(String courseName) {
    this.courseName = courseName;
}


//Returns average off the 10 grades
public double calculateAvg() {
    total += grade; //add all grades for average
    avg = total / 10;
    return avg;
}

//Highest Grade
public double highGrade() {
    return max = Math.max(grade, max);
}
//Lowest Grade
public double lowGrade() {
    return min = Math.min(grade, min);
}

ArrayList<Student>listGrades = new ArrayList<Student>();
4
  • 1
    You can create an ArrayList the same way you create things such as a double. It's just a variable Commented Dec 5, 2018 at 14:33
  • And where is the problem with your last line? It should work but btw List<Student> listGrades = new ArrayList<>(); would work too, java does not need the type specification in the second part (at least not in newer versions). You can do the same with doubles. Commented Dec 5, 2018 at 14:35
  • You want to have 10 student objects or ten subjects for one student or both ? Commented Dec 5, 2018 at 14:35
  • I just need 10 subjects for 1 student Commented Dec 5, 2018 at 14:53

3 Answers 3

2

Not sure to understand what you want but that will do it :

class Student {
  private final List<Double> grades = new ArrayList<>(10);
  private final String name;
  // Constructor, getters & setters omitted. 
}

// ..
Student student = // ...
for (int i=0; i<10; i++) {
    System.out.println("Please enter the "+(i+1)+"th grade:");
    student.getGrades().add(scanner.nextDouble());
}
// ..
Student student = // ...
student.getGrades().forEach(System.out::println);

Note that is not the optimal way to do it. You can search the https://codereview.stackexchange.com/ community for "student grade java" to find a lot of review on similar questions.

Sign up to request clarification or add additional context in comments.

5 Comments

It's probably better to have the ArrayList not be of a base size. Instead, I'd change the for loop to a while loop that runs while the input isn't empty. That way it would allow more than 10 grades, or less, with no problems (empty grades). With that, you'd have to change the (i+1) with something like student.getGrades().size().
@Meyi as said, this is not the optimal way. But initalizing an ArrayList with an initialCapacity, will not influence his size but just reserve the given amount of items in the backed array to avoid using too much space or resizing it (docs.oracle.com/javase/7/docs/api/java/util/…)
Interesting, I didn't know ArrayList<>() already gave it an initial size of 10. It would still be better to update the loop, but thanks for informing me.
@Meyi if you update the for loop to for(int i=0; i<grades.size(); i++) then this loop will never be executed because the initial size of grades is 0. I used a constant of 10 because this is what @Sarid Ruiz want : ".. to store 10 grades.."
@gervias.b I'll quote myself - "I'd change the for loop to a while loop that runs while the input isn't empty." It's always better to make it work for everything. What if you are given more than 10 grades? less than 10? You should make it work for all cases, not just the provided one.
0

Initialize it in a constructor or another method: [https://www.geeksforgeeks.org/initialize-an-arraylist-in-java/][1]

Comments

0

You should try something like this.

public static void main(String[] args) {
            System.out.println("Enter number of Students :");
            Scanner scanner = new Scanner(System.in); 
            Student s=new Student("gagan","Math",0.0d,0.0d, 0.0d, 0.0d,0.0d);
            s.listGrades.add(s);

            for(Student s1:s.listGrades) {
                System.out.println("Enter Grade for student"+s.getStudent()+": ");
                s1.setGrade(scanner.nextDouble());
            }
            scanner.close();


        }

Comments

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.