In: Computer Science
**only need part c and d answered below. It is in bold. In java, please implement the classes below. Each one needs to be created. Each class must be in separate file. The expected output is also in bold. I have also attached the driver for part 4.
2. The StudentRoster class should be in the assignment package.
a. There should be class variable for storing multiple students (the roster), semester and year. All class variables of the Student class must be private.
b. public StudentRoster(String semester, int year) is the single constructor of the class. All arguments of the constructor should be stored as class variables. There should no getters or setters for semester and year variables.
c. public boolean addStudent(String singleStudentData) takes a single student record as a string and breaks the string (tokenize) into first name, last name, grade1, grade2, grade3. The tokens are separated by comma. These values are used to create a Student object. If the student is already in the roster, then only the grades are updated (an additional student object is not added). If the student is not in the roster, then the Student object is added to the roster. The method returns true if a new student was added, return false otherwise.This method can generate two exceptions, BadGradeException and BadNameException. If first or last names are absent, or an integer is present where first and last names should be, then a BadNameException is generated. The message of this exception should state if it was caused by issues with first or last name. Similarly, a BadGradeException is generated if grades 1, 2 or 3 is absent or is not an integer. The message of this exception should state if it was caused by issues with grades 1, 2 or 3. The exceptions are not handled in this method.
d. public int addAllStudents(Scanner allStudents) takes a scanner object that contains multiple student records. Each student record is separated by a newline. This method must use theaddStudent method by retrieving one student record at a time from the Scanner object and passing that record to the addStudent method. The method returns the number of new students that were successfully added to the roster. This method handles all exceptions generated by the addStudent method. Before returning the count of successful additions to the roster, print to the console the total of each exception handled by the method.
The A4Driver class is attached below for guidance. I have also attached the expected output of the entire program.
package driver;
import java.util.Scanner;
import assignment.Student;
import assignment.StudentRoster;
public class A4Driver {
public static void main(String[] args) {
//test student class
System.out.println("test Student
class\n++++++++++++++++++++++++++++");
Student s = new Student("Bugs", "Bunny");
//same student
Student s3 = new Student("Bugs",
"Bunny");
System.out.println("Are students s
and s3 the same?: "+s.equals(s3)); //true
//not the same student
Student s2 = new Student("Buga",
"Bunny");
System.out.println("Are students s
and s2 the same?: "+s.equals(s2)); //false
//uncomment to check if
exception is thrown
s.equal(new Object());
//compute average 90
s.setGrade1(100);
s.setGrade2(90);
s.setGrade3(80);
System.out.println("Student s
average for the 3 grades is "+s.computeAverage());
System.out.println("\n\ntest
StudentRoster class\n++++++++++++++++++++++++++++");
String text =""
+ "Bugs, Bunny, 100 , 90, 80\n"
+ "Buga, Bunny, 100 , 80, 90\n"
+ "123, Bunny, 100 , 90, 80\n"
+ "Bugs, 1234 , 100 , 90, 80\n"
+ "BUGS, Bunny, 100 , 90, 80\n"
+ "Bugs, BUNNY, 100 , 90, 80\n"
+ "bugs, bunny, 70 , 90, 80\n"
+ "Betty, Davis, 100 , , \n"
+ "Betty, Davis, 100 , 40 , \n"
+ "Betty, Davis, , , \n"
+ "Betty, Davis, xx ,yy ,zz \n"
;
Scanner scan = new Scanner(text);
StudentRoster sr = new StudentRoster("Fall",
2019);
// Add only two students
System.out.println("\nTest addAllStudents, number of new students
added: "+sr.addAllStudents(scan));
//average of the two students should be 85
System.out.println("\nTest computeClassAverage, class
average is: "+sr.computeClassAverage());
//rank by grade1 Buga, Bugs
System.out.println("\nTest rankByGrade1:
"+sr.rankByGrade1());
//rank by grade2 Bugs, Buga
System.out.println("\nTest rankByGrade2:
"+sr.rankByGrade2());
//rank by grade3 Buga, Bugs
System.out.println("\nTest rankByGrade3:
"+sr.rankByGrade3());
//rank by average Buga, Bugs
System.out.println("\nTest rankByAverage:
"+sr.rankByAverage());
}
}
Example output:
__________Example from A4Driver shown below
test Student class ++++++++++++++++++++++++++++ Are students s and s3 the same?: true Are students s and s2 the same?: false Student s average for the 3 grades is 90.0
test StudentRoster class ++++++++++++++++++++++++++++ Exceptions Caught and Handled ========================== First name: 1
Last name: 1 Grade 1: 2 Grade 2: 1 Grade 3: 1
Test addAllStudents, number of new students added:
2
Test computeClassAverage, class average is: 85.0
Test rankByGrade1: [Buga Bunny grade1:100 grade2:80 grade3:90, Bugs
Bunny grade1:70 grade2:90 grade3:80]
Test rankByGrade2: [Bugs Bunny grade1:70 grade2:90 grade3:80, Buga Bunny grade1:100 grade2:80 grade3:90]
Test rankByGrade3: [Buga Bunny grade1:100 grade2:80 grade3:90, Bugs Bunny grade1:70 grade2:90 grade3:80]
Test rankByAverage: [Buga Bunny grade1:100 grade2:80 grade3:90, Bugs Bunny grade1:70 grade2:90 grade3:80]
BadGradeException.java
public class BadGradeException extends
IllegalArgumentException{
public BadGradeException(String msg)
{
super(msg);
}
}
BadNameException.java
public class BadNameException extends
IllegalArgumentException{
public BadNameException(String msg)
{
super(msg);
}
}
NotAStudentException.java
public class NotAStudentException extends
IllegalArgumentException{
public NotAStudentException(String msg)
{
super(msg);
}
}
Student.java
public class Student {
private String firstName, lastName;
private int grade1, grade2, grade3;
public Student(String fname, String lname)
{
this.firstName = fname;
this.lastName = lname;
this.grade1 = 0;
this.grade2 = 0;
this.grade3 = 0;
}
public String getFName() {
return firstName;
}
public String getLName() {
return lastName;
}
public double computeAverage()
{
double sum = (double)(this.grade1 + this.grade2 +
this.grade3);
return(sum / 3.0);
}
// GETTERS
public int getGrade1() {
return grade1;
}
public int getGrade2() {
return grade2;
}
public int getGrade3() {
return grade3;
}
// SETTERS
public void setGrade1(int grade1) {
this.grade1 = grade1;
}
public void setGrade2(int grade2) {
this.grade2 = grade2;
}
public void setGrade3(int grade3) {
this.grade3 = grade3;
}
@Override
public boolean equals(Object otherStudent)
{
if(otherStudent instanceof Student) return (((Student)
this).getFName().equals(((Student) otherStudent).getFName())
&&
((Student) this).getLName().equals(((Student)
otherStudent).getLName()));
else
throw new NotAStudentException("Object is not a Student
object.");
}
@Override
public String toString()
{
return("Name: " + this.firstName + " " + this.lastName + ", Grade
1: " + this.grade1
+ ", Grade2: " + this.grade2 + ", Grade3: " + this.grade3);
}
}
StudentRoster.java
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Scanner;
public class StudentRoster{
private ArrayList<Student> students;
private String semester;
private int year;
public StudentRoster(String semester, int year)
{
this.students = new ArrayList<>();
this.semester = "";
this.year = 0;
}
public boolean addStudent(String singleStudentData)
{
boolean isAdded;
String[] data = singleStudentData.split(",");
String fname = data[0].trim();
String lname = data[1].trim();
if(fname.equals("") || isInteger(fname))
throw new BadNameException("A BadNameException occurred for the
First Name");
if(lname.equals("") || isInteger(lname))
throw new BadNameException("A BadNameException occurred for the
Last Name");
String g1 = data[2].trim();
String g2 = data[3].trim();
String g3 = data[4].trim();
int grade1 = 0, grade2 = 0, grade3 = 0;
if(g1.equals("") || !isInteger(g1))
throw new BadGradeException("A BadGradeException occurred for Grade
1");
else
grade1 = Integer.parseInt(g1);
if(g2.equals("") || !isInteger(g2))
throw new BadGradeException("A BadGradeException occurred for Grade
2");
else
grade2 = Integer.parseInt(g2);
if(g3.equals("") || !isInteger(g3))
throw new BadGradeException("A BadGradeException occurred for Grade
3");
else
grade3 = Integer.parseInt(g3);
Student student = new Student(fname, lname);
if(this.students.isEmpty())
{
student.setGrade1(grade1);
student.setGrade2(grade2);
student.setGrade3(grade3);
this.students.add(student);
isAdded = true;
}
else
{
boolean found = false;
int index = 0;
for(int i = 0; i < this.students.size(); i++)
{
if(this.students.get(i).equals(student))
{
found = true;
index = i;
break;
}
}
if(found)
{
// student already present, update the grades
this.students.get(index).setGrade1(grade1);
this.students.get(index).setGrade2(grade2);
this.students.get(index).setGrade3(grade3);
isAdded = false;
}
else
{
// new student, add it
student.setGrade1(grade1);
student.setGrade2(grade2);
student.setGrade3(grade3);
this.students.add(student);
isAdded = true;
}
}
return isAdded;
}
public int addAllStudents(Scanner allStudents)
{
int count = 0, fnameExc = 0, lnameExc = 0, grade1Exc = 0, grade2Exc
= 0, grade3Exc = 0;
while(allStudents.hasNextLine())
{
try
{
if(addStudent(allStudents.nextLine().trim()))
{
count++;
}
}catch(BadNameException bne){
if(bne.getMessage().contains("First Name"))
fnameExc++;
else if(bne.getMessage().contains("Last Name"))
lnameExc++;
}catch(BadGradeException bge){
if(bge.getMessage().contains("Grade 1"))
grade1Exc++;
if(bge.getMessage().contains("Grade 2"))
grade2Exc++;
if(bge.getMessage().contains("Grade 3"))
grade3Exc++;
}
}
System.out.println("Exceptions Caught and Handled\n"
+ "=============================");
System.out.println("First name: " + fnameExc + "\n"
+ "Last name: " + lnameExc + "\n"
+ "Grade 1: " + grade1Exc + "\n"
+ "Grade 2: " + grade2Exc + "\n"
+ "Grade 3: " + grade3Exc);
return count;
}
public double computeClassAverage()
{
double sum = 0.0;
int count = 0;
for(Student st : this.students)
{
count++;
sum += st.computeAverage();
}
return(sum / (double)count);
}
static class CompareByGrade1 implements
Comparator<Student>
{
@Override
public int compare(Student o1, Student o2) {
if(o1.getGrade1() == o2.getGrade1())
return(o1.getFName().compareTo(o2.getFName()));
else
return (o2.getGrade1() - o1.getGrade1());
}
}
static class CompareByGrade2 implements
Comparator<Student>
{
@Override
public int compare(Student o1, Student o2) {
return (o2.getGrade2() - o1.getGrade2());
}
}
static class CompareByGrade3 implements
Comparator<Student>
{
@Override
public int compare(Student o1, Student o2) {
return (o2.getGrade3() - o1.getGrade3());
}
}
static class CompareByAverage implements
Comparator<Student>
{
@Override
public int compare(Student o1, Student o2) {
return (int)(o2.computeAverage() - o1.computeAverage());
}
}
public ArrayList<Student> rankByGrade1()
{
ArrayList<Student> temp = this.students;
Collections.sort(temp, new CompareByGrade1());
return temp;
}
public ArrayList<Student> rankByGrade2()
{
ArrayList<Student> temp = this.students;
Collections.sort(temp, new CompareByGrade2());
return temp;
}
public ArrayList<Student> rankByGrade3()
{
ArrayList<Student> temp = this.students;
Collections.sort(temp, new CompareByGrade3());
return temp;
}
public ArrayList<Student> rankByAverage()
{
ArrayList<Student> temp = this.students;
Collections.sort(temp, new CompareByAverage());
return temp;
}
private boolean isInteger(String s)
{
try
{
Integer.parseInt(s);
return true;
}catch(NumberFormatException nfe){
return false;
}
}
}
A4Driver.java (Driver class)
import java.util.Scanner;
public class A4Driver {
public static void main(String[] args) {
//test student class
System.out.println("test Student
class\n++++++++++++++++++++++++++++");
Student s = new Student("Bugs", "Bunny");
//same student
Student s3 = new Student("Bugs", "Bunny");
System.out.println("Are students s and s3 the same?: " +
s.equals(s3)); //true
//not the same student
Student s2 = new Student("Buga", "Bunny");
System.out.println("Are students s and s2 the same?: " +
s.equals(s2)); //false
try
{
s.equals(new Object());
}catch(Exception e){
System.out.println(e.getMessage());
}
//compute average 90
s.setGrade1(100);
s.setGrade2(90);
s.setGrade3(80);
System.out.println("Student's average for the 3 grades is " +
s.computeAverage());
System.out.println("\n\ntest StudentRoster
class\n+++++++++++++++++++++++++++++");
String text =""
+ "Bugs, Bunny, 70, 90, 80\n"
+ "Buga, Bunny, 100 , 80, 90\n"
+ "123, Bunny, 100 , 90, 80\n"
+ "Bugs, 1234 , 100 , 90, 80\n"
+ "BUGS, Bunny, xx , 90, 80\n"
+ "Bugs, BUNNY, 100 , yy, 80\n"
+ "bugs, bunny, 70 , 90, zz\n"
+ "Betty, Davis, !@ , 80, 67\n";
Scanner scan = new Scanner(text);
StudentRoster sr = new StudentRoster("Fall", 2019);
// Add only two students
//System.out.println("\nTest addAllStudents, number of new students
added: " + sr.addAllStudents(scan));
try
{
System.out.println("\nTest addAllStudents, number of new students
added: " + sr.addAllStudents(scan));
}catch(Exception e){
System.out.println(e.getMessage());
}
//average of the two students should be 85
System.out.println("\nTest computeClassAverage, class average is: "
+ sr.computeClassAverage());
//rank by grade1 Buga, Bugs
System.out.println("\nTest rankByGrade1: "+sr.rankByGrade1());
//rank by grade2 Bugs, Buga
System.out.println("\nTest rankByGrade2: "+sr.rankByGrade2());
//rank by grade3 Buga, Bugs
System.out.println("\nTest rankByGrade3: "+sr.rankByGrade3());
//rank by average Buga, Bugs
System.out.println("\nTest rankByAverage:
"+sr.rankByAverage());
}
}
**************************************************************** SCREENSHOT ***********************************************************