From 51d3054a53c0497fba0a8826413987087b89144a Mon Sep 17 00:00:00 2001 From: coderunnerone1 Date: Fri, 12 Jan 2024 11:46:41 -0800 Subject: [PATCH 1/2] Update StudentStatsArray.java --- .../StudentStatsArray.java | 130 ++++++++---------- 1 file changed, 55 insertions(+), 75 deletions(-) diff --git a/Unit-6/Assignment6-ArrayStatistics/StudentStatsArray.java b/Unit-6/Assignment6-ArrayStatistics/StudentStatsArray.java index 295b371..a2b3bf8 100644 --- a/Unit-6/Assignment6-ArrayStatistics/StudentStatsArray.java +++ b/Unit-6/Assignment6-ArrayStatistics/StudentStatsArray.java @@ -1,84 +1,70 @@ public class StudentStatsArray { - // Initialize Array + private final Student[] students; - - // Add private final variable to hold Students array - public StudentStatsArray(Student[] students) - { + + public StudentStatsArray(Student[] students) + { this.students = students; - } + } - // Returns the average gpa of the students - public double averageGpa() - { - // Temporary Storage + // Returns the average gpa of the students + public double averageGpa() + { double total = 0; - // Gather GPAs for (Student s: students) { total += s.getGpa(); } - // End - return total / students.length; - } + return total / students.length; // t + } - // Returns the gpa range of the students - public double getGpaRange() - { - // Temporary Storage + // Returns the gpa range of the students + public double getGpaRange() + { double maxGpa = students[0].getGpa(), minGpa = students[0].getGpa(); - // Gather GPAs for (Student s: students) { if (s.getGpa() > maxGpa) { - maxGpa = s.getGpa(); + maxGpa = s.getGpa(); } else if (s.getGpa() < minGpa) { minGpa = s.getGpa(); } } - // End return maxGpa - minGpa; - } + } - // Returns the name of the student that has the longest length - public String getLongestName() - { - // Temporary Storage + // Returns the name of the student that has the longest length + public String getLongestName() + { String longestName = ""; - // Check for longestName for (Student s: students) { if (s.getName().length() > longestName.length()) longestName = s.getName(); } - // End return longestName; - } + } - // Returns a count of the number freshman students - public int getNumFreshman() - { - // Temporary Storage + // Returns a count of the number freshman students + public int getNumFreshman() + { int numFreshman = 0; - // Check for Freshman for (Student s: students) { if (s.getYear() == 1) numFreshman++; } - // End return numFreshman; - } + } - // Returns the index of the first student with a name equal to name. - // Returns -1 if not found - public int search(String name) - { - // Search for Student + // Returns the index of the first student with a name equal to name. + // Returns -1 if not found + public int search(String name) + { for (int i = 0; i < students.length; i++) { if (students[i].getName().contains(name)) @@ -86,33 +72,27 @@ public int search(String name) return i; } } - // Not Found 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) - { - // Search for First GPA - for (int i = 0; i < students.length; i++) + // 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) // me when + { +for (int i = 0; i < students.length; i++) { if (students[i].getGpa() >= gpa) return i; } - // Not Found 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() - { - // Temporary Storage + // 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() + { int sort = 0; double prevGpa = 0; - - // Scan for Ascending Order prevGpa = students[0].getGpa(); for (Student s: students) { @@ -128,11 +108,10 @@ public int sortStatus() } if (sort == 0) break; + } - // Check Order if (sort == 1) return sort; - // Scan for Descending Order prevGpa = students[0].getGpa(); for (Student s: students) { @@ -149,21 +128,22 @@ public int sortStatus() if (sort == 0) break; } - // End return sort; - } + } - // Returns the array of students in JSON like format - public String toString() - { - // Temporary Storage - String output = ""; - // Gather Student Data - for (Student s: students) - { - output += ("{\n\tname: " + s.getName() + ",\n\tgpa: " + s.getGpa() + ",\n\tyear: " + s.getYear() + "\n},"); + + // Returns the array of students in JSON like format + public String toString() { + String output = "[\n"; + for (int i = 0; i < students.length; i++) { + Student s = students[i]; + output += "{\n"; + output += "\tname: " + s.getName() + ",\n"; + output += "\tgpa: " + s.getGpa() + ",\n"; + output += "\tyear: " + s.getYear() + "\n"; + output += "},\n" + (i < students.length - 1 ? "" : ""); + } + output += "]"; + return output; } - // End - return "[\n" + output + "\n]"; - } -} \ No newline at end of file +} From 39aa94e03dad08eae8de39244715165fd5c81592 Mon Sep 17 00:00:00 2001 From: Ricky <92121005+ricky8k@users.noreply.github.com> Date: Fri, 12 Jan 2024 17:41:49 -0500 Subject: [PATCH 2/2] chore: clean up `StudentStatsArray.java` - Removed extraneous if statement in `toString()` method - Amended formatting errors --- .../StudentStatsArray.java | 101 +++++++++--------- 1 file changed, 50 insertions(+), 51 deletions(-) diff --git a/Unit-6/Assignment6-ArrayStatistics/StudentStatsArray.java b/Unit-6/Assignment6-ArrayStatistics/StudentStatsArray.java index a2b3bf8..b629b69 100644 --- a/Unit-6/Assignment6-ArrayStatistics/StudentStatsArray.java +++ b/Unit-6/Assignment6-ArrayStatistics/StudentStatsArray.java @@ -1,27 +1,27 @@ public class StudentStatsArray { - private final Student[] students; - public StudentStatsArray(Student[] students) - { + // Constructor for students array + public StudentStatsArray(Student[] students) + { this.students = students; - } + } - // Returns the average gpa of the students - public double averageGpa() - { + // Returns the average gpa of the students + public double averageGpa() + { double total = 0; for (Student s: students) { total += s.getGpa(); } return total / students.length; // t - } + } - // Returns the gpa range of the students - public double getGpaRange() - { + // Returns the gpa range of the students + public double getGpaRange() + { double maxGpa = students[0].getGpa(), minGpa = students[0].getGpa(); for (Student s: students) { @@ -35,11 +35,11 @@ else if (s.getGpa() < minGpa) } } return maxGpa - minGpa; - } + } - // Returns the name of the student that has the longest length - public String getLongestName() - { + // Returns the name of the student that has the longest length + public String getLongestName() + { String longestName = ""; for (Student s: students) { @@ -47,11 +47,11 @@ public String getLongestName() longestName = s.getName(); } return longestName; - } + } - // Returns a count of the number freshman students - public int getNumFreshman() - { + // Returns a count of the number freshman students + public int getNumFreshman() + { int numFreshman = 0; for (Student s: students) { @@ -59,12 +59,12 @@ public int getNumFreshman() numFreshman++; } return numFreshman; - } + } - // Returns the index of the first student with a name equal to name. - // Returns -1 if not found - public int search(String name) - { + // Returns the index of the first student with a name equal to name. + // Returns -1 if not found + public int search(String name) + { for (int i = 0; i < students.length; i++) { if (students[i].getName().contains(name)) @@ -73,24 +73,24 @@ 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) // me when - { -for (int i = 0; i < students.length; i++) + // 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) // me when + { + for (int i = 0; i < students.length; i++) { if (students[i].getGpa() >= gpa) return i; } 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() - { + // 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() + { int sort = 0; double prevGpa = 0; prevGpa = students[0].getGpa(); @@ -108,7 +108,6 @@ public int sortStatus() } if (sort == 0) break; - } if (sort == 1) return sort; @@ -129,21 +128,21 @@ public int sortStatus() break; } return sort; - } + } - - // Returns the array of students in JSON like format - public String toString() { - String output = "[\n"; - for (int i = 0; i < students.length; i++) { - Student s = students[i]; - output += "{\n"; - output += "\tname: " + s.getName() + ",\n"; - output += "\tgpa: " + s.getGpa() + ",\n"; - output += "\tyear: " + s.getYear() + "\n"; - output += "},\n" + (i < students.length - 1 ? "" : ""); - } - output += "]"; - return output; + // Returns the array of students in JSON format + public String toString() + { + String output = "[\n"; + for (Student s : students) + { + output += "{\n"; + output += "\tname: " + s.getName() + ",\n"; + output += "\tgpa: " + s.getGpa() + ",\n"; + output += "\tyear: " + s.getYear() + "\n"; + output += "},\n"; } + output += "]"; + return output; + } }