Unit Testing Run the Unit Test program, GradeCalculatorTest, as shown above. Add the required tests to GradeCalculatorTest.java,
Question:
Unit Testing
- Run the Unit Test program, GradeCalculatorTest, as shown above.
- Add the required tests to GradeCalculatorTest.java, recompile, and rerun it.
- Again, if you are doing a good job of.testing, some of the tests should fail.
GradeCalculator Corrections
Correct the errors in the GradeCalculator program and repeat the System and Unit Testing. This an iterative process in which you may make corrections, test, make more corrections, test again, etc. Be sure to update the Actual Results for each system test as necessary. When you are all done, the Actual Results should match the Expected Results for each test. Also, all of your unit tests should pass.
import java.util.*;
/** * Calulates letter grade based on average * @author */ public class GradeCalculator {
/** Minimum average */ public static final int MIN_AVERAGE = 0; /** Maximum average */ public static final int MAX_AVERAGE = 100; /** Lowest average for A */ public static final int A_CUTOFF = 90; /** Lowest average for B */ public static final int B_CUTOFF = 80; /** Lowest average for C */ public static final int C_CUTOFF = 70; /** Lowest average for C */ public static final int D_CUTOFF = 60; /** * Prompts user for average and outputs letter grade * @param args command line arguments (not used) */ public static void main(String[] args) { Scanner console = new Scanner(System.in); System.out.print("Enter average: "); int average = console.nextInt(); if (isValidAverage(average)) { System.out.println("Grade: " + calculateGrade(average)); } else { System.out.println("Invalid average"); } } /** * Determine if average is within grade range * @param average average * @return true if average is within range, false otherwise */ public static boolean isValidAverage(int average) { if (MIN_AVERAGE <= average && average <= MAX_AVERAGE){ return true; } else { return false; } } /** * Calculates letter grade based on average * @param average average * @return letter grade corresponding to average * @throws IllegalArgumentException if average is invalid */ public static String calculateGrade(int average) { if (!isValidAverage(average)) { throw new IllegalArgumentException("Invalid average"); } if (average >= A_CUTOFF) { return "A"; } else if (average > B_CUTOFF) { return "B"; } else if (average >= D_CUTOFF) { return "C"; } else if (average >= D_CUTOFF) { return "D"; } else { return "F"; } } }
import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.Test;
/** * Program to test GradeCalculator methods * @author */ public class GradeCalculatorTest { @Test public void testIsValidAverage1() { assertTrue(GradeCalculator.isValidAverage(78), "GradeCalculator.isValidAverage(78)"); }
@Test public void testIsValidAverage2() { assertFalse(GradeCalculator.isValidAverage(104), "GradeCalculator.isValidAverage(104)"); }
// Add 3 more JUnit test cases for the isValidAverage method
@Test public void testCalculateGrade1() { // Example valid test case for calculateGrade method assertEquals("A", GradeCalculator.calculateGrade(95), "GradeCalculator(95)"); }
@Test public void testCalculateGrade2() { assertEquals("D", GradeCalculator.calculateGrade(65), "GradeCalculator(65)"); }
// Add 7 more *valid* JUnit test cases here for calculateGrade method @Test public void testCalculateGradeInvalid() { // Invalid test cases are provided for you below - You do NOT // need to add additional invalid tests. Just make sure these // pass! Exception exception = assertThrows( IllegalArgumentException.class, () -> GradeCalculator.calculateGrade(103), "calculateGrade(103)"); assertEquals("Invalid average", exception.getMessage(), "Testing calculateGrade(103) - exception message"); exception = assertThrows( IllegalArgumentException.class, () -> GradeCalculator.calculateGrade(-1), "calculateGrade(-1)"); assertEquals("Invalid average", exception.getMessage(), "Testing calculateGrade(-1) - exception message"); } }
Elementary Statistics In Social Research Essentials
ISBN: 9780205638000
3rd Edition
Authors: Jack A. Levin, James Alan Fox