← all problems

Exam Free Response 1 Practice: RosterTools.formatRosterLine 🌶️🌶️🌶️

Continuing the RosterTools class from the previous problem. You may assume initials works as specified, regardless of what you wrote for it. Write the formatRosterLine method: given a Student, return a String in the exact format "XY - score (grade)", where XY is the Student's initials, score is their numeric score, and grade is their letter grade. For example, a Student named "Ada Lovelace" with a score of 95 should produce "AL - 95 (A)".

Reference: Student — the methods you can call

class Student

A student record: a name, a score and an attendance percentage. A Student never changes once it is made. You do not write this class; make a record with new Student(name, score, attendance) and call its methods.

Student s = new Student("Ana", 92, 97);
s.getName()                    // "Ana"
s.getLetterGrade()             // "A"
s.isPassing()                  // true
s.toString()                   // "Ana: 92 (A), 97% attendance"
s.equals(new Student("Ana", 92, 97))   // true
MethodWhat it does
Student(String name, int score, int attendance)

Makes a student record.

  • name – the student's name
  • score – the score, as an int
  • attendance – the attendance percentage, as an int
String getName()

The student's name.

Returns the name

int getScore()

The student's score.

Returns the score

int getAttendance()

The attendance percentage.

Returns the attendance percentage

boolean isPassing()

Whether the student is passing.

Returns true if the score is 60 or above

String getLetterGrade()

The letter grade for the score, with the usual 90, 80, 70 and 60 cutoffs.

Returns "A", "B", "C", "D" or "F"

boolean equals(Object o)

Whether another object is a Student with the same name, score AND attendance.

  • o – the object to compare with

Returns true if all three match; false otherwise

String toString()

The student as text, for example "Ana: 92 (A), 97% attendance". It is called for you by System.out.println(s).

Returns the student as text

RosterTools.java — the class below is already written; you're only completing the method in the editable box.

class RosterTools {
    public static String initials(Student s) {
        String name = s.getName();
        int space = name.indexOf(" ");
        String first = name.substring(0, 1);
        String last = name.substring(space + 1, space + 2);
        return first + last;
    }
    // ---- write your method below ----
}