Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Do code for Lab2.java and Student.java and run in Lab2Tester.java Java /* * Lab2Tester.java * * A tester for the methods in Lab2.java * */

Do code for Lab2.java and Student.java and run in Lab2Tester.java Java /* * Lab2Tester.java * * A tester for the methods in Lab2.java * */ import java.util.Arrays; public class Lab2Tester { private static int testPassCount = 0; private static int testCount = 0; // for approximate comparison of floating point numbers private static final double THRESHOLD = 0.01; public static void main(String[] args) { testGetHigherGradeStudent(); testIsGradeAbove(); testGetClasslist(); testCountAbove(); testGetClassAverage(); testRegisterStudent(); System.out.println("Passed " + testPassCount + " / " + testCount + " tests"); } public static void testGetHigherGradeStudent() { // TODO: once you have implemented getHigherGradeStudent in Lab2.java // uncomment the following tests - make sure you understand what they are testing /* Student s0 = new Student("abc", 50); Student s1a = new Student("def", 56); Student s1b = new Student("xyz", 56); Student s2 = new Student("xyz", 99); Student result; result = Lab2.getHigherGradeStudent(s0,s1a); //System.out.println("should be " + s1a + " is " + result); displayResults(result.equals(s1a), "testGetHigherGradeStudent"); result = Lab2.getHigherGradeStudent(s1a,s0); //System.out.println("should be " + s1a + " is " + result); displayResults(result.equals(s1a), "testGetHigherGradeStudent"); result = Lab2.getHigherGradeStudent(s1b,s1a); //System.out.println("should be " + s1b + " is " + result); displayResults(result.equals(s1b) && result == s1b, "testGetHigherGradeStudent"); result = Lab2.getHigherGradeStudent(s1b,s2); //System.out.println("should be " + s2 + " is " + result); displayResults(result.equals(s2), "testGetHigherGradeStudent"); */ } public static void testIsGradeAbove() { // TODO: write tests for Lab2.isGradeAbove } public static void testGetClasslist() { // TODO: write tests for Lab2.getClasslist // NOTE: the Arrays library has been imported for you. // you can use the Arrays.equals method to compare // 2 arrays of String objects as String has a equals method // The API for Arrays.equals: // equals(Object[] a, Object[] a2) // Returns true if the two specified arrays are equal to one another. // TODO: once you have implemented getClasslist in Lab2.java // uncomment the following test. We have gotten you started with // some initial test data and one test, but you should consider // other cases (empty array, longer array) /* Student s0 = new Student("abc", 50); Student[] students1 = {s0}; String[] expected1 = {"abc"}; String[] result; result = Lab2.getClasslist(students1); displayResults(Arrays.equals(result, expected1), "testGetClasslist - 1 elements"); */ } public static void testCountAbove() { // TODO: write tests for Lab2.countAbove } public static void testGetClassAverage() { Student s0 = new Student("abc", 50); Student s1 = new Student(); // considered invalid for average average calculation Student s2 = new Student("xyz", 99); Student s3 = new Student("def", 88); Student[] students0 = {}; Student[] students1 = {s0}; Student[] students2 = {s0, s1, s2}; Student[] students3 = {s0, s2, s3}; double result = 0.0; double expected = 0.0; // Some of you noticed in Lab1 that floating point arithmetic sometimes // produces results with many decimals places. We use the threshold // (defined as a global variable at the top) to specify the margin // of error we are okay with, and just make sure the expected and // returned results are within the threshold of acceptable error. /* result = Lab2.getClassAverage(students0); expected = 0; displayResults(Math.abs(result-expected) 

/* * Lab2.java * * A class of static methods that operate on Students * */ public class Lab2 { /* * Purpose: determines which student has the higher grade * Parameters: Student - s1, Student - s2 * Returns: Student - the Student with the higher grade, * s1 if they have the same grade * Precondition: s1 and s2 are not null */ public static Student getHigherGradeStudent(Student s1, Student s2) { // TODO: implement getHigherGradeStudent return null; // so it compiles } /* * Purpose: determines whether the grade of Student s * is above the threshold * Parameters: Student - s, int - threshold * Returns: boolean - true if grade is above threshold, false otherwise * Preconditions: s is not null */ // ToDo: implement isGradeAbove /* * Purpose: creates an array sIDs of all Students in students * Parameters: Student[] - students * Returns: String[] - array of sIDs * Preconditions: students is not null and contains no null elements */ // ToDo: implement getClasslist /* * Purpose: counts the number of Students in students * with grade above threshold * Parameters: Student[] - students, int threshold * Preconditions: students is not null and contains no null elements * Returns: int - the count */ // ToDo: implement countAbove // HINT: you should be using the isGradeAbove method! /* * Purpose: calculates the average grade of Students in students, * does NOT include students with -1 grade in calculation * average is 0.0 if students is empty or all students have -1 grade * Parameters: Student[] - students * Returns: double - the average grade * Preconditions: students is not null and contains no null elements */ // ToDo: implement getClassAverage // HINT: you can use the isGradeAbove method again /* * Purpose: creates a new array 1 longer than students * and adds all students and s to the new array * Parameters: Student[] - students, Student s * Returns: Student[] - the new array * Preconditions: students is not null and contains no null elements * Student s is not already contained within students */ // ToDo: implement registerStudent }

/* * Student.java * * A Student class * */ public class Student { private String sID; // unique student ID private int grade; // % grade (-1 if no initial grade) public Student() { sID = ""; grade = -1; } public Student(String sID, int grade) { this.sID = sID; this.grade = grade; } /* * Purpose: returns this Student's sID * Parameters: none * Returns: String - the sID */ public String getSID() { return this.sID; } /* * Purpose: set's this Student's sID to sID parameter value * Parameters: String - sID * Returns: nothing */ public void setSID(String sID) { this.sID = sID; } /* * Purpose: returns this Student's grade * Parameters: none * Returns: int - the sID */ public int getGrade() { return this.grade; } /* * Purpose: set's this Student's grade to grade parameter value * Parameters: int - grade * Returns: nothing */ public void setGrade(int grade) { this.grade = grade; } /* * Purpose: returns a String representation of this Student * in the form "sID:grade" * Parameters: none * Returns: String - the representation */ public String toString() { return sID + ":" + grade; } /* * Purpose: returns true if this Student's sID * equals other Student's sID * Parameters: none * Returns: boolean - true if equal, false otherwise */ public boolean equals(Student other) { return (this.sID.equals(other.sID)); } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions

Question

2. Are you varying your pitch (to avoid being monotonous)?

Answered: 1 week ago

Question

3. Are you varying your speaking rate and volume?

Answered: 1 week ago