CS5044 Homework 4 Solved

50.00 $

Category:
Click Category Button to View Your Next Assignment | Homework

You'll get a download link with a: zip solution files instantly, after Payment

Securely Powered by: Secure Checkout

Description

5/5 - (2 votes)

Deliverables:

Submit a project via Web-CAT, with two files in the “person” package:

  • person java
  • personTest java

Homework Instructions

Modify the Person class from the Java Tutorial so that it is immutable, and create a JUnit test class for it called PersonTest.java. Both classes should reside in the package “person”. Your code should pass all of your unit tests, which should cover all statements and conditions in your code, and your code should pass all of the tests in Web-CAT. Your source code (but not the tests) should also meet all the Web-CAT style guidelines. You may submit your code to Web-CAT as many times as you like before the deadline.

Walkthrough of the Entire Homework

Dr. K provides a walkthrough of the entire mini-project in a series of videos:

  • A Tour of the Person Class from the Java Tutorial (1 video)
  • Making the Person Class Immutable (6 videos)
  • Unit Testing the Immutable Person Class (5 videos)

Note that Web-CAT style guidelines have been ignored in these videos. You will need to fix the style problems in your submission, including adding Javadocs to the Person class. You can do this as you follow along with the video or any time after.

Important Epilogue to the Videos

After following along in all the videos, your Person class will still have a few minor structural issues that will need to be resolved in order to pass all the Web-CAT tests:

  • In the areAnyNull() and areAnyInvalid() helper methods generated by Eclipse, the parameter names chosen by Eclipse are exactly the same as the names of the instance fields, which is considered a bad practice (and rejected by Web-CAT).  There are two ways to fix this, with the second option being much easier than the first:
    Option 1 – You can refactor the parameter names to be different (such as renaming “name” to “nameX”). To do this, place the cursor within the “name” parameter of the areAnyNull() method signature, type Alt-Shift-R, and change the name of the parameter. This should change all instances (even in your Javadocs, if you’ve added them already) for that method. Repeat this action for each of the other parameter names within the areAnyNull() method signature, and again for all parameter names within the areAnyInvalid() method signature.
    Option 2 – You can reintegrate the methods you extracted during the video, placing the code back into the constructor. To do this, place the cursor within the call to areAnyNull() within the constructor, and type Alt-Shift-I (that’s the letter “i”) to “inline” the method and click OK. Repeat this action for the call to the areAnyInvalid() method.
  • The Person constructor throws NullPointerException if any parameter is null, but Web-CAT considers it a bad practice to explicitly throw this exception. It can indeed be confusing in some instances, but it’s certainly not a bad practice in general. However, we need to pass the Web-CAT tests, so you must change the constructor to throw a new IllegalArgumentException instead. As a result, you will also need to update the “expect=” annotation for several of your test methods accordingly, from NullPointerException.class to IllegalArgumentException.class.
  • Finally, within the equals() method generated by Eclipse, the last if() statement is actually redundant, and Web-CAT will reject it as such. The code is perfectly acceptable as it is, and arguably more readable. However, we again must appease Web-CAT.  To do so, you’ll need to change the following four lines:

if (!emailAddress.equals(that.emailAddress)) {
return false;
}
return true;

into this one line instead:

return emailAddress.equals(that.emailAddress); // note the lack of “!”

The code is now technically less redundant, but most developers would probably prefer the original form as more readable.

After making these changes, your code should still pass all the test methods in PersonTest, and you should still have 100% code coverage from Eclipse. Once you’ve addressed all the style issues as well, your project should receive 30/30 from Web-CAT.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Recordings: Immutable Person

Three Playlists

The videos on this page represent a mini review of some of the principles and techniques you have learned throughout the course so far.

There are three sections to this document (grand total run time 2:12:26)

  • A Tour of the Person Class from the Java Tutorial (total run time 16:55)
  • Making the Person Class Immutable (total run time 42:31)
  • Unit Testing the Immutable Person Class (total run time 1:13:00)

A Tour of the Person Class from the Java Tutorial

This following video looks at a Person class from the Java Tutorial. You can download the Person class here:

https://docs.oracle.com/javase/tutorial/java/javaOO/examples/Person.java (Links to an external site.) 

To follow along with the video, place this downloaded class in a new Eclipse project, within a “person” package.

Playlist is a single video (total run time 16:55)

Person Class Tour 16:55 no slides

Watch Video on YouTube (Links to an external site.)

Making the Person Class Immutable

In this video we make the Person class from the last video into a true immutable class. In doing so, we also add exceptions to the constructor, a toString() method, a hashCode() and an equals() method, and we make the class comparable by adding a compareTo() method.

Playlist includes the following videos (total run time 42:31)

Fields Constructor 6:30 no slides
Accessors 1:33 no slides
Constructor Exceptions 14:59 no slides
ToString 1:29 no slides
HashCode Equals 9:36 no slides
Comparable 8:24 no slides

Watch Playlist on YouTube (Links to an external site.)

 

Unit Testing the Immutable Person Class

The following videos begin with several prerequisites not covered above, so if you want to follow along in Eclipse, you’ll need to take a few steps before you start this video:

  • If you have not yet done so, place the Person class above in the “person” package.
  • Add the JUnit 4 library to your project by right-clicking your project, then select Build Path | Configure Build Path. In the Libraries tab, select Classpath and click Add Library to add JUnit (be sure to select JUnit version 4).
  • Right-click your project and click New | Source Folder and name the new folder “test” then right-click this folder and add a New | Package named “person” here.
  • Download the initial javaclass, as it exists at the starting point of the upcoming videos, and add it to the person package of the “test” folder.
  • Configure Coverage by selecting “Coverage…” from the Run menu. In the Test tab, use Search to select “person.PersonTest” as the Test Class (if not already there). In the Coverage tab, ensure that only the “src” folder is checked, with the “test” folder (and all other rows) left unchecked.

 

 

 

 

 

 

Playlist includes the following videos (total run time 1:13:00)

Coverage Review 7:50 no slides
Exceptions 19:24 no slides
ToString HashCode 11:54 no slides
Equals 18:41 no slides
CompareTo 15:11 no slides

Watch Playlist on YouTube (Links to an external site.)

 

 

 

  • H4-cxlbhh.zip