Kotlinlearncs.online LogoJava
Return to List

Test Writing: University Class Create

Created By: Ellen Spertus
/ Version: 2024.1.0

Create a class University with final private instance variables for the name (String) and founding year (int). Create public getters getName() and getFoundingYear() and override the equals() and toString() methods.

Two instances of `University` should be equal only if they have the same name and founding year.

The printed representation of a university should be `"Northeastern University (est. 1898)"` if its name is `"Northeastern University"` and its founding year is 1898.

This shows how the class might be used: ```java University nu = new University("Northeastern University", 1898); System.out.println(nu.getName()); // Northeastern University System.out.println(nu.getFoundingYear()); // 1898 University uiuc = new University("University of Illinois Urbana-Champaign", 1867); System.out.println(nu.equals(uiuc)); // false System.out.println(nu.equals(nu)); // true System.out.println(nu); // Northeastern University (est. 1898) System.out.println(uiuc); // University of Illinois Urbana-Champaign (est. 1867) ```

Test Design Challenge

You're challenge is to write tests for this problem described above.

  • Provide a public class named TestUniversity with a single non-private class method named test that accepts no arguments and does not return a value.
  • If the implementation of the class described above is incorrect, your test method should throw an exception.
  • If it is correct, do not throw an exception.
  • You may want to use Java's assert method