diff options
Diffstat (limited to 'src/Q1')
| -rw-r--r-- | src/Q1/AddressBook.java | 383 | ||||
| -rw-r--r-- | src/Q1/AddressBookTest.java | 93 | ||||
| -rw-r--r-- | src/Q1/README.md | 222 | ||||
| -rw-r--r-- | src/Q1/ReversedSentence.java | 5 | ||||
| -rw-r--r-- | src/Q1/ReversedSentenceTest.java | 20 |
5 files changed, 26 insertions, 697 deletions
diff --git a/src/Q1/AddressBook.java b/src/Q1/AddressBook.java deleted file mode 100644 index 2c1b9a6..0000000 --- a/src/Q1/AddressBook.java +++ /dev/null @@ -1,383 +0,0 @@ -/** - * Assignment 1, COMP268 Class: AddressBook.java - * - * @description Represents a contact from an address book. - * @author: mo khan Student ID: 3431709 - * @date May 6, 2019 - * @version 1.0 - */ -package Q1; - -import java.util.Objects; -import java.util.Scanner; - -public class AddressBook implements Comparable<AddressBook> { - private String businessPhone; - private String cellPhone; - private String facebookId; - private String firstName = ""; - private String homeAddress; - private String homePhone; - private String lastName = ""; - private String middleName = ""; - private String personalWebSite; - private String skypeId; - - /** Create a new instance of class AddressBook. Initialize all fields to a blank string. */ - public AddressBook() { - this(""); - } - - /** - * Create a new instance of class AddressBook. Initialize the firstName and set all other fields - * to a blank string. - * - * @param firstName the first name for the contact. - */ - public AddressBook(String firstName) { - this(firstName, "", ""); - } - - /** - * Create a new instance of class AddressBook. Initialize the first name, middle name and last - * name. Set all other fields to a blank string. - * - * @param firstName the first name of the contact. - * @param middleName the middle name of the contact. - * @param lastName the last name of the contact. - */ - public AddressBook(String firstName, String middleName, String lastName) { - this(firstName, middleName, lastName, "", "", "", "", "", "", ""); - } - - /** - * Create a new instance of class AddressBook. Initialize the first name, middle name, last name, - * business phone number, cell phone number, Facebook Id, home address, home phone number, - * personal website and Skype Id of the contact. - * - * @param firstName the first name of the contact. - * @param middleName the middle name of the contact. - * @param lastName the last name of the contact. - * @param businessPhone the phone number of the contact. - * @param cellPhone the cell phone number of the contact. - * @param facebookId the Facebook Id of the contact. - * @param homeAddress the home address of the contact. - * @param homePhone the home phone number of the contact. - * @param personalWebSite the website of the contact. - * @param skypeId the Skype Id of the contact. - */ - public AddressBook( - String firstName, - String middleName, - String lastName, - String businessPhone, - String cellPhone, - String facebookId, - String homeAddress, - String homePhone, - String personalWebSite, - String skypeId) { - this.businessPhone = businessPhone; - this.cellPhone = cellPhone; - this.facebookId = facebookId; - this.firstName = firstName; - this.homeAddress = homeAddress; - this.homePhone = homePhone; - this.lastName = lastName; - this.middleName = middleName; - this.personalWebSite = personalWebSite; - this.skypeId = skypeId; - } - - /** - * Returns the business phone number. - * - * @return the business phone number. - */ - public String getBusinessPhone() { - return this.businessPhone; - } - - /** - * Returns the cell phone number. - * - * @return the cell phone number. - */ - public String getCellPhone() { - return this.cellPhone; - } - - /** - * Returns the Facebook Id. - * - * @return the Facebook Id - */ - public String getFacebookId() { - return this.facebookId; - } - - /** - * Returns the first name. - * - * @return the first name - */ - public String getFirstName() { - return this.firstName; - } - - /** - * Returns the home address. - * - * @return the home address - */ - public String getHomeAddress() { - return this.homeAddress; - } - - /** - * Returns the home phone number. - * - * @return the phone number - */ - public String getHomePhone() { - return this.homePhone; - } - - /** - * Returns the last name. - * - * @return the last name - */ - public String getLastName() { - return this.lastName; - } - - /** - * Returns the middle name. - * - * @return the middle name - */ - public String getMiddleName() { - return this.middleName; - } - - /** - * Returns the personal website. - * - * @return the website name - */ - public String getPersonalWebSite() { - return this.personalWebSite; - } - - /** - * Returns the Skype Id. - * - * @return the skype id - */ - public String getSkypeId() { - return this.skypeId; - } - - /** - * Sets the business phone number. - * - * @param value The new phone number. - */ - public void setBusinessPhone(String value) { - this.businessPhone = value; - } - - /** - * Sets the cell phone number. - * - * @param value The new cell phone number. - */ - public void setCellPhone(String value) { - this.cellPhone = value; - } - - /** - * Sets the Facebook Id. - * - * @param value The new Facebook id. - */ - public void setFacebookId(String value) { - this.facebookId = value; - } - - /** - * Sets the first name. - * - * @param name the new first name. - */ - public void setFirstName(String name) { - this.firstName = name; - } - - /** - * Sets the home address. - * - * @param address new home address. - */ - public void setHomeAddress(String address) { - this.homeAddress = address; - } - - /** - * Sets the home phone number. - * - * @param value the new home phone number. - */ - public void setHomePhone(String value) { - this.homePhone = value; - } - - /** - * Sets the last name. - * - * @param name the last name. - */ - public void setLastName(String name) { - this.lastName = name; - } - - /** - * Sets the middle name. - * - * @param name the middle name. - */ - public void setMiddleName(String name) { - this.middleName = name; - } - - /** - * Sets the personal website. - * - * @param value the personal website - */ - public void setPersonalWebSite(String value) { - this.personalWebSite = value; - } - - /** - * Sets the Skype Id. - * - * @param value the Skype Id. - */ - public void setSkypeId(String value) { - this.skypeId = value; - } - - /** - * Converts a name to an Address Book. - * - * @param name the name to parse. - * @return an AddressBook instance. - */ - public static AddressBook parseFrom(String name) { - String[] parts = name.split(" "); - - switch (parts.length) { - case 1: - return new AddressBook(parts[0]); - case 2: - return new AddressBook(parts[0], "", parts[1]); - case 3: - return new AddressBook(parts[0], parts[1], parts[2]); - default: - return new AddressBook(); - } - } - - /** - * Compares name1 with name2 and returns a string representation. Returns a negative value if - * name1 is less than name2. Returns a positive value if name1 is greater than name2. Returns a - * zero if name1 is equal to name2. - * - * @param name1 first name - * @param name2 second name - * @return string version of -1, 0, 1 - */ - public static String compareNames(String name1, String name2) { - return Integer.toString(parseFrom(name1).compareTo(parseFrom(name2))); - } - - /** - * Compares one address book with another. Returns a negative value if this is less than other. - * Returns a positive value if this is greater than other. Returns a zero if this is equal to - * other. - * - * @param other The other address book to compare to. - */ - public int compareTo(AddressBook other) { - return this.firstName.compareTo(other.firstName) - + this.middleName.compareTo(other.middleName) - + this.lastName.compareTo(other.lastName); - } - - /** - * Overrides equals to check for value equality instead of reference equality. - * - * @param o The other item to check against. - * @return true if values match, otherwise false. - */ - @Override - public boolean equals(Object o) { - if (this == o) return true; - if (!(o instanceof AddressBook)) return false; - AddressBook that = (AddressBook) o; - return Objects.equals(businessPhone, that.businessPhone) - && Objects.equals(cellPhone, that.cellPhone) - && Objects.equals(facebookId, that.facebookId) - && Objects.equals(firstName, that.firstName) - && Objects.equals(homeAddress, that.homeAddress) - && Objects.equals(homePhone, that.homePhone) - && Objects.equals(lastName, that.lastName) - && Objects.equals(middleName, that.middleName) - && Objects.equals(personalWebSite, that.personalWebSite) - && Objects.equals(skypeId, that.skypeId); - } - - /** - * Overrides hashCode to ensure instances that are equal by value equate to the same hash code for - * hash key computations. - * - * @return The hash code - */ - @Override - public int hashCode() { - return Objects.hash( - businessPhone, - cellPhone, - facebookId, - firstName, - homeAddress, - homePhone, - lastName, - middleName, - personalWebSite, - skypeId); - } - - /** - * Overrides toString to return the first name of the contact. - * - * @return the first name of the contact. - */ - @Override - public String toString() { - return this.firstName; - } - - public static void main(String[] args) { - Scanner in = new Scanner(System.in); - System.out.print("Please enter first name: "); - String first = in.next(); - - System.out.print("Please enter second name: "); - String second = in.next(); - - System.out.println("The comparison result is " + AddressBook.compareNames(first, second)); - } -} diff --git a/src/Q1/AddressBookTest.java b/src/Q1/AddressBookTest.java deleted file mode 100644 index b75d1d1..0000000 --- a/src/Q1/AddressBookTest.java +++ /dev/null @@ -1,93 +0,0 @@ -package ca.mokhan.test; - -import Q1.*; -import junit.framework.Test; -import junit.framework.TestCase; -import junit.framework.TestSuite; - -public class AddressBookTest extends TestCase { - private AddressBook subject; - - public AddressBookTest(String testName) { - super(testName); - this.subject = new AddressBook(); - } - - public static Test suite() { - return new TestSuite(AddressBookTest.class); - } - - public void testFirstName() { - subject.setFirstName("mo"); - assertEquals(subject.getFirstName(), "mo"); - } - - public void testMiddleName() { - subject.setMiddleName("tsuyoshi"); - assertEquals(subject.getMiddleName(), "tsuyoshi"); - } - - public void testLastName() { - subject.setLastName("garrett"); - assertEquals(subject.getLastName(), "garrett"); - } - - public void testHomeAddress() { - subject.setHomeAddress("1 University Dr, Athabasca, AB T9S 3A3"); - assertEquals(subject.getHomeAddress(), "1 University Dr, Athabasca, AB T9S 3A3"); - } - - public void testBusinessPhone() { - subject.setBusinessPhone("1-800-788-9041"); - assertEquals(subject.getBusinessPhone(), "1-800-788-9041"); - } - - public void testHomePhone() { - subject.setHomePhone("1-800-788-9041"); - assertEquals(subject.getHomePhone(), "1-800-788-9041"); - } - - public void testCellPhone() { - subject.setCellPhone("1-800-788-9041"); - assertEquals(subject.getCellPhone(), "1-800-788-9041"); - } - - public void testSkypeId() { - subject.setSkypeId("1-800-788-9041"); - assertEquals(subject.getSkypeId(), "1-800-788-9041"); - } - - public void testFacebookId() { - subject.setFacebookId("1-800-788-9041"); - assertEquals(subject.getFacebookId(), "1-800-788-9041"); - } - - public void testPersonalWebsite() { - subject.setPersonalWebSite("https://www.mokhan.ca/"); - assertEquals(subject.getPersonalWebSite(), "https://www.mokhan.ca/"); - } - - public void testCompareNames() { - assertTrue( - Integer.parseInt(AddressBook.compareNames("Tsuyoshi M. Garret", "Takashi Shirogane")) > 0); - assertTrue( - Integer.parseInt(AddressBook.compareNames("Takashi Shirogane", "Tsuyoshi M. Garret")) < 0); - assertTrue( - Integer.parseInt(AddressBook.compareNames("Tsuyoshi Garret", "Tsuyoshi Garret")) == 0); - } - - public void testCompareTo() { - AddressBook hunk = new AddressBook(); - hunk.setFirstName("Tsuyoshi"); - hunk.setLastName("Garrett"); - - AddressBook shiro = new AddressBook(); - shiro.setFirstName("Takashi"); - shiro.setLastName("Shirogane"); - - assertTrue(hunk.compareTo(shiro) > 0); - assertTrue(shiro.compareTo(hunk) < 0); - assertTrue(hunk.compareTo(hunk) == 0); - assertTrue(shiro.compareTo(shiro) == 0); - } -} diff --git a/src/Q1/README.md b/src/Q1/README.md index 57457fe..c40e830 100644 --- a/src/Q1/README.md +++ b/src/Q1/README.md @@ -1,230 +1,10 @@ -Learning Profile for Assignment #1, And Question #1 +Learning Profile for Assignment #2, And Question #1 Name: Mo Khan Student ID: 3431709 1. Problem Statement: - -Create a class named AddressBook that has the following field names: - -* firstName -* middleName -* lastName -* homeAddress -* businessPhone -* homePhone -* cellphone -* skypeId -* facebookId -* personalWebSite - -Use appropriate data types to store the values for these fields in AddressBook objects. -Create appropriate get and set methods to retrieve and assign values to these names. -For example `getMiddleName(viveAddressBook)` should return the middle name of the person `Vive`. -Similarly, `vive.setPersonalWebsite(url)` should set the personal website of the person `Vive` to the specified URL object. -Using the get and set methods, create a comparison method `compareNames(name1, name2)` that compares the first, middle, and last names of strings name1 and name2. -Assume that name1 and name2 follow the following format: "FirstName M. LastName". - -Test your program for correct, partially correct (e.g., name string without the middleName), -and incorrect inputs (e.g., phone number containing special characters). - - 2. Description of the Code: - -I solved this exercise by installing `maven` and `junit`. Then I started -implementing the `AddressBook` API one method at a time. - -I had trouble understanding the `compareNames` API because it was -non-intuitive to me. I have previously worked with `C#` and am familiar -with the `IComparer<T>` interface so I looked for the equivalent in -Java. I found the `Comparable<T>` interface and chose to implement that. - -The `Comparable<T>` interface requires implementers to implement a -method named `compareTo` that returns an integer value. This interface -is used for sorting. - -Instead of trying to interpret how the `compareNames` method is supposed -to behave, I chose to delegate to `compareTo` and return the string -representation of the integer value. - -I also chose to override equality for this object to use `value` -equality instead of `reference` equality. - -For code style I chose to use the [Google Java Style Guide](https://google.github.io/styleguide/javaguide.html). I used a maven plugin to auto format the code to abide by the style guide each time I ran the tests. - 3. Errors and Warnings: - -I practice test driven development. So I always focus on writing a -failing test first. This allows me to make sure that I see the intended -failure in my test before implementing the required code to make it -pass. It also helps me design the API of my software from the -perspective of the client code that would use my software. The design -benefits of test driven development were not as useful here, since the -design of the API was provided to us in the assignment. - -Here's an example of a failure that occurred when I wrote the test first -before implementing the required interface. - -```bash -モ mvn test -[INFO] Scanning for projects... -[INFO] -[INFO] -------------------< ca.mokhan.comp268:assignment1 >-------------------- -[INFO] Building assignment1 1.0-SNAPSHOT -[INFO] --------------------------------[ jar ]--------------------------------- -[INFO] -[INFO] --- fmt-maven-plugin:2.8:format (default) @ assignment1 --- -[INFO] Processed 8 files (0 reformatted). -[INFO] -[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ assignment1 --- -[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent! -[INFO] skip non existing resourceDirectory /Users/mokha/development/gh/comp-268/src/main/resources -[INFO] -[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ assignment1 --- -[INFO] Changes detected - recompiling the module! -[WARNING] File encoding has not been set, using platform encoding UTF-8, i.e. build is platform dependent! -[INFO] Compiling 4 source files to /Users/mokha/development/gh/comp-268/target/classes -[INFO] ------------------------------------------------------------- -[ERROR] COMPILATION ERROR : -[INFO] ------------------------------------------------------------- -[ERROR] /Users/mokha/development/gh/comp-268/src/Q1/AddressBookTest.java:[21,12] cannot find symbol - symbol: method setFirstName(java.lang.String) - location: variable subject of type Q1.AddressBook -[ERROR] /Users/mokha/development/gh/comp-268/src/Q1/AddressBookTest.java:[81,9] cannot find symbol - symbol: method setFirstName(java.lang.String) - location: variable hunk of type Q1.AddressBook -[ERROR] /Users/mokha/development/gh/comp-268/src/Q1/AddressBookTest.java:[85,10] cannot find symbol - symbol: method setFirstName(java.lang.String) - location: variable shiro of type Q1.AddressBook -[INFO] 3 errors -[INFO] ------------------------------------------------------------- -[INFO] ------------------------------------------------------------------------ -[INFO] BUILD FAILURE -[INFO] ------------------------------------------------------------------------ -[INFO] Total time: 1.759 s -[INFO] Finished at: 2019-05-13T19:55:36-06:00 -[INFO] ------------------------------------------------------------------------ -[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:compile (default-compile) on project assignment1: Compilation failure: Compilation failure: -[ERROR] /Users/mokha/development/gh/comp-268/src/Q1/AddressBookTest.java:[21,12] cannot find symbol -[ERROR] symbol: method setFirstName(java.lang.String) -[ERROR] location: variable subject of type Q1.AddressBook -[ERROR] /Users/mokha/development/gh/comp-268/src/Q1/AddressBookTest.java:[81,9] cannot find symbol -[ERROR] symbol: method setFirstName(java.lang.String) -[ERROR] location: variable hunk of type Q1.AddressBook -[ERROR] /Users/mokha/development/gh/comp-268/src/Q1/AddressBookTest.java:[85,10] cannot find symbol -[ERROR] symbol: method setFirstName(java.lang.String) -[ERROR] location: variable shiro of type Q1.AddressBook -[ERROR] -> [Help 1] -[ERROR] -[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch. -[ERROR] Re-run Maven using the -X switch to enable full debug logging. -[ERROR] -[ERROR] For more information about the errors and possible solutions, please read the following articles: -[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoFailureException -``` - -Here is the output after fixing the failing test: - -```bash -モ mvn test -[INFO] Scanning for projects... -[INFO] -[INFO] -------------------< ca.mokhan.comp268:assignment1 >-------------------- -[INFO] Building assignment1 1.0-SNAPSHOT -[INFO] --------------------------------[ jar ]--------------------------------- -[INFO] -[INFO] --- fmt-maven-plugin:2.8:format (default) @ assignment1 --- -[INFO] Processed 8 files (0 reformatted). -[INFO] -[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ assignment1 --- -[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent! -[INFO] skip non existing resourceDirectory /Users/mokha/development/gh/comp-268/src/main/resources -[INFO] -[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ assignment1 --- -[INFO] Changes detected - recompiling the module! -[WARNING] File encoding has not been set, using platform encoding UTF-8, i.e. build is platform dependent! -[INFO] Compiling 4 source files to /Users/mokha/development/gh/comp-268/target/classes -[INFO] -[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ assignment1 --- -[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent! -[INFO] skip non existing resourceDirectory /Users/mokha/development/gh/comp-268/src/test/resources -[INFO] -[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ assignment1 --- -[INFO] Changes detected - recompiling the module! -[WARNING] File encoding has not been set, using platform encoding UTF-8, i.e. build is platform dependent! -[INFO] Compiling 4 source files to /Users/mokha/development/gh/comp-268/target/test-classes -[INFO] -[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ assignment1 --- -[INFO] Surefire report directory: /Users/mokha/development/gh/comp-268/target/surefire-reports - -------------------------------------------------------- - T E S T S -------------------------------------------------------- -Running ca.mokhan.comp268.AppTest -Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.01 sec -Running ca.mokhan.test.AddressBookTest -Tests run: 12, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.001 sec - -Results : - -Tests run: 13, Failures: 0, Errors: 0, Skipped: 0 - -[INFO] ------------------------------------------------------------------------ -[INFO] BUILD SUCCESS -[INFO] ------------------------------------------------------------------------ -[INFO] Total time: 2.552 s -[INFO] Finished at: 2019-05-13T19:56:21-06:00 -[INFO] ------------------------------------------------------------------------ -``` - 4. Sample Input and Output: - -The test cases are described in `AddressBookTest.java`. - 5. Discussion: - -When I look at the design for the `AddressBook` class there are a few -things that come to mind. - -1. The name `AddressBook` is a poor choice for a name. This object does - not represent an address book but rather a single contact in an - address book. I would split this class into two. One class named - `Contact` that represents the individual details for a contact in the - address book and a class named `AddressBook` that acts as the - aggregate root for contacts. -2. This is more of a comment on Java than the API design, but `Java` - properties seem to requires A LOT of ceremony. - -For example in `Ruby` you can define a property/attribute very -succinctly. - -```ruby -class Contact - attr_accessor :first_name -end -``` - -C# also has a nice shorthand for this. C# also allows specifying -different access control for the getter and setter. - -```csharp -public class Contact { - public string FirstName { get; set; } -} -``` - -Finally the `Java` version requires much more ceremony. - -```java -public class Contact { - private String firstName; - - public String getFirstName() { - return this.firstName; - } - - public void setFirstName(String firstName) { - this.firstName = firstName; - } -} -``` diff --git a/src/Q1/ReversedSentence.java b/src/Q1/ReversedSentence.java new file mode 100644 index 0000000..055e9c5 --- /dev/null +++ b/src/Q1/ReversedSentence.java @@ -0,0 +1,5 @@ +package Q1; + +public class ReversedSentence { + public static void main(String[] args) {} +} diff --git a/src/Q1/ReversedSentenceTest.java b/src/Q1/ReversedSentenceTest.java new file mode 100644 index 0000000..75d809d --- /dev/null +++ b/src/Q1/ReversedSentenceTest.java @@ -0,0 +1,20 @@ +package ca.mokhan.test; + +import Q1.*; +import junit.framework.Test; +import junit.framework.TestCase; +import junit.framework.TestSuite; + +public class ReversedSentenceTest extends TestCase { + public ReversedSentenceTest(String testName) { + super(testName); + } + + public static Test suite() { + return new TestSuite(ReversedSentenceTest.class); + } + + public void testTruthy() { + assertEquals(true, true); + } +} |
