Question: For this assignment, you will complete a class which will be used to find statistics on an array of Student objects. The class, StudentStatsArray ,

For this assignment, you will complete a class which will be used to find statistics on an array of Student objects.

The class, StudentStatsArray, needs to store an array of Student objects as a member variable. This should be declared as private and final, so none of your methods will set this variable to point at a different array. The single constructor for this class has a parameter of a Student array, and should set the member variable to point to this parameter array.

The member variables and methods of the Student class are as follows:

Variables

String name - the name of the student

double gpa - the student's GPA (0-4.0 inclusive)

int year - the student's year (1-4 inclusive)

Methods

String getName() - returns the name of this student

double getGpa() - returns the GPA of this student

int getYear() - returns this students year

String toString () - return a String representing this student in a JSON like format, which is as follows:

{ name: , gpa: , year:  }

After inspecting the code for the Student class, you will need to complete the following methods in the StudentStatsArray class (you may assume that the member variable array is not empty when writing these methods):

Variables

Student[] students - the array of student objects

Methods

double averageGpa() - returns the average GPA of the student's in the students array

double getGpaRange() - returns the range of the GPAs of the student's in the students array

String getLongestName() - returns the name of the student with the longest name in the students array

int getNumFreshman() - returns the number of freshman (year = 1) in the students array

int search(String name) - returns the index of the first student with a name equal to name. If there are no students with a name equal to name, the method should return -1.

int search(int gpa) - return the index of the first student with a gpa greater than or equal to gpa. If there are no students with a gpa greater than or equal to gpa, the method should return -1.

int sortStatus() - returns 1 if the students are sorted in ascending order by their gpa and -1 if in descending order. Make sure to take into account that the array can still be sorted in ascending or descending order if two or more students in consecutive indices have the same GPA. If all students have the same GPA, then return 1. Return 0 in all other cases.

String toString() - returns a string representing the students array in a format similar to JSON. The format is as follows:

[ { name: , gpa: , year:  },  { name: , gpa: , year:  }, ] 

You should use the runner_StudentStatsArray class to test your classes. Do not add a main method to the Student.java or StudentStatsArray.java files, as it will cause your submission to be scored incorrectly.

Remember not to have spaces between your brackets, as a space will also cause your submission to be scored incorrectly.

Milestones

As you work on this assignment, you can use the milestones below to inform your development process:

Milestone 1: Add the Student array member variable to the StudentStatsArray class and complete the constructor. Write the toString method for the class and test that it formats the output string in the exact format described.

Milestone 2: Add both of the overloaded search methods. The first, to search a Student by their name and the second to search for a student by their gpa, returning the index of the Student or -1 if no student is found.

Milestone 3: Add both the getNumFreshman and getLongestName methods to the StudentStatsArray class. The first method, getNumFreshman, should return the number of students with year = 1. The second method, getLongestName, should use a loop to keep track of the student with the longest name, and return the name of the longest student.

Milestone 4: Write a loop in the average method which sums the values, then use this to return the average. Write a loop to iterate through the array and find the minimum/maximum GPA of the students in the range method, then use these values to calculate and return the range of GPAs.

Milestone 5: Implement the sortStatus function checks if the students are sorted in ascending or descending order by their GPAs.

Student.java:

public class Student { private String name; private double gpa; private int year;

public Student(String name, double gpa, int year) { this.name = name; this.gpa = gpa; this.year = year; }

public String getName() { return this.name; } public double getGpa() { return this.gpa; } public int getYear() { return this.year; }

public String toString() { return "{ \tname: " + this.name + ", \tgpa: " + this.gpa + ", \tyear: " + this.year + " }"; } }

StudentStatsArray.java:

public class StudentStatsArray {

// Add private final variable to hold Students array

public StudentStatsArray(Student[] students) { // Constructor code }

// Returns the average gpa of the students public double averageGpa() { return -1; }

// Returns the gpa range of the students public double getGpaRange() { return -1; }

// Returns the name of the student that has the longest length public String getLongestName() { return ""; }

// Returns a count of the number freshman students public int getNumFreshman() { return -1; }

// Returns the index of the first student with a name equal to name. // Returns -1 if not found public int search(String name) { return -1; }

// Returns the index of the first student with a gpa greater than or equal to the gpa // Returns -1 if not found public int search(double gpa) { return -1; }

// Returns 1 if the students are sorted in ascending order by their gpa; -1 if they // are sorted in descending order; 0 otherwise. public int sortStatus() { return -1; }

// Returns the array of students in JSON like format public String toString() { return ""; }

}

runner_StudentStatsArray:

import java.util.Scanner;

public class runner_StudentStatsArray {

public static void main(String[] args) {

Scanner scan = new Scanner(System.in);

String choice = ""; while(!choice.equals("n")) { System.out.print("Would you like to test StudentStats? (y/n): "); choice = scan.nextLine().trim().toLowerCase();

if (choice.equals("y")) { testStudentStats(scan); } else if (!choice.equals("n")) { System.out.println("Invalid choice."); } else { System.out.println("Bye!"); } } }

public static void testStudentStats(Scanner scan) { System.out.print("Enter the length of the Student array: "); int len = scan.nextInt(); scan.nextLine();

Student[] arr = new Student[len]; for (int i = 0; i < len; i++) { System.out.println(" Please enter the info for student " + (i + 1) + ""); arr[i] = studentBuilder(scan); }

System.out.print("Enter a students name to search for: "); String name = scan.nextLine().trim(); System.out.print("Enter a minimum gpa to search for: "); double gpa = scan.nextDouble(); scan.nextLine();

StudentStatsArray statsArray = new StudentStatsArray(arr); System.out.println(statsArray); System.out.println(" Method return values: "); System.out.println("averageGpa(): " + statsArray.averageGpa()); System.out.println("getGpaRange(): " + statsArray.getGpaRange()); System.out.println("getLongestName(): " + statsArray.getLongestName()); System.out.println("getNumFreshman(): " + statsArray.getNumFreshman()); System.out.println("search(" + name + "): " + statsArray.search(name)); System.out.println("search(" + gpa + "): " + statsArray.search(gpa)); int sortStatus = statsArray.sortStatus(); System.out.println("sortStatus(): " + (sortStatus == 1 ? "Ascending" : (sortStatus == -1 ? "Descending" : "Not sorted")));

System.out.println(); }

public static Student studentBuilder(Scanner scan) { System.out.print("Enter the students name: "); String name = scan.nextLine().trim(); System.out.print("Enter the students gpa: (0.0-4.0) "); double gpa = scan.nextDouble(); scan.nextLine(); System.out.print("Enter the students year (1-4): "); int year = scan.nextInt(); scan.nextLine(); Student student = new Student(name, gpa, year);

return student; } }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!