1. Setup

1.1. Java Tools

Download and install the Java Development Kit (JDK) version 8. Make sure that you select the appropriate link for your Operating System (Windows, OSX, Linux) and its version.

1.2. Eclipse

The instructor will be using Eclipse in class. You do not have to install Eclipse, you can select to use a different IDE. We will try and help you regardless of your IDE but be prepared to do some work on your own if you select an IDE other than Eclipse.

1.3. Test your Setup

  1. Open your newly installed IDE.

  2. Create a new Java project.

  3. Create a Java file called Author.java and copy paste the following code

Author.java
/**
 * Represents an Author with their details--name, email and physical address
 *
 * @author therapon
 *
 */

public class Author {

  private String name;
  private String email;
  private String address;

  /**
   * Creates a new author given the author's name, email and address as strings.
   *
   * @param name the author's name
   * @param email the author's email address
   * @param address the authors physical address
   */
  public Author(String name, String email, String address) {
    this.name = name;
    this.email = email;
    this.address = address;
  }

  /**
   * @return the name
   */
  public String getName() {
    return this.name;
  }

  /**
   * @return the email
   */
  public String getEmail() {
    return this.email;
  }

  /**
   * @return the address
   */
  public String getAddress() {
    return this.address;
  }

  /**
   * {@inheritDoc}
   */
  @Override
  public String toString() {
    return "Author [name=" + this.name + ", email=" + this.email + ", address=" + this.address
        + "]";
  }

  /**
   * {@inheritDoc}
   */
  @Override
  public int hashCode() {
    final int prime = 31;
    int result = 1;
    result = (prime * result) + ((this.address == null) ? 0 : this.address.hashCode());
    result = (prime * result) + ((this.email == null) ? 0 : this.email.hashCode());
    result = (prime * result) + ((this.name == null) ? 0 : this.name.hashCode());
    return result;
  }

  /**
   * {@inheritDoc}
   */
  @Override
  public boolean equals(Object obj) {
    if (this == obj) {
      return true;
    }
    if (obj == null) {
      return false;
    }
    if (getClass() != obj.getClass()) {
      return false;
    }
    Author other = (Author) obj;
    if (this.address == null) {
      if (other.address != null) {
        return false;
      }
    } else if (!this.address.equals(other.address)) {
      return false;
    }
    if (this.email == null) {
      if (other.email != null) {
        return false;
      }
    } else if (!this.email.equals(other.email)) {
      return false;
    }
    if (this.name == null) {
      if (other.name != null) {
        return false;
      }
    } else if (!this.name.equals(other.name)) {
      return false;
    }
    return true;
  }

}
  1. In Eclipse, right click on the file name Author.java.

  2. Select New from the pop-up menu

  3. Select JUnit Test Case from the pop-up menu

  4. A new window will popup. In the middle of the new window select

    1. setUp()

    2. tearDown()

  5. Click Next

  6. After clicking Next a new window will east all the available methods on the class Author for which we want to write a test. Select getName()

  7. Click Finish

  8. Eclipse will ask you if you would like to add Junit 4 in your build path. Click OK

  9. Eclipse will generate a stub test class for you that should look like the one below.

AuthorTest.java
import static org.junit.Assert.*;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

public class AuthorTest {

  @Before
  public void setUp() throws Exception {}

  @After
  public void tearDown() throws Exception {}

  @Test
  public void testGetName() {
    fail("Not yet implemented");
  }

}
  1. Select AuthorTest.java on the Package Explorer Window.

  2. Right click while AuthorTest.java is selected. A pop-up menu will appear.

  3. Select Run As from the pop-up menu.

  4. Select JUnit Test

  5. Eclipse will alter some windows around, mainly your Package Explorer Window. The results of your test run appear at the same location as the one occupied by the Package Explorer window. Your test should fail.

Practise Exercises
  1. Make your test succeed.

  2. Add more tests for the method getName().

  3. Add tests for the rest of the getter methods in Author.java.

  4. Draw the UML class diagram for Author

2. Eclipse and Git

Your typical workflow would be

  1. Check out your repo from CCIS Github.

  2. Create a new Java Project in Eclipse

    1. Right click in the Package Explorer window

    2. Select New

    3. Select Java Project

    4. In the pop-up window

      1. Type a name for the Eclipse Project. This can be any name you like

      2. Unselect Use default location. This action will enable the kdb:[Browse] in the line line immediately below.

      3. Click Browse and navigate the directory on your computer in which you checked out your repo (see Step 1)

      4. Click Finish

Because we pointed Eclipse to a directory that contains a Git repo, Eclipse will load its Git extensions. To see the Eclipse Git extensions in action

  1. Expand your project in the Package Explorer window until you find the file Readme.md

  2. Select the file Readme.md.

  3. While the file Readme.md is selected, right click to access the pop-up menu.

  4. In the pop-up menu find the option named Team.

  5. The sub-menu options under Team should look familiar/identical to Git commands, e.g., Commit.

  6. The menu Repository nested in Team contains actions that let you push/pull from the server, i.e.,

    1. Push to Upstream

    2. Fetch from Upstream

There is already a repository created for you on the CCIS github with the name lab1-<login> where <login> is your CCIS login name. Check the repository to your desktop. Create a new Java project in Eclipse and move (copy/paste) all your code in your new Java Project. Push your code to the CCIS Github Server using Eclipse.

3. Creating new classes

We decided to update our Author class. Instead of holding an author’s name as a string, we would like to create a new class called Person that will hold * a person’s first name, and, * a person’s last name.

Practise Exercises
  1. Create the class Person.

  2. Create the test class PersonTest and add tests for your class Person.

  3. Update your Author class to use Person instead of a string for an author’s name.

    • Make sure you update any other parts of your code, e.g., tests, getter methods etc.

  4. Draw a new UML class diagram to show how your design has changed not that we added Person.

4. Creating more classes

We also decided that we need a better way to capture email addresses. Using a String is not convenient.

Practise Exercises
  1. Design a new class named Email. The class should hold the login name of the email address and the domain. For example the email address joe@a.b.com has

    • the login name john

    • the domain a.b.com

  2. Ensure that your toString() method prints out a valid email address, i.e., the login name followed by the at-sign @ followed by the domain

  3. Create a test class named EmailTest and add tests for all methods in the class Email that you wrote/generated code.

  4. Update your Author class to use Email instead of a string for an author’s email.

    • Make sure you update any other parts of your code, e.g., tests, getter methods etc.

  5. Draw a new UML class diagram to show how your design has changed not that we added Email.

5. Javadoc

For all the code that you have written up to this point in the lab should have also written documentation for it.

If you have not, go back and add documentation now!

Lets generate the HTML version of your code’s documentation.

  1. Select your project in the Package Explorer window.

  2. On the Eclipse main menu found at the top of your window (or the top of your screen on a Mac), click on Project menu item.

  3. From the pop-up sub-menu of Project select Generete Javadoc…​. A new window will appear.

  4. In the new window you will see a list of all your Eclipse project. Select the project that you have been working on during this lab.

  5. Towards the bottom of the window you will see a Browse button. Click Browse and designate the destination folder on your computer to store the generated documentation files. These are going to be HTML files that you can open with your browser.

  6. Click Finish

  7. Observe that at the bottom window in Eclipse you can see the progress of the javadoc generation including any errors and warnings. Make sure that there are now errors or warnings. If you have some, click on the error/warning and it will take you to the correct file and line to fix the error. Consult Javadoc documentation if you have to.

  8. On your computer navigate to the folder that you selected as the destination for the generate documentation and open the file index.html.

6. Classes again!

We would like to capture addresses more precisely than just using one long String.

Practise Exercises
  1. Design a new class named Address. The class should hold an author’s address. An address is made up of

    1. Street number

    2. Street name

    3. City name

    4. State acronym

    5. A zip code represented as a number

    6. Country name

  2. Ensure that your toString() method prints out nicely formatted address, e.g.,

    98 Main St.,
    Seattle,
    WA, 98000,
    USA
  3. Create a test class named AddressTest and add tests for all methods in the class Address that you wrote/generated code.

  4. Update your Author class to use Address instead of a string for an author’s address.

    • Make sure you update any other parts of your code, e.g., tests, getter methods etc.

  5. Draw a new UML class diagram to show how your design has changed not that we added Email.