3

In the following code, I have taken in a list of 5 student names, and loading them in an ArrayList of type String.

import java.util.Scanner;
import java.util.ArrayList;
public class QuizAverage
{
    public static void main( String[] args ) {
        final int NAMELIMIT = 5 ;
        final int QUIZLIMIT = 5 ;
        ArrayList<String> sNames = new ArrayList<String>();
        ArrayList<String> sFamily = new ArrayList<String>();
        Scanner in = new Scanner(System.in);
        //Load the 5 names of the students in the arraylist
        for(int i = 1; i<=NAMELIMIT; i++)
        {
            String[] input = in.nextLine().split("\\s+");

            sNames.add(input[0]);
            sFamily.add(input[1]);
        }
        System.out.println("Name: ");
        System.out.println();
        for(int i=0; i<NAMELIMIT; i++)
        {
            System.out.println("Name: " +sNames.get(i) + " " +    sFamily.get(i));
        }
        System.out.println();
   }
}

However, now I am trying to add to the code a part that reads in marks for 5 quizes for each student and loads the quiz marks in An ArrayList of type Integer

So I know I need to use

ArrayList<Integer> quizMarks = readArrayList(readQuiz.nextLine());

and then pass it on to this code which takes the quiz marks and weights them out of 15 instead of 100

public static ArrayList<Integer> readArrayList(String input)
{
    ArrayList<Integer> quiz = new ArrayList<Integer>();

    int i = 1;
    while (i <= QUIZLIMIT)
    {
       if (readQuiz.hasNextInt()) {
        quiz.add(readQuiz.nextInt());
        i++;
       } else {
           readQuiz.next(); // toss the next read token
       }
   }
   return quiz;
 }
 //Computer the average of quiz marks
 public static void computerAverage(ArrayList<Integer>quiz)
 {
    double total = 0 ;
    for(Integer value : quiz)
    {
        total = total + value;
    }
    total *= MAX_SCORE/100;
   System.out.println("Quiz Avg: "+ total / QUIZLIMIT ); 
  }
 }

So my current code with the input:

Sally Mae 90 80 45 60 75
Charlotte Tea 60 75 80 90 70
Oliver Cats 55 65 76 90 80
Milo Peet 90 95 85 75 80
Gavin Brown 45 65 75 55 80

Gives the output

Name: Sally Mae
Name: Charlotte Tea
Name: Oliver Cats
Name: Milo Peet
Name: Gavin Brown

when the desired output is

Name: Sally Mae Quiz Avg: 10.5
Name: Charlotte Tea Quiz Avg: 11.25
Name: Oliver Cats Quiz Avg: 10.95
Name: Milo Peet Quiz Avg: 12.75
Name: Gavin Brown Quiz Avg: 9.6
2
  • 2
    This approach is known as "parallel arrays" and is VERY BAD DESIGN. Create a Student class that stores a student's first name, family name and list of grades. Commented Jul 16, 2016 at 1:19
  • @timNorth Listen to Jim Garrison. I have gone this path and believe me it doesn't end well :P Commented Jul 16, 2016 at 3:28

3 Answers 3

2

As Jim said, you should store the marks and the name of the student in the same class. The reason for doing that is say going forward you want to find the marks of a particular student named Raj.

In your implementation you would have to go through the first array for find the index of the person named Raj and then go to the same index in the marks array. If there was no problem in initializing the first array but there was some problem in creating the marks array you might end up with the wrong marks.

Also if you have to add a new attribute to a student you would have to create a new array and then add one more logic to read that data.

Having a class gives you an easy way to group all the data belonging to a student.

For your problem of averaging, I'd have a class as follows

class Student {
    private String name;
    private Double marks;

    public Student(String name, String marks) {
        this.name = name;
        this.marks = marks;
    }

    //getters setters
}

Have a list of Student

List<Student> students = new ArrayList<Student>();

After reading you will append the object to this list

students.add(new Student(name, marks));

Computing average is as follows

Double average = 0;
for(Student student: students) {
    average += student.getMarks();
}
average = average / student.size();

Checkout https://docs.oracle.com/javase/tutorial/collections/streams/reduction.html for other ways to get the average.

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

Comments

1

Don't use parallel arrays. Use a Student class instead:

public class Student {
    private String name;
    private String family;
    private List<Integer> marks = new ArrayList<>();
    public Student(String name, String family)
    {
         this.name = name;
         this.family = family;
    }

    //  getters, setters
    ...

    public void addMark(int mark)
    {
        this.marks.add(mark);
    }
    public void getAverage()
    {
         ... // etc
    }
}

Then store instances of this class in a single list in your main program

List<Student> students = new ArrayList<>();

Comments

0

You haven't called computerAverage() yet from what I see.

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.