Java: A student gradebook

An object can hold a collection of data and answer questions about it.

What to build

A gradebook of students, each with marks.

Requirements

  • Write a class Student with a private name, a private int[] for up to 20 marks and a count, and the methods addMark(int), average(), best() and getName().
  • Commands: add NAME creates a student; mark NAME M adds a mark; show NAME prints NAME: N marks, average A, best B with A to one decimal place, or NAME: no marks. An unknown name prints no student NAME.

Input

Line 1: q. Then q commands.

Output

One line per show command and per unknown name.

Constraints

1 ≤ q ≤ 100; at most 10 students; 0 ≤ M ≤ 100.

Example

Input:

6
add Ada
mark Ada 80
mark Ada 95
show Ada
add Alan
show Alan

Output:

Ada: 2 marks, average 87.5, best 95
Alan: no marks

Why it works

Each Student object keeps its own marks and does its own arithmetic, so the main program only finds the right student and asks it.

Examples

Example 1 · Input
6
add Ada
mark Ada 80
mark Ada 95
show Ada
add Alan
show Alan
Expected output
Ada: 2 marks, average 87.5, best 95
Alan: no marks
Example 2 · Input
2
show Bob
mark Bob 50
Expected output
no student Bob
no student Bob

Hints

Hint 1

Keep the marks in an int array inside the Student object, with a count of how many are used.

Hint 2

The average is a double: divide the total by the count, cast first.

Solution

Show one correct solution (try it yourself first)
import java.util.Scanner;

class Student {
    private final String name;
    private final int[] marks = new int[20];
    private int count = 0;

    Student(String name) {
        this.name = name;
    }

    String getName() {
        return name;
    }

    void addMark(int m) {
        marks[count++] = m;
    }

    int count() {
        return count;
    }

    double average() {
        int total = 0;
        for (int i = 0; i < count; i++) total += marks[i];
        return (double) total / count;
    }

    int best() {
        int b = marks[0];
        for (int i = 1; i < count; i++) b = Math.max(b, marks[i]);
        return b;
    }
}

public class Main {
    static Student[] students = new Student[10];
    static int size = 0;

    static Student find(String name) {
        for (int i = 0; i < size; i++) if (students[i].getName().equals(name)) return students[i];
        return null;
    }

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int q = in.nextInt();
        for (int i = 0; i < q; i++) {
            String cmd = in.next();
            String name = in.next();
            if (cmd.equals("add")) {
                students[size++] = new Student(name);
                continue;
            }
            int mark = cmd.equals("mark") ? in.nextInt() : 0;
            Student s = find(name);
            if (s == null) {
                System.out.println("no student " + name);
            } else if (cmd.equals("mark")) {
                s.addMark(mark);
            } else if (s.count() == 0) {
                System.out.println(name + ": no marks");
            } else {
                System.out.printf("%s: %d marks, average %.1f, best %d%n", name, s.count(), s.average(), s.best());
            }
        }
    }
}

Lesson: Classes and objects →