# Assignment2_31911077 **Repository Path**: hellolwp/assignment2_31911077 ## Basic Information - **Project Name**: Assignment2_31911077 - **Description**: 作业assignment2_31911077 - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2023-03-27 - **Last Updated**: 2023-03-27 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README Assignment 2 by *your name* (*your ID number*) --- > Assignment2_31911077. Hello, teacher. Please review my homework, thank you ! > > The following secondary title represents a package in a Java project. > > The following third title represents a Class in a Java project. ## Java Generics - ### NumberPassword ````java package javagenerics; public class NumberPassword { private int password; public void addPassword(int password) { this.password = password; } public int retrievePassword() { return password; } /* * In the Main class, we create an instance of the NumberPassword class using the new keyword, * set the password to 1234 using the addPassword() method, * retrieve the password using the retrievePassword() method, * and then display the password using the System.out.println() method. * */ public static void main(String[] args) { NumberPassword numberPassword = new NumberPassword(); numberPassword.addPassword(1234); int password = numberPassword.retrievePassword(); System.out.println("The password is: " + password); } } ```` - ### StringPassword ````java package javagenerics; public class StringPassword { private String password; public void addPassword(String password) { this.password = password; } public String retrievePassword() { return password; } /* *The principle is the same as NumberPassword, except changing the "int" into "String". * */ public static void main(String[] args) { StringPassword stringPassword = new StringPassword(); stringPassword.addPassword("abcd"); String password = stringPassword.retrievePassword(); System.out.println("The password is: " + password); } } ```` - ### Password ````java package javagenerics; public class Password { private T password; public void addPassword(T password) { this.password = password; } public T retrievePassword() { return password; } /* * In the Main class, we create two instances of the Password class. * The first instance numericPassword is of type Password and represents a password that contains only numbers. * We set the password to 11234567 using the addPassword() method, * retrieve the password using the retrievePassword() method, and then display the password using the System.out.println() method. * */ public static void main(String[] args) { Password numericPassword = new Password<>(); numericPassword.addPassword(11234567); int numericPasswordValue = numericPassword.retrievePassword(); System.out.println("Numeric password is: " + numericPasswordValue); Password alphanumericPassword = new Password<>(); alphanumericPassword.addPassword("secret007"); String alphanumericPasswordValue = alphanumericPassword.retrievePassword(); System.out.println("Alphanumeric password is: " + alphanumericPasswordValue); } } ```` ## JUnit Testing - ### Student ````java package junittesting; public class Student { private double midExam; private double finalExam; public Student(double midExam, double finalExam) { this.midExam = midExam; this.finalExam = finalExam; } /* * The getAverage() method should compute * and return the average of the midExam and finalExam attributes (e.g. 80 and 70 has average of 75). * */ public double getAverage() { return (midExam + finalExam) / 2; } /* * The passed() method should return true if the average is at least 60, otherwise, * it should return false. * */ public boolean passed() { return getAverage() >= 60; } } ```` - ### TestStudent ````java package junittesting; import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; public class TestStudent { @Test @DisplayName("test getAverage() using 90 and 92") public void testGetAverage() { Student s1 = new Student(90, 92); double average = s1.getAverage(); System.out.println("Average:" + average); } /* The expected average value in the testAssertEqualsAverage() method has been changed to 91 to reflect the correct expected value. Finally, all test methods are now properly annotated with @Test and should be executed by a test runner, so all three tests should be executed. */ @Test public void assertEqualsAverage() { Student s1 = new Student(90, 92); double averageActual = s1.getAverage(); double averageExpected = 91; Assertions.assertEquals(averageExpected, averageActual); } @Test public void AssertPassedTrue(){ Student s1 = new Student(90, 92); boolean passed = s1.passed(); Assertions.assertTrue(passed); } /* * added two new methods: start() and end(). * These methods are annotated with @BeforeAll and @AfterAll, * respectively, which means that they will be executed before and after all test methods in the class. * */ @BeforeAll public static void start() { System.out.println("Student Class testing begins."); } @AfterAll public static void end() { System.out.println("Student Class testing ended."); } /* * The method testGetAverage2() should test getAverage() method using grades * that will result to an average lower than 60 (e.g.30 and 60). * */ @Test @DisplayName("test getAverage() using 30 and 60") public void testGetAverage2() { Student s1 = new Student(30, 60); double average = s1.getAverage(); System.out.println("Average:" + average); } @Test @DisplayName("test that averageActual is not equal to averageExpected") public void assertNotEqualsAverage() { Student s1 = new Student(90, 92); double averageActual = s1.getAverage(); double averageExpected = 80; Assertions.assertNotEquals(averageExpected, averageActual); } /* * The method assertPassedFalse() should use grades that will result to an average lower than 60 (e.g.30 and 60). * */ @Test @DisplayName("test that student has not passed") public void assertPassedFalse() { Student s1 = new Student(30, 60); boolean passed = s1.passed(); Assertions.assertFalse(passed); } } ````