Question

I ONLY need question 2 part C AND D answered...it is in bold. Please do not...

I ONLY need question 2 part C AND D answered...it is in bold. Please do not use hash sets or tables. 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 to help.

1. The Student class should be in the assignment package.

a. There should be class variable for first and last names. There should be three (3) class variables for storing grades. All class variables of the Student class must be private.

b. public Student(String fname, String lname) is the single constructor of the class. All arguments of the constructor should be stored as class variables. There should only be getter methods for first and last name variables.

c. public double computeAverage() returns the average of the three grades.

d. public int getGrade1() & public void setGrade1(int grade1) sets and gets grade1.

e. public int getGrade2() & public void setGrade2(int grade2) sets and gets grade2.

f. public int getGrade3() & public void setGrade3(int grade3) sets and gets grade3.

g. public String getFname() & public String getLname() gets first and last name of the student.

h. public boolean equals(Object otherStudent) if first and last names are the same then returns true, otherwise false. This method also throws NotAStudentException if object is not of the class Student.

i. public String toString() return first name, last name followed by grades 1 to 3.

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.

e. public double computeClassAverage() returns the class’ average score using all three grades for all students in the roster.

f. public ArrayList rankByGrade1() returns an ArrayList of the roster sorted by grade 1, from highest to lowest.

g. public ArrayList rankByGrade2() returns an ArrayList of the roster sorted by grade 2, from highest to lowest.

h. public ArrayList rankByGrade3() returns an ArrayList of the roster sorted by grade 3, from highest to lowest.

i. public ArrayList rankByAverage() returns an ArrayList of the roster sorted by average of the three grades, from highest to lowest.

Hint: Consider Comparators for 2f-2i.


3. Create these Exception classes and use as described. The exception

classes should be in the assignment.exceptions package.
a. NotAStudentException, used by the Student class. Generated if object is not of the class Student.

b. BadGradeException, used by the StudentRoster class. 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.

c. BadNameException, used by the StudentRoster class. Generated if first or last names are absent, or an integer is present where first and last names should be. The message of this exception should state if it was caused by issues with first or last name.

4. The A4Driver class is:

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]

0 0
Add a comment Improve this question Transcribed image text
Answer #1

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().equals("Grade1"))
grade1Exc++;
else if(bge.getMessage().equals("Grade2"))
grade2Exc++;
else if(bge.getMessage().equals("Grade3"))
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++;
System.out.println("Av: " + st.computeAverage());
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;
}
}
}

NotAStudentException.java

public class NotAStudentException extends IllegalArgumentException{
  
public NotAStudentException(String msg)
{
super(msg);
}
}

BadNameException.java

public class BadNameException extends IllegalArgumentException{
  
public BadNameException(String msg)
{
super(msg);
}
}

BadGradeException.java

public class BadGradeException extends IllegalArgumentException{
  
public BadGradeException(String msg)
{
super(msg);
}
}

A4Driver.java (Driver/Main 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\ntes t 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, 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));
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 ********************************************************

Add a comment
Know the answer?
Add Answer to:
I ONLY need question 2 part C AND D answered...it is in bold. Please do not...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Hello, Can you please error check my javascript? It should do the following: Write a program...

    Hello, Can you please error check my javascript? It should do the following: Write a program that uses a class for storing student data. Build the class using the given information. The data should include the student’s ID number; grades on exams 1, 2, and 3; and average grade. Use appropriate assessor and mutator methods for the grades (new grades should be passed as parameters). Use a mutator method for changing the student ID. Use another method to calculate the...

  • **Only need the bold answered Implement the Athlete, Swimmer, Runner, and AthleteRoster classes below. Each class...

    **Only need the bold answered Implement the Athlete, Swimmer, Runner, and AthleteRoster classes below. Each class must be in separate file. Draw an UML diagram with the inheritance relationship of the classes. 1. The Athlete class a. All class variables of the Athlete class must be private. b.is a class with a single constructor: Athlete(String lName, String fName, int birthYear, int birthMonth, int birthDay, char gender). All arguments of the constructor should be stored as class variables. There should only...

  • Introduction In this lab, you will be working with three classes: Person, Student, and Roster. Person...

    Introduction In this lab, you will be working with three classes: Person, Student, and Roster. Person and Student represent individuals with a first and last name as String class variables, and Student has an additional int class variable that represents a ID number. The Roster class represents a group of people that are objects of either the Person or Student class. It contains an ArrayList. The Person class has been completed for you with class variables, a constructor, and a...

  • I need to change the following code so that it results in the sample output below...

    I need to change the following code so that it results in the sample output below but also imports and utilizes the code from the GradeCalculator, MaxMin, and Student classes in the com.csc123 package. If you need to change anything in the any of the classes that's fine but there needs to be all 4 classes. I've included the sample input and what I've done so far: package lab03; import java.util.ArrayList; import java.util.Scanner; import com.csc241.*; public class Lab03 { public...

  • Given a class called Student and a class called Course that contains an ArrayList of Student....

    Given a class called Student and a class called Course that contains an ArrayList of Student. Write a method called getDeansList() as described below. Refer to Student.java below to learn what methods are available. I will paste both of the simple .JAVA codes below COURSE.JAVA import java.util.*; import java.io.*; /****************************************************** * A list of students in a course *****************************************************/ public class Course{     /** collection of Students */     private ArrayList<Student> roster; /***************************************************** Constructor for objects of class Course *****************************************************/...

  • (I don't need code for this question I only need explanation how to solve the question.)...

    (I don't need code for this question I only need explanation how to solve the question.) The following code causes an exception error: import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; public class Ex02 {     public static void main(String[] args) throws IOException {         BufferedReader userInput = new BufferedReader             (new InputStreamReader(System.in));         ArrayList<String> myArr = new ArrayList<String>();         myArr.add("Zero");         myArr.add("One");         myArr.add("Two");         myArr.add("Three");                 System.out.println(myArr.get(7));                     } } Starting with this provided code,...

  • JAVA 3files seprate 1.Classroom.java 2.ClassroomTester.java (provided) 3.Student.java(provided) Use the following files: ClassroomTester.java import java.util.ArrayList; /** *...

    JAVA 3files seprate 1.Classroom.java 2.ClassroomTester.java (provided) 3.Student.java(provided) Use the following files: ClassroomTester.java import java.util.ArrayList; /** * Write a description of class UniversityTester here. * * @author (your name) * @version (a version number or a date) */ public class ClassroomTester { public static void main(String[] args) { ArrayList<Double> grades1 = new ArrayList<>(); grades1.add(82.0); grades1.add(91.5); grades1.add(85.0); Student student1 = new Student("Srivani", grades1); ArrayList<Double> grades2 = new ArrayList<>(); grades2.add(95.0); grades2.add(87.0); grades2.add(99.0); grades2.add(100.0); Student student2 = new Student("Carlos", grades2); ArrayList<Double> grades3 = new...

  • I have a little problem about my code. when i'm working on "toString method" in "Course"...

    I have a little problem about my code. when i'm working on "toString method" in "Course" class, it always say there is a mistake, but i can not fix it: Student class: public class Student {    private String firstName;    private String lastName;    private String id;    private boolean tuitionPaid;       public Student(String firstName, String lastName, String id, boolean tuitionPaid)    {        this.firstName=firstName;        this.lastName=lastName;        this.id=id;        this.tuitionPaid=tuitionPaid;    }   ...

  • I have this program that works but not for the correct input file. I need the...

    I have this program that works but not for the correct input file. I need the program to detect the commas Input looks like: first_name,last_name,grade1,grade2,grade3,grade4,grade5 Dylan,Kelly,97,99,95,88,94 Tom,Brady,100,90,54,91,77 Adam,Sandler,90,87,78,66,55 Michael,Jordan,80,95,100,89,79 Elon,Musk,80,58,76,100,95 output needs to look like: Tom Brady -------------------------- Assignment 1: A Assignment 2: A Assignment 3: E Assignment 4: A Assignment 5: C Final Grade: 82.4 = B The current program: import java.io.File; import java.io.FileNotFoundException; import java.io.FileWriter; import java.io.IOException; import java.util.Scanner; public class main {    public static void main(String[]...

  • Please help. I need a very simple code. For a CS 1 class. Write a program...

    Please help. I need a very simple code. For a CS 1 class. Write a program in Java and run it in BlueJ according to the following specifications: The program reads a text file with student records (first name, last name and grade). Then it prompts the user to enter a command, executes the command and loops. The commands are the following: "printall" - prints all student records (first name, last name, grade). "firstname name" - prints all students with...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT