diff options
Diffstat (limited to 'src')
34 files changed, 26 insertions, 3434 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); + } +} diff --git a/src/Q10/README.md b/src/Q10/README.md deleted file mode 100644 index 112696a..0000000 --- a/src/Q10/README.md +++ /dev/null @@ -1,170 +0,0 @@ -Learning Profile for Assignment #1, And Question #2 - -Name: Mo Khan -Student ID: 3431709 - -1. Problem Statement: - -Modify the following program to the specifications given below: - -* I. Add a new status `SingleParent` where the tax is computed as a SINGLE but with a further reduction of $5000 per child. -* II. Add a new tax condition - if the income is greater than $249,999 for SINGLE, then add a tax of 25% on income amount above $150,000; if the income is greater than $349,999 for MARRIED, then add a tax of 35% on income amount above $200,000. -* III. Unknown status - if the status doesn't belong to `SINGLE` or `MARRIED` or `SINGLE_PARENT`, then compute a 33% tax on the income. - -```java -import java.util.Scanner; - -public class TaxReturn { - public TaxReturn(double anIncome, int aStatus) { - income = anIncome; - status = aStatus; - } - - public double getTax() { - double tax = 0; - if (status == SINGLE) { - if (income <= SINGLE_BRACKET1) tax = RATE1 * income; - else if (income <= SINGLE_BRACKET2) - tax = RATE1 * SINGLE_BRACKET1 + RATE2 * (income - SINGLE_BRACKET1); - else - tax = - RATE1 * SINGLE_BRACKET1 - + RATE2 * (SINGLE_BRACKET2 - SINGLE_BRACKET1) - + RATE3 * (income - SINGLE_BRACKET2); - } else { - if (income <= MARRIED_BRACKET1) tax = RATE1 * income; - else if (income <= MARRIED_BRACKET2) - tax = RATE1 * MARRIED_BRACKET1 + RATE2 * (income - MARRIED_BRACKET1); - else - tax = - RATE1 * MARRIED_BRACKET1 - + RATE2 * (MARRIED_BRACKET2 - MARRIED_BRACKET1) - + RATE3 * (income - MARRIED_BRACKET2); - } - return tax; - } - - public static final int SINGLE = 1; - public static final int MARRIED = 2; - private static final double RATE1 = 0.15; - private static final double RATE2 = 0.28; - private static final double RATE3 = 0.31; - private static final double SINGLE_BRACKET1 = 21450; - private static final double SINGLE_BRACKET2 = 51900; - private static final double MARRIED_BRACKET1 = 35800; - private static final double MARRIED_BRACKET2 = 86500; - private double income; - private int status; - - public static void main(String[] args) { - Scanner in = new Scanner(System.in); - System.out.print("Please enter your income: "); - double income = in.nextDouble(); - System.out.print("Enter S (single) or M (married): "); - String input = in.next(); - int status = 0; - if (input.equalsIgnoreCase("S")) status = TaxReturn.SINGLE; - else if (input.equalsIgnoreCase("M")) status = TaxReturn.MARRIED; - else { - System.out.println("Bad input."); - return; - } - TaxReturn aTaxReturn = new TaxReturn(income, status); - System.out.println("The tax is " + aTaxReturn.getTax()); - } -} -``` - -2. Description of the Code: - -I started by adding tests for the existing code. By adding test coverage -for the existing code, this makes it possible to add new features -without breaking old features. - -I chose to decrease the taxable income in the constructor if the person -is single. - - -3. Errors and Warnings: - -```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 48 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 24 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 24 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.007 sec -Running ca.mokhan.test.CandidateTest -Tests run: 4, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.053 sec -Running ca.mokhan.test.NumberTest -Tests run: 5, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.021 sec -Running ca.mokhan.test.EmployeeSavingsTest -Tests run: 6, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.008 sec -Running ca.mokhan.test.CartesianCoordinateSystemTest -Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0 sec -Running ca.mokhan.test.CommunicationTest -Tests run: 2, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0 sec -Running ca.mokhan.test.TaxReturnTest -Tests run: 12, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.001 sec -Running ca.mokhan.test.BanffMarathonRunnerTest -Tests run: 4, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.001 sec -Running ca.mokhan.test.AddressBookTest -Tests run: 12, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0 sec -Running ca.mokhan.test.TriangleTest -Tests run: 2, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0 sec -Running ca.mokhan.test.BonusOnSavingsTest -Tests run: 2, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0 sec -Running ca.mokhan.test.HailstoneSequenceTest -Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0 sec - -Results : - -Tests run: 52, Failures: 0, Errors: 0, Skipped: 0 - -[INFO] ------------------------------------------------------------------------ -[INFO] BUILD SUCCESS -[INFO] ------------------------------------------------------------------------ -[INFO] Total time: 2.871 s -[INFO] Finished at: 2019-05-13T20:07:42-06:00 -[INFO] ------------------------------------------------------------------------ -``` - -4. Sample Input and Output: - -Test cases are defined in `TaxReturnTest.java`. - -5. Discussion: - -There is a lot of conditional logic in `getTax()`. I considered -[Refactor conditional with polymorphism](https://refactoring.com/catalog/replaceConditionalWithPolymorphism.html) -but decided not to make this change. diff --git a/src/Q10/TaxReturn.java b/src/Q10/TaxReturn.java deleted file mode 100644 index 7fbd48b..0000000 --- a/src/Q10/TaxReturn.java +++ /dev/null @@ -1,120 +0,0 @@ -/** - * Assignment 1, COMP268 Class: TaxReturn.java - * - * @description Represents a contact from an address book. - * @author: mo khan Student ID: 3431709 - * @date May 6, 2019 - * @version 1.0 - */ -package Q10; - -import java.util.Scanner; - -public class TaxReturn { - private double income; - private int status; - private int children; - - /** - * Constructs a TaxReturn object for a given income and marital status, and computes the tax. - * - * @param income the taxpayer income - * @param status either SINGLE or MARRIED - */ - public TaxReturn(double income, int status) { - this(income, status, 0); - } - - /** - * Constructs a TaxReturn object for a given income and marital status and, and # of children. - * - * @param income the taxpayer income - * @param status either SINGLE or MARRIED - * @param children the number of children - */ - public TaxReturn(double income, int status, int children) { - this.income = income; - this.status = status; - this.children = children; - if (this.isSingle()) this.income -= children * 5000; - } - - /** - * Returns the calculated taxes to pay. - * - * @return the amount of tax to pay. - */ - public double getTax() { - double tax = 0; - - if (isSingle()) { - if (income <= SINGLE_BRACKET1) tax = RATE1 * income; - else if (income <= SINGLE_BRACKET2) - tax = RATE1 * SINGLE_BRACKET1 + RATE2 * (income - SINGLE_BRACKET1); - else - tax = - RATE1 * SINGLE_BRACKET1 - + RATE2 * (SINGLE_BRACKET2 - SINGLE_BRACKET1) - + RATE3 * (income - SINGLE_BRACKET2); - - if (income > 249999.0) tax += (income - 150000) * 0.25; - } else if (isMarried()) { - if (income <= MARRIED_BRACKET1) tax = RATE1 * income; - else if (income <= MARRIED_BRACKET2) - tax = RATE1 * MARRIED_BRACKET1 + RATE2 * (income - MARRIED_BRACKET1); - else - tax = - RATE1 * MARRIED_BRACKET1 - + RATE2 * (MARRIED_BRACKET2 - MARRIED_BRACKET1) - + RATE3 * (income - MARRIED_BRACKET2); - - if (income > 349999.0) tax += (income - 200000) * 0.35; - } else { - tax = income * 0.33; - } - return tax; - } - - public static final int SINGLE = 1; - public static final int MARRIED = 2; - - public static final double RATE1 = 0.15; - public static final double RATE2 = 0.28; - public static final double RATE3 = 0.31; - - public static final double SINGLE_BRACKET1 = 21450; - public static final double SINGLE_BRACKET2 = 51900; - - public static final double MARRIED_BRACKET1 = 35800; - public static final double MARRIED_BRACKET2 = 86500; - - private boolean isSingle() { - return this.status == SINGLE; - } - - private boolean isMarried() { - return this.status == MARRIED; - } - - public static void main(String[] args) { - Scanner in = new Scanner(System.in); - System.out.print("Please enter your income: "); - double income = in.nextDouble(); - - System.out.print("Enter S (single) or M (married): "); - String input = in.next(); - int status = 0; - - if (input.equalsIgnoreCase("S")) status = TaxReturn.SINGLE; - else if (input.equalsIgnoreCase("M")) status = TaxReturn.MARRIED; - else { - System.out.println("Bad input."); - return; - } - System.out.print("Enter # of children: "); - int children = in.nextInt(); - - TaxReturn aTaxReturn = new TaxReturn(income, status, children); - System.out.println("The tax is " + aTaxReturn.getTax()); - } -} diff --git a/src/Q10/TaxReturnTest.java b/src/Q10/TaxReturnTest.java deleted file mode 100644 index c1af186..0000000 --- a/src/Q10/TaxReturnTest.java +++ /dev/null @@ -1,91 +0,0 @@ -package ca.mokhan.test; - -import Q10.*; -import junit.framework.Test; -import junit.framework.TestCase; -import junit.framework.TestSuite; - -public class TaxReturnTest extends TestCase { - public TaxReturnTest(String testName) { - super(testName); - } - - public static Test suite() { - return new TestSuite(TaxReturnTest.class); - } - - public void test_SINGLE_BRACKET1() { - TaxReturn subject = new TaxReturn(TaxReturn.SINGLE_BRACKET1 - 0.01, TaxReturn.SINGLE); - assertEquals(3217.4985, subject.getTax()); - } - - public void test_SINGLE_BRACKET1_1_CHILD() { - TaxReturn subject = new TaxReturn(TaxReturn.SINGLE_BRACKET1 - 0.01, TaxReturn.SINGLE, 1); - assertEquals(2467.4985, subject.getTax()); - } - - public void test_SINGLE_BRACKET1_2_CHILDREN() { - TaxReturn subject = new TaxReturn(TaxReturn.SINGLE_BRACKET1 - 0.01, TaxReturn.SINGLE, 2); - assertEquals(1717.4985000000001, subject.getTax()); - } - - public void test_SINGLE_BRACKET1_3_CHILDREN() { - TaxReturn subject = new TaxReturn(TaxReturn.SINGLE_BRACKET1 - 0.01, TaxReturn.SINGLE, 3); - assertEquals(967.4985000000001, subject.getTax()); - } - - public void test_SINGLE_BRACKET2() { - TaxReturn subject = new TaxReturn(TaxReturn.SINGLE_BRACKET2 - 0.01, TaxReturn.SINGLE); - assertEquals(11743.4972, subject.getTax()); - } - - public void test_SINGLE_BRACKET3() { - TaxReturn subject = new TaxReturn(TaxReturn.SINGLE_BRACKET2 + 0.01, TaxReturn.SINGLE); - assertEquals(11743.5031, subject.getTax()); - } - - // if the income is greater than $249,999 for SINGLE, then add a tax of 25% on income amount above - // $150,000; - public void test_SINGLE_Income_Greater_Than_250K() { - // $0 - $21450: 0.15% = $3,217.5 - // $21450 - $51900: 0.28% = $8,526.0 - // $51900 - $250,000: 0.31% = $61411.0 - // $150,000 - $250,000: 0.25% = $25,000.0 - // total: $98,154.50 - - TaxReturn subject = new TaxReturn(250000, TaxReturn.SINGLE); - assertEquals(98154.50, subject.getTax()); - } - - public void test_MARRIED_BRACKET1() { - TaxReturn subject = new TaxReturn(TaxReturn.MARRIED_BRACKET1 - 0.01, TaxReturn.MARRIED); - assertEquals(5369.9985, subject.getTax()); - } - - public void test_MARRIED_BRACKET2() { - TaxReturn subject = new TaxReturn(TaxReturn.MARRIED_BRACKET2 - 0.01, TaxReturn.MARRIED); - assertEquals(19565.997200000005, subject.getTax()); - } - - public void test_MARRIED_BRACKET3() { - TaxReturn subject = new TaxReturn(TaxReturn.MARRIED_BRACKET2 + 0.01, TaxReturn.MARRIED); - assertEquals(19566.003099999998, subject.getTax()); - } - - // if the income is greater than $349,999 for MARRIED, then add a tax of 35% on income amount - // above $200,000. - public void test_MARRIED_Income_Greater_Than_350K() { - // $0 - $35,800: 0.15% = $5,370.0 - // $35,800 - $86,500: 0.28% = $14196.000000000002 - // $86,500 - $350,000: 0.31% = $81685.0 - // $200,000 - $350,000: 0.35% = $52500.0 - // total: $153,751.0 - TaxReturn subject = new TaxReturn(350000, TaxReturn.MARRIED); - assertEquals(153751.0, subject.getTax()); - } - - public void test_UNKNOWN_status_Should_tax_at_33_percent() { - TaxReturn subject = new TaxReturn(100000, 3); - assertEquals(33000.0, subject.getTax()); - } -} diff --git a/src/Q2/BonusOnSavings.java b/src/Q2/BonusOnSavings.java deleted file mode 100644 index 2872d02..0000000 --- a/src/Q2/BonusOnSavings.java +++ /dev/null @@ -1,101 +0,0 @@ -/** - * Assignment 1, COMP268 Class: BonusOnSavings.java - * - * @description Represents an object that can compute a bonus based on the amount an employee saves. - * @author: mo khan Student ID: 3431709 - * @date May 7, 2019 - * @version 1.0 - */ -package Q2; - -import java.util.Scanner; - -public class BonusOnSavings { - double annualRate = 0.0; - double quarterlyRate = 0.0; - - /** Constructs a BonusOnSavings with a default quarterly rate of 3% and annual rate of 5%. */ - public BonusOnSavings() { - this(0.03, 0.05); - } - - /** - * Constructs a BonusOnSavings object for a given income and marital status, and computes the tax. - * - * @param quarterlyRate the quarterly rate to apply. - * @param annualRate the annual rate to apply. - */ - public BonusOnSavings(double quarterlyRate, double annualRate) { - this.quarterlyRate = quarterlyRate; - this.annualRate = annualRate; - } - - /** - * Computes the bonus based on the target monthly commitment and the actual quarterly - * contributions. - * - * @param monthlyCommitment the target monthly commitment for each month. - * @param q1 the actual contribution for the first quarter. - * @param q2 the actual contribution for the second quarter. - * @param q3 the actual contribution for the third quarter. - * @param q4 the actual contribution for the fourth quarter. - * @return the calculated bonus amount for the year. - */ - public double computeBonus(double monthlyCommitment, double q1, double q2, double q3, double q4) { - double quarterlyTarget = monthlyCommitment * 3; - double annualTarget = monthlyCommitment * 12; - - return this.quarterlyBonus(quarterlyTarget, q1) - + this.quarterlyBonus(quarterlyTarget, q2) - + this.quarterlyBonus(quarterlyTarget, q3) - + this.quarterlyBonus(quarterlyTarget, q4) - + this.annualBonus(annualTarget, q1 + q2 + q3 + q4); - } - - /** - * Computes the quarterly bonus for a given target and actual contribution amount. - * - * @param target the target quarterly contribution amount for this employee. - * @param actual the actual quarterly contribution amount for this employee. - * @return the calculated quarterly bonus - */ - private double quarterlyBonus(double target, double actual) { - return (actual >= target) ? actual * this.quarterlyRate : 0.0; - } - - /** - * Calculates the annual bonus amount for the target annual contribution and the actual - * contribution. - * - * @param target the target annual contribution - * @param actual the actual contribution amount for the year - * @return the calculated annual bonus - */ - private double annualBonus(double target, double actual) { - if (actual < target) return 0.0; - - return (actual * this.annualRate) + ((actual - target) * 0.25); - } - - public static void main(String[] args) { - Scanner in = new Scanner(System.in); - System.out.print("Please enter your target monthly contribution: "); - double monthlyCommitment = in.nextDouble(); - - System.out.print("Please enter Q1 contributions: "); - double q1 = in.nextDouble(); - - System.out.print("Please enter Q2 contributions: "); - double q2 = in.nextDouble(); - - System.out.print("Please enter Q3 contributions: "); - double q3 = in.nextDouble(); - - System.out.print("Please enter Q4 contributions: "); - double q4 = in.nextDouble(); - - double bonus = new BonusOnSavings().computeBonus(monthlyCommitment, q1, q2, q3, q4); - - System.out.println("The calculated bonus is " + bonus); - } -} diff --git a/src/Q2/BonusOnSavingsTest.java b/src/Q2/BonusOnSavingsTest.java deleted file mode 100644 index 91198fd..0000000 --- a/src/Q2/BonusOnSavingsTest.java +++ /dev/null @@ -1,27 +0,0 @@ -package ca.mokhan.test; - -import Q2.*; -import junit.framework.Test; -import junit.framework.TestCase; -import junit.framework.TestSuite; - -public class BonusOnSavingsTest extends TestCase { - private BonusOnSavings subject; - - public BonusOnSavingsTest(String testName) { - super(testName); - this.subject = new BonusOnSavings(); - } - - public static Test suite() { - return new TestSuite(BonusOnSavingsTest.class); - } - - public void testComputeBonusEmployeeOne() { - assertEquals(1650.00, subject.computeBonus(2000, 5000, 7000, 4000, 8000)); - } - - public void testComputeBonusEmployeeTwo() { - assertEquals(4680.00, subject.computeBonus(3000, 6000, 9000, 10000, 17000)); - } -} diff --git a/src/Q2/README.md b/src/Q2/README.md deleted file mode 100644 index 1c66db0..0000000 --- a/src/Q2/README.md +++ /dev/null @@ -1,171 +0,0 @@ -Learning Profile for Assignment #1, And Question #2 - -Name: Mo Khan -Student ID: 3431709 - -1. Problem Statement: - -Space Inc. will give a quarterly and annual bonus to its employees only if the savings of the quarter and/or -the year are greater than or equal to quarterly minimum (monthly commitment x 3) and/or the annual minimum (monthly commitment x 12) amount, respectively. - -The quarterly bonus is 3% of eligible quarterly savings, and the annual bonus is 5% of annual savings if eligible. -If the annual savings exceeds the committed amount by at least 25%, Space Inc. matches the additional savings (25% or above) as part of the annual bonus. - -I. An employee has committed to save $2000 per month. - -Her quarterly savings are as follows: - -* Q1 – $5000 -* Q2 – $7000 -* Q3 – $4000 -* Q4 – $8000 - -II. Another employee has committed to save $3000 per month. -His quarterly savings are as follows: - -* Q1 – $6000 -* Q2 – $9000 -* Q3 – $10000 -* Q4 – $17000 - -Write a program to compute the total bonus amount earned by these two employees in the year. - -2. Description of the Code: - -I created a class that implemented the interface described in the -assignment. To calculate the bonus for the year I created a method -called `quarterlyBonus` to calculate the bonus for each month. I also -created a method called `annualBonus` to calculate the additional bonus -for the annual target. Then I summed the quarterly bonuses with the -annual bonus to calculate the final bonus amount. - -3. Errors and Warnings: - -```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 48 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 24 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 24 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.007 sec -Running ca.mokhan.test.CandidateTest -Tests run: 4, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.053 sec -Running ca.mokhan.test.NumberTest -Tests run: 5, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.021 sec -Running ca.mokhan.test.EmployeeSavingsTest -Tests run: 6, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.008 sec -Running ca.mokhan.test.CartesianCoordinateSystemTest -Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0 sec -Running ca.mokhan.test.CommunicationTest -Tests run: 2, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0 sec -Running ca.mokhan.test.TaxReturnTest -Tests run: 12, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.001 sec -Running ca.mokhan.test.BanffMarathonRunnerTest -Tests run: 4, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.001 sec -Running ca.mokhan.test.AddressBookTest -Tests run: 12, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0 sec -Running ca.mokhan.test.TriangleTest -Tests run: 2, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0 sec -Running ca.mokhan.test.BonusOnSavingsTest -Tests run: 2, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0 sec -Running ca.mokhan.test.HailstoneSequenceTest -Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0 sec - -Results : - -Tests run: 52, Failures: 0, Errors: 0, Skipped: 0 - -[INFO] ------------------------------------------------------------------------ -[INFO] BUILD SUCCESS -[INFO] ------------------------------------------------------------------------ -[INFO] Total time: 2.871 s -[INFO] Finished at: 2019-05-13T20:07:42-06:00 -[INFO] ------------------------------------------------------------------------ -``` - - -4. Sample Input and Output: -[Provide some test cases with sample input and output of your program.] - -The test cases can be found in `BonusOnSavingsTest.java`. - -5. Discussion: - -I manually calculated the expected bonuses to make sure that I -understood the problem and how to calculate a result. - -Employee 1 - -Her quarterly savings are as follows: - -* Q1 – $5000 -* Q2 – $7000 -* Q3 – $4000 -* Q4 – $8000 - -Quarterly minimum: $6,000 -Annual minimum: $24,000 - -* Q1: Does not meet quarterly minimum. -* Q2: Exceeds quarterly minimum. $7000 * 3% = $210 -* Q3: Does not meet quarterly minimum -* Q4: Exceeds quarterly minimum. $8000 * 3% = $240. -* Y1: $24,000.00 meets the annual minimum. $24,000.00 * 5% = $1,200.00 - -Total bonus: 210 + 240 + 1200 = $1,650.00 - -Employee 2 - -* Q1 – $6000 -* Q2 – $9000 -* Q3 – $10000 -* Q4 – $17000 - -Quarterly min: $9000.00 -Annual min: $36,000.00 - -* Q1: does not meet quarterly min. -* Q2: $9,000 * 3% = $270.00 -* Q3: $10,000 * 3% = $300.00 -* Q4: $17,000 * 3% = $510.00 -* Y1: $42,000.00 saved. $42,000.00 * 5% = $2,100.00 - -42,000 - 36,000 = $6,000 * 25% = $1,500.00 - -Total: $270.00 + $300.00 + $510.00 + $2,100.00 + $1,500.00 -$4,680.00 - -Then I started by writing tests to reproduce the expected results. Once -I had a working test, I began to implment the code to make the test -pass. After I got the tests passing I started to refactor the code by -extracting methods. diff --git a/src/Q3/CartesianCoordinateSystem.java b/src/Q3/CartesianCoordinateSystem.java deleted file mode 100644 index abd7341..0000000 --- a/src/Q3/CartesianCoordinateSystem.java +++ /dev/null @@ -1,46 +0,0 @@ -/** - * Assignment 1, COMP268 Class: CartesianCoordinateSystem.java - * - * @description Represents an object that can calculate the distance between two points on a - * cartesian plain - * @author: mo khan Student ID: 3431709 - * @date May 7, 2019 - * @version 1.0 - */ -package Q3; - -import java.util.Scanner; - -public class CartesianCoordinateSystem { - /** - * Calculate the distance between two points. - * - * @param x1 x coordinate for the first point. - * @param y1 y coordinate for the first point. - * @param x2 x coordinate for the second point. - * @param y2 y coordinate for the second point. - * @return the distance between the two points - */ - public double calculateDistance(double x1, double y1, double x2, double y2) { - return Math.round(Math.pow(Math.pow(x2 - x1, 2) + Math.pow(y2 - y1, 2), 0.5) * 100) / 100.0; - } - - public static void main(String[] args) { - Scanner in = new Scanner(System.in); - - System.out.print("Please enter X1: "); - double x1 = in.nextDouble(); - - System.out.print("Please enter Y1 contributions: "); - double y1 = in.nextDouble(); - - System.out.print("Please enter X2 contributions: "); - double x2 = in.nextDouble(); - - System.out.print("Please enter Y2 contributions: "); - double y2 = in.nextDouble(); - - CartesianCoordinateSystem cartesian = new CartesianCoordinateSystem(); - System.out.println("The distance is " + cartesian.calculateDistance(x1, y1, x2, y2)); - } -} diff --git a/src/Q3/CartesianCoordinateSystemTest.java b/src/Q3/CartesianCoordinateSystemTest.java deleted file mode 100644 index 9906323..0000000 --- a/src/Q3/CartesianCoordinateSystemTest.java +++ /dev/null @@ -1,23 +0,0 @@ -package ca.mokhan.test; - -import Q3.*; -import junit.framework.Test; -import junit.framework.TestCase; -import junit.framework.TestSuite; - -public class CartesianCoordinateSystemTest extends TestCase { - private CartesianCoordinateSystem subject; - - public CartesianCoordinateSystemTest(String testName) { - super(testName); - this.subject = new CartesianCoordinateSystem(); - } - - public static Test suite() { - return new TestSuite(CartesianCoordinateSystemTest.class); - } - - public void testDistanceBetweenPoints() { - assertEquals(7.28, subject.calculateDistance(-2, -3, -4, 4)); - } -} diff --git a/src/Q3/README.md b/src/Q3/README.md deleted file mode 100644 index 3edb62a..0000000 --- a/src/Q3/README.md +++ /dev/null @@ -1,101 +0,0 @@ -Learning Profile for Assignment #1, And Question #3 - -Name: Mo Khan -Student ID: 3431709 - -1. Problem Statement: - -Write a program that prompts the user to enter two points `(x1, y1)` and `(x2, y2)`. -Calculate and display the distance between the two points using the formula below. -Round the answer up to 2 decimal points. -You can use `Math.pow(a,0.5)` to compute the square root of an expression. -`Math.pow()` returns a double. - -For example, the distance between the points (−2, −3) and (−4, 4) is approximately 7.28, as shown below. - -2. Description of the Code: - -[Briefly describe how you solved the problem in your code. You should include short description of classes, methods, and variables (if necessary) that you used in your code.] - -I took the example described in the problem statement and converted it -to a junit test. Then I implemented the `calculateDistance` interface. - -3. Errors and Warnings: - -```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 48 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 24 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 24 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.007 sec -Running ca.mokhan.test.CandidateTest -Tests run: 4, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.046 sec -Running ca.mokhan.test.NumberTest -Tests run: 5, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.021 sec -Running ca.mokhan.test.EmployeeSavingsTest -Tests run: 6, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.007 sec -Running ca.mokhan.test.CartesianCoordinateSystemTest -Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0 sec -Running ca.mokhan.test.CommunicationTest -Tests run: 2, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.001 sec -Running ca.mokhan.test.TaxReturnTest -Tests run: 12, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.001 sec -Running ca.mokhan.test.BanffMarathonRunnerTest -Tests run: 4, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0 sec -Running ca.mokhan.test.AddressBookTest -Tests run: 12, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.001 sec -Running ca.mokhan.test.TriangleTest -Tests run: 2, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0 sec -Running ca.mokhan.test.BonusOnSavingsTest -Tests run: 2, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0 sec -Running ca.mokhan.test.HailstoneSequenceTest -Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0 sec - -Results : - -Tests run: 52, Failures: 0, Errors: 0, Skipped: 0 - -[INFO] ------------------------------------------------------------------------ -[INFO] BUILD SUCCESS -[INFO] ------------------------------------------------------------------------ -[INFO] Total time: 2.899 s -[INFO] Finished at: 2019-05-13T21:15:06-06:00 -[INFO] ------------------------------------------------------------------------ -``` - -4. Sample Input and Output: - -Tests are available in `CartesianCoordinateSystemTest.java`. - -5. Discussion: diff --git a/src/Q4/BanffMarathonRunner.java b/src/Q4/BanffMarathonRunner.java deleted file mode 100644 index d9f5617..0000000 --- a/src/Q4/BanffMarathonRunner.java +++ /dev/null @@ -1,148 +0,0 @@ -/** - * Assignment 1, COMP268 Class: BanffMarathonRunner.java - * - * @description Represents a marathon runner - * @author: mo khan Student ID: 3431709 - * @date May 8, 2019 - * @version 1.0 - */ -package Q4; - -import Q1.*; -import java.util.ArrayList; -import java.util.Arrays; - -public class BanffMarathonRunner extends AddressBook { - private int time; - private int years; - - /** - * Constructs a BanffMarathonRunner object. - * - * @param firstName the first name of the runner - * @param lastName the last name of the runner - * @param time the time it took the runner to complete the marathon - * @param years the number of years they participated - */ - public BanffMarathonRunner(String firstName, String lastName, int time, int years) { - super(firstName, "", lastName); - this.time = time; - this.years = years; - } - - /** - * Performs a comparison of this runner with another based on the time it took them to complete - * the marathon. Because java generics does not allow implementing a generic interface with - * different type parameters, this code assumes the AddressBook is an instance of a Runner. - * - * @param other the other runner to compare against - * @return a negative integer, zero, or a positive integer as the first argument is less than, - * equal to, or greater than the second. - */ - public int compareTo(AddressBook other) { - BanffMarathonRunner runner = (BanffMarathonRunner) other; - return Integer.compare(this.time, runner.time); - } - - /** - * Return the time take to complete the race. - * - * @return the time taken to complete the race - */ - public int getTime() { - return this.time; - } - - /** - * Returns a string representation of the runner. - * - * @return The first name + the # of years that they participated. - */ - @Override - public String toString() { - return super.getFirstName() + " " + this.years; - } - - /** - * Sorts the list of runners based on time, then returns the fastest runner. - * - * @param runners the list of runners. - * @return the fastest runner - */ - public static BanffMarathonRunner getFastestRunner(BanffMarathonRunner[] runners) { - Arrays.sort(runners); - return runners[0]; - } - - /** - * Sorts the list of runners based on time, then returns the second fastest runner. - * - * @param runners the list of runners. - * @return the second fastest runner - */ - public static BanffMarathonRunner getSecondFastestRunner(BanffMarathonRunner[] runners) { - Arrays.sort(runners); - return runners[1]; - } - - /** - * Calculates the average time that it took the runners to complete the marathon. - * - * @param runners the array of runners. - * @return the average time taken to complete the marathon - */ - public static int getAverageTime(BanffMarathonRunner[] runners) { - int sum = 0; - for (BanffMarathonRunner runner : runners) sum += runner.time; - return sum / runners.length; - } - - /** - * Returns the runners that finished the marathon in above or equal to average time. - * - * @param runners the list of runners - * @return the list of runners that finished the marathon in above average time. - */ - public static String getAboveAverageRunners(BanffMarathonRunner[] runners) { - int average = getAverageTime(runners); - ArrayList<String> winners = new ArrayList<String>(); - - for (BanffMarathonRunner runner : runners) - if (runner.time <= average) winners.add(runner.toString()); - - return String.join(System.lineSeparator(), winners); - } - - public static void main(String[] args) { - BanffMarathonRunner[] runners = { - new BanffMarathonRunner("Elena", "Brandon", 341, 1), - new BanffMarathonRunner("Thomas", "Molson", 273, 2), - new BanffMarathonRunner("Hamilton", "Winn", 278, 5), - new BanffMarathonRunner("Suzie", "Sarandin", 329, 7), - new BanffMarathonRunner("Philip", "Winne", 445, 9), - new BanffMarathonRunner("Alex", "Trebok", 275, 3), - new BanffMarathonRunner("Emma", "Pivoto", 275, 4), - new BanffMarathonRunner("John", "Lenthen", 243, 1), - new BanffMarathonRunner("James", "Lean", 334, 1), - new BanffMarathonRunner("Jane", "Ostin", 412, 1), - new BanffMarathonRunner("Emily", "Car", 393, 4), - new BanffMarathonRunner("Daniel", "Hamshire", 299, 4), - new BanffMarathonRunner("Neda", "Bazdar", 343, 3), - new BanffMarathonRunner("Aaron", "Smith", 317, 6), - new BanffMarathonRunner("Kate", "Hen", 265, 8) - }; - - BanffMarathonRunner fastestRunner = BanffMarathonRunner.getFastestRunner(runners); - System.out.println(fastestRunner.getFirstName()); - System.out.println(fastestRunner.getHomeAddress()); - System.out.println(fastestRunner.getTime()); - - BanffMarathonRunner secondFastestRunner = BanffMarathonRunner.getSecondFastestRunner(runners); - System.out.println(secondFastestRunner.getFirstName()); - System.out.println(secondFastestRunner.getHomeAddress()); - System.out.println(secondFastestRunner.getTime()); - System.out.println(secondFastestRunner.getTime() - fastestRunner.getTime()); - - System.out.print(BanffMarathonRunner.getAboveAverageRunners(runners)); - } -} diff --git a/src/Q4/BanffMarathonRunnerTest.java b/src/Q4/BanffMarathonRunnerTest.java deleted file mode 100644 index 5e41380..0000000 --- a/src/Q4/BanffMarathonRunnerTest.java +++ /dev/null @@ -1,63 +0,0 @@ -package ca.mokhan.test; - -import Q4.*; -import junit.framework.Test; -import junit.framework.TestCase; -import junit.framework.TestSuite; - -public class BanffMarathonRunnerTest extends TestCase { - private BanffMarathonRunner john = new BanffMarathonRunner("John", "Lenthen", 243, 1); - private BanffMarathonRunner kate = new BanffMarathonRunner("Kate", "Hen", 265, 8); - private BanffMarathonRunner[] runners = { - new BanffMarathonRunner("Elena", "Brandon", 341, 1), - new BanffMarathonRunner("Thomas", "Molson", 273, 2), - new BanffMarathonRunner("Hamilton", "Winn", 278, 5), - new BanffMarathonRunner("Suzie", "Sarandin", 329, 7), - new BanffMarathonRunner("Philip", "Winne", 445, 9), - new BanffMarathonRunner("Alex", "Trebok", 275, 3), - new BanffMarathonRunner("Emma", "Pivoto", 275, 4), - this.john, - new BanffMarathonRunner("James", "Lean", 334, 1), - new BanffMarathonRunner("Jane", "Ostin", 412, 1), - new BanffMarathonRunner("Emily", "Car", 393, 4), - new BanffMarathonRunner("Daniel", "Hamshire", 299, 4), - new BanffMarathonRunner("Neda", "Bazdar", 343, 3), - new BanffMarathonRunner("Aaron", "Smith", 317, 6), - this.kate - }; - - public BanffMarathonRunnerTest(String testName) { - super(testName); - } - - public static Test suite() { - return new TestSuite(BanffMarathonRunnerTest.class); - } - - public void testGetFastestRunner() { - assertEquals(this.john, BanffMarathonRunner.getFastestRunner(this.runners)); - } - - public void testGetSecondFastestRunner() { - assertEquals(this.kate, BanffMarathonRunner.getSecondFastestRunner(this.runners)); - } - - public void testGetAverageTime() { - assertEquals(321, BanffMarathonRunner.getAverageTime(this.runners)); - } - - public void testGetAboveAverageRunners() { - String expected = - String.join( - System.lineSeparator(), - "Thomas 2", - "Hamilton 5", - "Alex 3", - "Emma 4", - "John 1", - "Daniel 4", - "Aaron 6", - "Kate 8"); - assertEquals(expected, BanffMarathonRunner.getAboveAverageRunners(this.runners)); - } -} diff --git a/src/Q4/README.md b/src/Q4/README.md deleted file mode 100644 index 80a84eb..0000000 --- a/src/Q4/README.md +++ /dev/null @@ -1,140 +0,0 @@ -Learning Profile for Assignment #1, And Question #4 - -Name: Mo Khan -Student ID: 3431709 - -1. Problem Statement: - -A group of AU friends decide to run the Banff, Alberta, Marathon. -Their names, times (marathon completion time in minutes), and number of years participated are given below: - -| id | Name | time (mins) | years | -| --- | --- | --- | --- | -| 1 | Elena Brandon | 341 | 1 | -| 2 | Thomas Molson | 273 | 2 | -| 3 | Hamilton Winn | 278 | 5 | -| 4 | Suzie Sarandin | 329 | 7 | -| 5 | Philip Winne | 445 | 9 | -| 6 | Alex Trebok | 275 | 3 | -| 7 | Emma Pivoto | 275 | 4 | -| 8 | John Lenthen | 243 | 1 | -| 9 | James Lean | 334 | 1 | -| 10 | Jane Ostin | 412 | 1 | -| 11 | Emily Car | 393 | 4 | -| 12 | Daniel Hamshire | 299 | 4 | -| 13 | Neda Bazdar | 343 | 3 | -| 14 | Aaron Smith | 317 | 6 | -| 15 | Kate Hen | 265 | 8| - -Extend the AddressBook class from Problem 1 to store the additional data. -Now, write a method to find the fastest runner. -Print the name, address, and his/her time (in minutes) on three separate lines. -Find the second fastest runner. -Print the name, address, his/her time (in minutes), and the difference in time with the fastest runner. -Compute the average time of completion taken by these runners. -Finally, print the name and number of years participated for each runner if the runner’s time of completion is equal to or better than the average time of completion. - -2. Description of the Code: - -[Briefly describe how you solved the problem in your code. You should include short description of classes, methods, and variables (if necessary) that you used in your code.] - -I followed the instructions in the assignment and created a class the -inherits from `AddressBook`. The sub class has a single constructor that -chains to the super class constructor. - -I did an override of the `Comparable<AddressBook>` interface to compare -using the runners time instead of the default comparison from the base -class. - -Java's implementation of generics doesn't allow a class to -implement the same generic interface using a different type parameter. -So I had to downcast the `AddressBook` to a `BanffMarathonRunner`. The -`compareTo` implementation in the subclass always assums that it is -being compared to another `BanffMarathonRunner`. - -Sorting arrays becomes very using after implementing the `Comparable<T>` -interface. - -3. Errors and Warnings: - -```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 46 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 24 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 24 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.007 sec -Running ca.mokhan.test.CandidateTest -Tests run: 4, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.05 sec -Running ca.mokhan.test.NumberTest -Tests run: 5, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.024 sec -Running ca.mokhan.test.EmployeeSavingsTest -Tests run: 6, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.007 sec -Running ca.mokhan.test.CartesianCoordinateSystemTest -Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0 sec -Running ca.mokhan.test.CommunicationTest -Tests run: 2, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0 sec -Running ca.mokhan.test.TaxReturnTest -Tests run: 12, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.001 sec -Running ca.mokhan.test.BanffMarathonRunnerTest -Tests run: 4, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.001 sec -Running ca.mokhan.test.AddressBookTest -Tests run: 12, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.001 sec -Running ca.mokhan.test.TriangleTest -Tests run: 2, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0 sec -Running ca.mokhan.test.BonusOnSavingsTest -Tests run: 2, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0 sec -Running ca.mokhan.test.HailstoneSequenceTest -Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.001 sec - -Results : - -Tests run: 52, Failures: 0, Errors: 0, Skipped: 0 - -[INFO] ------------------------------------------------------------------------ -[INFO] BUILD SUCCESS -[INFO] ------------------------------------------------------------------------ -[INFO] Total time: 2.893 s -[INFO] Finished at: 2019-05-13T21:20:12-06:00 -[INFO] ------------------------------------------------------------------------ -``` - - -4. Sample Input and Output: - -Tests are available in `BanffMarathonRunnerTest.java`. - -5. Discussion: - -The implementation of `getAboveAverageRunners` assumes that `better` -is less than or equal to the average time taken by all runners. diff --git a/src/Q5/EmployeeSavings.java b/src/Q5/EmployeeSavings.java deleted file mode 100644 index e4b067d..0000000 --- a/src/Q5/EmployeeSavings.java +++ /dev/null @@ -1,184 +0,0 @@ -/** - * Assignment 1, COMP268 Class: EmployeeSavings.java - * - * @description Represents an employee savings account. - * @author: mo khan Student ID: 3431709 - * @date May 8, 2019 - * @version 1.0 - */ -package Q5; - -import Q1.*; -import java.util.ArrayList; -import java.util.Currency; -import java.util.Locale; -import java.util.Random; - -public class EmployeeSavings extends AddressBook { - private double accountValue; - private double[] monthlyInterests; - private double[] monthlySavings; - private double monthlyContribution; - public static final double ANNUAL_INTEREST_RATE = 0.05; - - /** - * Constructs an EmployeeSavings object with a randomly generated monthly contribution. - * - * @param firstName the first name of the runner - * @param lastName the last name of the runner - */ - public EmployeeSavings(String firstName, String lastName) { - this(firstName, lastName, new Random().nextInt(800 - 100) + 100.0); - } - - /** - * Constructs an EmployeeSavings object - * - * @param firstName the first name of the runner - * @param lastName the last name of the runner - * @param monthlyContribution the monthly contribution to the savings account - */ - public EmployeeSavings(String firstName, String lastName, double monthlyContribution) { - super(firstName, "", lastName); - this.monthlyContribution = monthlyContribution; - this.accountValue = predictBalanceAfterMonths(12); - } - - /** - * Constructs an EmployeeSavings object - * - * @param firstName the first name of the runner - * @param lastName the last name of the runner - * @param d1 unknown - * @param d2 unknown - */ - public EmployeeSavings(String firstName, String lastName, double[] d1, double[] d2) { - super(firstName, "", lastName); - } - - /** - * Return the predicted value of the account after 12 months. - * - * @return the predicted account value after 12 months - */ - public double getAccountValue() { - return this.accountValue; - } - - /** - * Return an array of the predicted interest earned for each month in the upcoming year. - * - * @return the predicted interest earned for each month - */ - public double[] getMonthlyInterests() { - return this.monthlyInterests; - } - - /** - * Return an array of the predicted monthly savings earned for each month in the upcoming year. - * - * @return the predicted monthly savings earned for each month - */ - public double[] getMonthlySavings() { - return this.monthlySavings; - } - - /** - * Return an array of the predicted interest earned for each month in the upcoming year. - * - * @return the predicted interest earned for each month - */ - public double[] calculateInterests() { - this.monthlyInterests = new double[12]; - for (int i = 1; i <= 12; i++) this.monthlyInterests[i - 1] = predictInterestAfterMonths(i); - return this.monthlyInterests; - } - - /** - * Return an array of the predicted monthly savings earned for each month in the upcoming year. - * - * @return the predicted monthly savings earned for each month - */ - public double[] generateMonthlySavings() { - this.monthlySavings = new double[12]; - for (int i = 1; i <= 12; i++) this.monthlySavings[i - 1] = predictBalanceAfterMonths(i); - return this.monthlySavings; - } - - /** - * Predicts the balance of an accounts after n months. - * - * @param months The # of months from now to predict a balance for. - * @return the predicted balance - */ - public double predictBalanceAfterMonths(int months) { - double monthlyRate = ANNUAL_INTEREST_RATE / 12.0; - double balance = 0; - - for (int i = 0; i < months; i++) - balance = (balance + this.monthlyContribution) * (1 + monthlyRate); - - return Math.round(balance * 1000) / 1000.0; - } - - /** - * Predicts the interest of an accounts after n months. - * - * @param months The # of months from now to predict the interest for. - * @return the predicted interest - */ - public double predictInterestAfterMonths(int months) { - double totalBalance = predictBalanceAfterMonths(months); - double totalContributions = (months * this.monthlyContribution); - - return Math.round((totalBalance - totalContributions) * 1000) / 1000.0; - } - - /** - * Generates a report that can be printed to the console. - * - * @param accounts the list of accounts to print a statement for - * @return a string that represents a report for the accounts given. - */ - public static String getReport(EmployeeSavings[] accounts) { - ArrayList<String> statement = new ArrayList<String>(); - - for (EmployeeSavings account : accounts) statement.add(account.toString()); - - return String.join(System.lineSeparator(), statement); - } - - /** - * Returns The first name, and predicted account balance after 12 months. - * - * @return a string that represents a summary for an account. - */ - @Override - public String toString() { - Currency currency = Currency.getInstance(Locale.getDefault()); - return String.format( - "%s %s%.2f", super.getFirstName(), currency.getSymbol(), this.getAccountValue()); - } - - public static void main(String[] args) { - EmployeeSavings[] accounts = { - new EmployeeSavings("Elena", "Brandon"), - new EmployeeSavings("Thomas", "Molson"), - new EmployeeSavings("Hamilton", "Winn"), - new EmployeeSavings("Suzie", "Sarandin"), - new EmployeeSavings("Philip", "Winne"), - new EmployeeSavings("Alex", "Trebok"), - new EmployeeSavings("Emma", "Pivoto"), - new EmployeeSavings("John", "Lenthen"), - new EmployeeSavings("James", "Lean"), - new EmployeeSavings("Jane", "Ostin"), - new EmployeeSavings("Emily", "Car"), - new EmployeeSavings("Daniel", "Hamshire"), - new EmployeeSavings("Neda", "Bazdar"), - new EmployeeSavings("Aaron", "Smith"), - new EmployeeSavings("Kate", "Hen") - }; - - System.out.print(EmployeeSavings.getReport(accounts)); - } -} diff --git a/src/Q5/EmployeeSavingsTest.java b/src/Q5/EmployeeSavingsTest.java deleted file mode 100644 index a85ab32..0000000 --- a/src/Q5/EmployeeSavingsTest.java +++ /dev/null @@ -1,84 +0,0 @@ -package ca.mokhan.test; - -import Q5.*; -import junit.framework.Test; -import junit.framework.TestCase; -import junit.framework.TestSuite; - -public class EmployeeSavingsTest extends TestCase { - private EmployeeSavings subject; - - public EmployeeSavingsTest(String testName) { - super(testName); - double monthlyContribution = 100.00; - this.subject = new EmployeeSavings("Tsuyoshi", "Garrett", monthlyContribution); - } - - public static Test suite() { - return new TestSuite(EmployeeSavingsTest.class); - } - - public void testPredictSavingsAfter1Month() { - // Bankers rounding rules would round this amount down to $100.41 - // the $0.007 would go to the bank. - assertEquals(100.417, subject.predictBalanceAfterMonths(1)); - assertEquals(201.252, subject.predictBalanceAfterMonths(2)); - assertEquals(302.507, subject.predictBalanceAfterMonths(3)); - } - - public void testPredictInterestAfter1Month() { - // Bankers rounding rules would round this amount down to $100.41 - // the $0.007 would go to the bank. - assertEquals(0.417, subject.predictInterestAfterMonths(1)); - assertEquals(1.252, subject.predictInterestAfterMonths(2)); - assertEquals(2.507, subject.predictInterestAfterMonths(3)); - } - - public void testGetAccountValue() { - assertEquals(subject.predictBalanceAfterMonths(12), subject.getAccountValue()); - } - - public void testCalculateInterests() { - assertEquals(0.417, subject.calculateInterests()[0]); - assertEquals(1.252, subject.calculateInterests()[1]); - assertEquals(2.507, subject.calculateInterests()[2]); - assertEquals(4.184, subject.calculateInterests()[3]); - assertEquals(6.285, subject.calculateInterests()[4]); - assertEquals(8.811, subject.calculateInterests()[5]); - assertEquals(11.764, subject.calculateInterests()[6]); - assertEquals(15.147, subject.calculateInterests()[7]); - assertEquals(18.96, subject.calculateInterests()[8]); - assertEquals(23.206, subject.calculateInterests()[9]); - assertEquals(27.886, subject.calculateInterests()[10]); - assertEquals(33.002, subject.calculateInterests()[11]); - } - - public void testGenerateMonthlySavings() { - assertEquals(100.417, subject.generateMonthlySavings()[0]); - assertEquals(201.252, subject.generateMonthlySavings()[1]); - assertEquals(302.507, subject.generateMonthlySavings()[2]); - } - - public void testGetReport() { - EmployeeSavings[] accounts = - new EmployeeSavings[] { - new EmployeeSavings("Elena", "Brandon"), - new EmployeeSavings("Thomas", "Molson"), - new EmployeeSavings("Hamilton", "Winn"), - new EmployeeSavings("Suzie", "Sarandin"), - new EmployeeSavings("Philip", "Winne"), - new EmployeeSavings("Alex", "Trebok"), - new EmployeeSavings("Emma", "Pivoto"), - new EmployeeSavings("John", "Lenthen"), - new EmployeeSavings("James", "Lean"), - new EmployeeSavings("Jane", "Ostin"), - new EmployeeSavings("Emily", "Car"), - new EmployeeSavings("Daniel", "Hamshire"), - new EmployeeSavings("Neda", "Bazdar"), - new EmployeeSavings("Aaron", "Smith"), - new EmployeeSavings("Kate", "Hen") - }; - String report = EmployeeSavings.getReport(accounts); - for (EmployeeSavings account : accounts) assertTrue(report.contains(account.getFirstName())); - } -} diff --git a/src/Q5/README.md b/src/Q5/README.md deleted file mode 100644 index c46f8da..0000000 --- a/src/Q5/README.md +++ /dev/null @@ -1,119 +0,0 @@ -Learning Profile for Assignment #1, And Question #5 - -Name: Mo Khan -Student ID: 3431709 - -1. Problem Statement: - -Solve the following problem using a program: -Suppose you save $100 each month into a savings account with an annual interest rate of 5%. -Thus, the monthly interest rate is 0.05/12 = 0.00417. -After the first month, the value in the account becomes 100 * (1 + 0.00417) = 100.417 -After the second month, the value in the account becomes (100 + 100.417) * (1 + 0.00417) = 201.252 -And after the third month, the value in the account becomes (100 + 201.252) * (1 + 0.00417) = 302.507 -... and so on. - -Write a program that randomly generates monthly savings amounts for the 15 runners in Problem 4. -Each monthly saving should be in the range of $100 to $800. -Extend the AddressBook class to store the monthly savings generated by the random number generator. -Then, display the final account value for each of the 15 runners. - - -2. Description of the Code: - -I created a constructor overload that accepts the `monthlyContribution` -as a parameter. This made it easier to write unit tests rather than -depend on random monthlyContributions. The constructor that accepts a -`firstName` and `lastName` will generate a random `monthlyContribution` -and chain to the overloaded constructor. - -I had no idea what `d1` and `d2` means, so I didn't use those -parameters. - -I designed this class so that you could predict the future value for `n` -months from now. I did assign the `accountValue` to a prediction of 12 -months from now. I would have preferred to implement an interface that -didn't require private instance variables and rather depend on -calculations on the fly but I did my best to bride the requirements with -my own preferences. - -3. Errors and Warnings: - -```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 47 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 24 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 24 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.006 sec -Running ca.mokhan.test.CandidateTest -Tests run: 4, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.053 sec -Running ca.mokhan.test.NumberTest -Tests run: 5, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.021 sec -Running ca.mokhan.test.EmployeeSavingsTest -Tests run: 6, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.007 sec -Running ca.mokhan.test.CartesianCoordinateSystemTest -Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0 sec -Running ca.mokhan.test.CommunicationTest -Tests run: 2, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0 sec -Running ca.mokhan.test.TaxReturnTest -Tests run: 12, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0 sec -Running ca.mokhan.test.BanffMarathonRunnerTest -Tests run: 4, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.001 sec -Running ca.mokhan.test.AddressBookTest -Tests run: 12, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.002 sec -Running ca.mokhan.test.TriangleTest -Tests run: 2, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0 sec -Running ca.mokhan.test.BonusOnSavingsTest -Tests run: 2, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.001 sec -Running ca.mokhan.test.HailstoneSequenceTest -Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0 sec - -Results : - -Tests run: 52, Failures: 0, Errors: 0, Skipped: 0 - -[INFO] ------------------------------------------------------------------------ -[INFO] BUILD SUCCESS -[INFO] ------------------------------------------------------------------------ -[INFO] Total time: 3.620 s -[INFO] Finished at: 2019-05-13T21:26:40-06:00 -[INFO] ------------------------------------------------------------------------ -``` - -4. Sample Input and Output: -[Provide some test cases with sample input and output of your program.] - -Tests are available in `EmployeeSavingsTest.java`. - -5. Discussion: diff --git a/src/Q6/README.md b/src/Q6/README.md deleted file mode 100644 index 594f920..0000000 --- a/src/Q6/README.md +++ /dev/null @@ -1,124 +0,0 @@ -Learning Profile for Assignment #1, And Question #6 - -Name: Mo Khan -Student ID: 3431709 - -1. Problem Statement: - -"The Pythagorean Theorem relates the lengths of the three sides of any right triangle. -The legs of a right triangle (the two sides of the triangle that meet at the right angle) -are customarily labelled as having lengths "a" and "b", and the hypotenuse -(the long side of the triangle, opposite the right angle) is labelled as having length "c". -The lengths are related by the following equation: a^2 + b^2 = c^2." -– http://www.purplemath.com/modules/pythagthm.htm - -This equation allows you to find the length of a side of a right triangle when they’ve given you the lengths for the other two sides, -and, going in the other direction, allows you to determine if a triangle is a right triangle when they’ve given you the lengths for all three sides. -This equation can alternatively be written as c = sqrt of (a^2+b^2). -You can find the square root of a number by calling the standard function Math.sqrt. - -For example, the statement `double y = Math.sqrt(x)` sets `y` to the square root of `x`. - -I. Given the right triangles described below, write a program to compute the lengths of the remaining sides using a program. -a. a=48, c=80 b = 64 -b. a=84, c=91 b = 35 - -```text - a^2 + b^2 = c^2 - b^2 = c^2 - a^2 - b = sqrt(c^2 - a^2) -``` - -II. Determine if the following triangles are right-angled triangles: -a. a=45, b=55, c=75 -b. a=28, b=45, c=53 - -2. Description of the Code: - -The `Triangle` class accepts the length of size `A`, `B`, and `C` in the -constructor. I used the constant `Triangle.NULL` to represent a null -value, to indicate which side needed to be calculated. - -I assumed only side `B` needed to be calculated based on the problem -description but could have also checked side `A` or `C`. - -3. Errors and Warnings: - -```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 47 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 24 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 24 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.006 sec -Running ca.mokhan.test.CandidateTest -Tests run: 4, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.053 sec -Running ca.mokhan.test.NumberTest -Tests run: 5, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.021 sec -Running ca.mokhan.test.EmployeeSavingsTest -Tests run: 6, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.007 sec -Running ca.mokhan.test.CartesianCoordinateSystemTest -Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0 sec -Running ca.mokhan.test.CommunicationTest -Tests run: 2, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0 sec -Running ca.mokhan.test.TaxReturnTest -Tests run: 12, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0 sec -Running ca.mokhan.test.BanffMarathonRunnerTest -Tests run: 4, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.001 sec -Running ca.mokhan.test.AddressBookTest -Tests run: 12, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.002 sec -Running ca.mokhan.test.TriangleTest -Tests run: 2, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0 sec -Running ca.mokhan.test.BonusOnSavingsTest -Tests run: 2, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.001 sec -Running ca.mokhan.test.HailstoneSequenceTest -Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0 sec - -Results : - -Tests run: 52, Failures: 0, Errors: 0, Skipped: 0 - -[INFO] ------------------------------------------------------------------------ -[INFO] BUILD SUCCESS -[INFO] ------------------------------------------------------------------------ -[INFO] Total time: 3.620 s -[INFO] Finished at: 2019-05-13T21:26:40-06:00 -[INFO] ------------------------------------------------------------------------ -``` - -4. Sample Input and Output: -[Provide some test cases with sample input and output of your program.] - -Tests are available in `TriangleTest.java`. - -5. Discussion: diff --git a/src/Q6/Triangle.java b/src/Q6/Triangle.java deleted file mode 100644 index 3e641ce..0000000 --- a/src/Q6/Triangle.java +++ /dev/null @@ -1,66 +0,0 @@ -/** - * Assignment 1, COMP268 Class: Triangle.java - * - * @description Represents a Triangle - * @author: mo khan Student ID: 3431709 - * @date May 8, 2019 - * @version 1.0 - */ -package Q6; - -public class Triangle { - public static double NULL = 0.0; - private double a, b, c; - - /** - * Constructs a Triangle - * - * @param a the length of side A - * @param b the length of side B - * @param c the length of side C - */ - public Triangle(double a, double b, double c) { - this.a = a; - this.b = b; - this.c = c; - } - - /** @return the length of side A */ - public double getA() { - return this.a; - } - - /** @return the length of side B */ - public double getB() { - if (this.b == NULL) this.b = Math.sqrt(Math.pow(this.getC(), 2) - Math.pow(this.getA(), 2)); - return this.b; - } - - /** @return the length of side C */ - public double getC() { - return this.c; - } - - /** - * Determines if the triangle is a right angle triangle. - * - * @return boolean to indicate if the triangle is a right angle triangle - */ - public boolean isRightTriangle() { - return Math.pow(this.getA(), 2) + Math.pow(this.getB(), 2) == Math.pow(this.getC(), 2); - } - - /** @return a string with the length of each side */ - @Override - public String toString() { - return String.format("A: %f, B: %f, C: %f", this.getA(), this.getB(), this.getC()); - } - - public static void main(String[] args) { - System.out.println(new Triangle(48, Triangle.NULL, 80).toString()); - System.out.println(new Triangle(84, Triangle.NULL, 91).toString()); - - System.out.println(new Triangle(45, 55, 75).isRightTriangle()); - System.out.println(new Triangle(28, 45, 53).isRightTriangle()); - } -} diff --git a/src/Q6/TriangleTest.java b/src/Q6/TriangleTest.java deleted file mode 100644 index 8f4e035..0000000 --- a/src/Q6/TriangleTest.java +++ /dev/null @@ -1,26 +0,0 @@ -package ca.mokhan.test; - -import Q6.*; -import junit.framework.Test; -import junit.framework.TestCase; -import junit.framework.TestSuite; - -public class TriangleTest extends TestCase { - public TriangleTest(String testName) { - super(testName); - } - - public static Test suite() { - return new TestSuite(TriangleTest.class); - } - - public void testIsRightTriangle() { - assertFalse(new Triangle(45.0, 55.0, 75.0).isRightTriangle()); - assertTrue(new Triangle(28.0, 45.0, 53.0).isRightTriangle()); - } - - public void testGetB() { - assertEquals(64.0, new Triangle(48.0, Triangle.NULL, 80.0).getB()); - assertEquals(35.0, new Triangle(84.0, Triangle.NULL, 91.0).getB()); - } -} diff --git a/src/Q7/HailstoneSequence.java b/src/Q7/HailstoneSequence.java deleted file mode 100644 index a217a49..0000000 --- a/src/Q7/HailstoneSequence.java +++ /dev/null @@ -1,50 +0,0 @@ -/** - * Assignment 1, COMP268 Class: HailstoneSequence.java - * - * @description Represents a hailstone sequence - * @author: mo khan Student ID: 3431709 - * @date May 8, 2019 - * @version 1.0 - */ -package Q7; - -import java.util.ArrayList; -import java.util.Scanner; - -public class HailstoneSequence { - /** - * Returns a hailstone sequence using the seed provided. - * - * @param n the seed value for the hailstone sequence - * @return a list of integers that represents the hailstone sequence. - */ - public static ArrayList<Integer> getHailstoneSequence(int n) { - return getHailstoneSequence(n, new ArrayList<Integer>()); - } - - /** - * Appends to the hailstone sequence starting from the seed value provided. - * - * @param n the seed value for the hailstone sequence - * @param items the list of items to append the next set of hailstone sequence to. - * @return a list of integers that represents the hailstone sequence. - */ - public static ArrayList<Integer> getHailstoneSequence(int n, ArrayList<Integer> items) { - items.add(n); - - if (n == 1) return items; - else if (n % 2 == 0) return getHailstoneSequence(n / 2, items); - else return getHailstoneSequence((n * 3) + 1, items); - } - - public static void main(String[] args) { - Scanner in = new Scanner(System.in); - - System.out.print("Please enter seed for hailstone sequence: "); - ArrayList<Integer> sequence = HailstoneSequence.getHailstoneSequence(in.nextInt()); - for (Integer i : sequence) System.out.println(i); - - for (Integer i : sequence) System.out.print("-"); - System.out.println(); - } -} diff --git a/src/Q7/HailstoneSequenceTest.java b/src/Q7/HailstoneSequenceTest.java deleted file mode 100644 index 8b35662..0000000 --- a/src/Q7/HailstoneSequenceTest.java +++ /dev/null @@ -1,40 +0,0 @@ -package ca.mokhan.test; - -import Q7.*; -import java.util.ArrayList; -import junit.framework.Test; -import junit.framework.TestCase; -import junit.framework.TestSuite; - -public class HailstoneSequenceTest extends TestCase { - public HailstoneSequenceTest(String testName) { - super(testName); - } - - public static Test suite() { - return new TestSuite(HailstoneSequenceTest.class); - } - - public void testGetHailstoneSequence() { - ArrayList<Integer> results = HailstoneSequence.getHailstoneSequence(15); - - assertTrue(15 == results.get(0)); - assertTrue(46 == results.get(1)); - assertTrue(23 == results.get(2)); - assertTrue(70 == results.get(3)); - assertTrue(35 == results.get(4)); - assertTrue(106 == results.get(5)); - assertTrue(53 == results.get(6)); - assertTrue(160 == results.get(7)); - assertTrue(80 == results.get(8)); - assertTrue(40 == results.get(9)); - assertTrue(20 == results.get(10)); - assertTrue(10 == results.get(11)); - assertTrue(5 == results.get(12)); - assertTrue(16 == results.get(13)); - assertTrue(8 == results.get(14)); - assertTrue(4 == results.get(15)); - assertTrue(2 == results.get(16)); - assertTrue(1 == results.get(17)); - } -} diff --git a/src/Q7/README.md b/src/Q7/README.md deleted file mode 100644 index df7e40c..0000000 --- a/src/Q7/README.md +++ /dev/null @@ -1,135 +0,0 @@ -Learning Profile for Assignment #1, And Question #7 - -Name: Mo Khan -Student ID: 3431709 - -1. Problem Statement: - -Douglas Hofstadter’s Pulitzer-prize-winning book Gödel, Escher, Bach contains many interesting mathematical puzzles. - -In Chapter XII, Hofstadter mentions a wonderful problem that is well within the scope of the control statements in Java. - -The problem can be expressed as follows: - -* Pick some positive integer and call it n. -* If n is even, divide it by two. -* If n is odd, multiply it by three and add one. -Continue this process until n is equal to 1. - -Hofstadter illustrates this process with the following example, -starting with the number n = 15: -15 is odd, so I make 3n+1: 46 -46 is even, so I take half: 23 -23 is odd, so I make 3n+1: 70 -70 is even, so I take half: 35 -35 is odd, so I make 3n+1: 106 -106 is even, so I take half: 53 -53 is odd, so I make 3n+1: 160 -160 is even, so I take half: 80 -80 is even, so I take half: 40 -40 is even, so I take half: 20 -20 is even, so I take half: 10 -10 is even, so I take half: 5 -5 is odd, so I make 3n+1: 16 -16 is even, so I take half: 8 -8 is even, so I take half: 4 -4 is even, so I take half: 2 -2 is even, so I take half: 1 - -As you can see from this example, the numbers go up and down, but eventually—at least for all numbers that have ever been tried—come down to end in 1. -In some respects, this process is reminiscent of the formation of hailstones, -which get carried upward by the winds over and over again before they finally descend to the ground. -Because of this analogy, this sequence of numbers is usually called the Hailstone sequence, -although it goes by many other names as well. - -Write a program that reads in a number from the user and then displays the Hailstone sequence for that number, -followed by a line showing the number of steps taken to reach 1. - -2. Description of the Code: - -My first implementation of this algorithm used an iterative approach. I -later changed it to use a recursive approach because I felt the code was -a little bit easier to read and blowing the stack or performance wasn't -a big concern for this exercise. - -The base case for the recursion is `n == 1`. - -3. Errors and Warnings: - -```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 48 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 24 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 24 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.006 sec -Running ca.mokhan.test.CandidateTest -Tests run: 4, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.05 sec -Running ca.mokhan.test.NumberTest -Tests run: 5, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.02 sec -Running ca.mokhan.test.EmployeeSavingsTest -Tests run: 6, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.006 sec -Running ca.mokhan.test.CartesianCoordinateSystemTest -Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0 sec -Running ca.mokhan.test.CommunicationTest -Tests run: 2, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.001 sec -Running ca.mokhan.test.TaxReturnTest -Tests run: 12, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.001 sec -Running ca.mokhan.test.BanffMarathonRunnerTest -Tests run: 4, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.001 sec -Running ca.mokhan.test.AddressBookTest -Tests run: 12, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0 sec -Running ca.mokhan.test.TriangleTest -Tests run: 2, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0 sec -Running ca.mokhan.test.BonusOnSavingsTest -Tests run: 2, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0 sec -Running ca.mokhan.test.HailstoneSequenceTest -Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.001 sec - -Results : - -Tests run: 52, Failures: 0, Errors: 0, Skipped: 0 - -[INFO] ------------------------------------------------------------------------ -[INFO] BUILD SUCCESS -[INFO] ------------------------------------------------------------------------ -[INFO] Total time: 2.912 s -[INFO] Finished at: 2019-05-13T21:33:23-06:00 -[INFO] ------------------------------------------------------------------------ -``` - -4. Sample Input and Output: - -Tests can be found in `HailstoneSequenceTest.java`. - -5. Discussion: diff --git a/src/Q8/Candidate.java b/src/Q8/Candidate.java deleted file mode 100644 index 2024267..0000000 --- a/src/Q8/Candidate.java +++ /dev/null @@ -1,147 +0,0 @@ -/** - * Assignment 1, COMP268 Class: Candidate.java - * - * @description Represents a candidate - * @author: mo khan Student ID: 3431709 - * @date May 8, 2019 - * @version 1.0 - */ -package Q8; - -import Q1.*; -import java.util.ArrayList; -import java.util.Scanner; - -public class Candidate extends AddressBook { - private double grade = 0.0; - private Communication communication; - private boolean isInnovative; - private double regulatoryCapability; - - /** - * Constructs a Candidate object. - * - * @param firstName the first name of the runner - * @param lastName the last name of the runner - * @param grade the grade for the candidate - * @param communication the level of communication - * @param isInnovative specifies if the candidate is innovative. - * @param regulatoryCapability specifies the candidates level of regulatory capability - */ - public Candidate( - String firstName, - String lastName, - double grade, - String communication, - boolean isInnovative, - double regulatoryCapability) { - super(firstName, "", lastName); - this.grade = grade; - this.setCommunication(communication); - this.isInnovative = isInnovative; - this.regulatoryCapability = regulatoryCapability; - } - - /** @return the candidates innovativeness. */ - public boolean isInnovative() { - return this.isInnovative; - } - - /** @return the candidates grade. */ - public double getGrade() { - return this.grade; - } - - /** @return the candidates regulatory capability */ - public double getRegulation() { - return this.regulatoryCapability; - } - - /** @return the candidates communication ability */ - public String getCommunication() { - return this.communication.toString(); - } - - /** - * Sets the candidate communication level. - * - * @param communication the candidates communication ability - */ - public void setCommunication(String communication) { - this.communication = Communication.findBy(communication); - } - - /** - * Sets the candidate grade level. - * - * @param grade the candidates grade - */ - public void setGrade(double grade) { - this.grade = grade; - } - - /** - * Sets the candidate innovation level. - * - * @param innovation the candidates innovation ability - */ - public void setInnovation(boolean innovation) { - this.isInnovative = innovation; - } - - /** - * Sets the candidate regulatory capability level. - * - * @param regulatoryCapability the candidates regulatory capability - */ - public void setRegulation(double regulatoryCapability) { - this.regulatoryCapability = regulatoryCapability; - } - - /** @return true if the candidate is eligible for a position at the company. */ - public boolean isEligible() { - if (this.grade >= 85.0) { - return this.communication.isAtLeast(Communication.Average) || this.isInnovative(); - } else { - return (this.regulatoryCapability >= 0.5 - && this.communication.isAtLeast(Communication.Average)); - } - } - - /** - * @param candidates the list of candidates to check for qualifications - * @return a list of candidates that are eligible for employment at the company. - */ - public static ArrayList<Candidate> getEligibleCandidates(Candidate[] candidates) { - ArrayList<Candidate> eligible = new ArrayList<Candidate>(); - - for (Candidate candidate : candidates) if (candidate.isEligible()) eligible.add(candidate); - - return eligible; - } - - public static void main(String[] args) { - Scanner in = new Scanner(System.in); - - Candidate[] candidates = { - new Candidate("Elena", "Brandon", 82.30, "poor", true, 0.5), - new Candidate("Thomas", "Molson", 85.10, "poor", false, 1.0), - new Candidate("Hamilton", "Winn", 77.77, "average", false, 0.8), - new Candidate("Suzie", "Sarandin", 69.93, "average", false, 0.0), - new Candidate("Philip", "Winne", 93.03, "average", true, 1.0), - new Candidate("Alex", "Trebok", 88.61, "poor", true, 0.7), - new Candidate("Emma", "Pivoto", 55.99, "excellent", false, 0.8), - new Candidate("John", "Lenthen", 87.49, "excellent", true, 0.9), - new Candidate("James", "Lean", 88.00, "excellent", false, 0.5), - new Candidate("Jane", "Ostin", 91.20, "average", true, 0.6), - new Candidate("Emily", "Car", 66.79, "excellent", false, 0.3), - new Candidate("Daniel", "Hamshire", 76.65, "average", true, 0.2), - new Candidate("Neda", "Bazdar", 55.89, "excellent", true, 0.5), - new Candidate("Aaron", "Smith", 90.01, "excellent", false, 0.3), - new Candidate("Kate", "Hen", 87.9, "poor", false, 0.8) - }; - - for (Candidate candidate : Candidate.getEligibleCandidates(candidates)) - System.out.println(candidate); - } -} diff --git a/src/Q8/CandidateTest.java b/src/Q8/CandidateTest.java deleted file mode 100644 index 56513b8..0000000 --- a/src/Q8/CandidateTest.java +++ /dev/null @@ -1,63 +0,0 @@ -package ca.mokhan.test; - -import Q8.*; -import java.util.ArrayList; -import junit.framework.Test; -import junit.framework.TestCase; -import junit.framework.TestSuite; - -public class CandidateTest extends TestCase { - private Candidate[] candidates = { - new Candidate("Elena", "Brandon", 82.30, "poor", true, 0.5), - new Candidate("Thomas", "Molson", 85.10, "poor", false, 1.0), - new Candidate("Hamilton", "Winn", 77.77, "average", false, 0.8), - new Candidate("Suzie", "Sarandin", 69.93, "average", false, 0.0), - new Candidate("Philip", "Winne", 93.03, "average", true, 1.0), - new Candidate("Alex", "Trebok", 88.61, "poor", true, 0.7), - new Candidate("Emma", "Pivoto", 55.99, "excellent", false, 0.8), - new Candidate("John", "Lenthen", 87.49, "excellent", true, 0.9), - new Candidate("James", "Lean", 88.00, "excellent", false, 0.5), - new Candidate("Jane", "Ostin", 91.20, "average", true, 0.6), - new Candidate("Emily", "Car", 66.79, "excellent", false, 0.3), - new Candidate("Daniel", "Hamshire", 76.65, "average", true, 0.2), - new Candidate("Neda", "Bazdar", 55.89, "excellent", true, 0.5), - new Candidate("Aaron", "Smith", 90.01, "excellent", false, 0.3), - new Candidate("Kate", "Hen", 87.9, "poor", false, 0.8) - }; - - public CandidateTest(String testName) { - super(testName); - } - - public static Test suite() { - return new TestSuite(CandidateTest.class); - } - - public void testGetEligibleCandidates() { - ArrayList<Candidate> eligibleCandidates = Candidate.getEligibleCandidates(this.candidates); - for (String expected : - new String[] { - "Hamilton", "Philip", "Alex", "Emma", "John", "James", "Jane", "Neda", "Aaron" - }) { - assertNotNull( - eligibleCandidates.stream() - .filter(x -> expected.equals(x.getFirstName())) - .findAny() - .orElse(null)); - } - } - - public void testIsElligibleWithGreaterThanRequiredGrade() { - assertTrue(new Candidate("Tsuyoshi", "Garrett", 85.0, "excellent", false, 0.0).isEligible()); - } - - public void testIsElligibleWithLessThanRequiredGrade() { - assertFalse(new Candidate("Tsuyoshi", "Garrett", 84.9, "average", false, 0.0).isEligible()); - assertTrue(new Candidate("Tsuyoshi", "Garrett", 84.9, "average", false, 0.5).isEligible()); - } - - public void testIsElligibleWithPoorCommunication() { - assertTrue(new Candidate("Tsuyoshi", "Garrett", 85.0, "poor", true, 0.0).isEligible()); - assertFalse(new Candidate("Tsuyoshi", "Garrett", 85.0, "poor", false, 0.0).isEligible()); - } -} diff --git a/src/Q8/Communication.java b/src/Q8/Communication.java deleted file mode 100644 index cc217e3..0000000 --- a/src/Q8/Communication.java +++ /dev/null @@ -1,76 +0,0 @@ -/** - * Assignment 1, COMP268 Class: Communication.java - * - * @description Represents a candidates communication ability. - * @author: mo khan Student ID: 3431709 - * @date May 8, 2019 - * @version 1.0 - */ -package Q8; - -public class Communication implements Comparable<Communication> { - private String name; - private Integer ranking; - - /** - * Constructs a Communication object. - * - * @param name the name of the communication level - * @param ranking the ranking of the communication level. - */ - public Communication(String name, Integer ranking) { - this.name = name; - this.ranking = ranking; - } - - /** - * Compares one communication level with another. - * - * @param other the other communication level to compare against - * @return a negative integer, zero, or a positive integer as this object is less than, equal to, - * or greater than the specified object. - */ - public int compareTo(Communication other) { - return this.ranking.compareTo(other.ranking); - } - - /** - * Compares one communication level with another. - * - * @param other the other communication level to compare against - * @return true if this communication level is equal to or better than the other. - */ - public boolean isAtLeast(Communication other) { - return this.compareTo(other) >= 0; - } - - /** - * The string representation of the candidate. - * - * @return the name of the candidate - */ - @Override - public String toString() { - return this.name; - } - - public static final Communication Poor = new Communication("poor", 0); - public static final Communication Average = new Communication("average", 1); - public static final Communication Excellent = new Communication("excellent", 2); - - /** - * @param name of the communication level to find. - * @return the Communication level matching the name - */ - public static Communication findBy(String name) { - switch (name) { - case "poor": - return Communication.Poor; - case "average": - return Communication.Average; - case "excellent": - return Communication.Excellent; - } - throw new IllegalArgumentException("Unknown communication type"); - } -} diff --git a/src/Q8/CommunicationTest.java b/src/Q8/CommunicationTest.java deleted file mode 100644 index c2bdfa3..0000000 --- a/src/Q8/CommunicationTest.java +++ /dev/null @@ -1,44 +0,0 @@ -package ca.mokhan.test; - -import Q8.*; -import junit.framework.Test; -import junit.framework.TestCase; -import junit.framework.TestSuite; - -public class CommunicationTest extends TestCase { - public CommunicationTest(String testName) { - super(testName); - } - - public static Test suite() { - return new TestSuite(CommunicationTest.class); - } - - public void testCompareTo() { - assertEquals(-1, Communication.Poor.compareTo(Communication.Average)); - assertEquals(-1, Communication.Poor.compareTo(Communication.Excellent)); - assertEquals(0, Communication.Poor.compareTo(Communication.Poor)); - - assertEquals(-1, Communication.Average.compareTo(Communication.Excellent)); - assertEquals(1, Communication.Average.compareTo(Communication.Poor)); - assertEquals(0, Communication.Average.compareTo(Communication.Average)); - - assertEquals(1, Communication.Excellent.compareTo(Communication.Average)); - assertEquals(1, Communication.Excellent.compareTo(Communication.Poor)); - assertEquals(0, Communication.Excellent.compareTo(Communication.Excellent)); - } - - public void testIsAtLeast() { - assertFalse(Communication.Poor.isAtLeast(Communication.Average)); - assertFalse(Communication.Poor.isAtLeast(Communication.Excellent)); - assertTrue(Communication.Poor.isAtLeast(Communication.Poor)); - - assertFalse(Communication.Average.isAtLeast(Communication.Excellent)); - assertTrue(Communication.Average.isAtLeast(Communication.Poor)); - assertTrue(Communication.Average.isAtLeast(Communication.Average)); - - assertTrue(Communication.Excellent.isAtLeast(Communication.Average)); - assertTrue(Communication.Excellent.isAtLeast(Communication.Poor)); - assertTrue(Communication.Excellent.isAtLeast(Communication.Excellent)); - } -} diff --git a/src/Q8/README.md b/src/Q8/README.md deleted file mode 100644 index 706d676..0000000 --- a/src/Q8/README.md +++ /dev/null @@ -1,121 +0,0 @@ -Learning Profile for Assignment #1, And Question #8 - -Name: Mo Khan -Student ID: 3431709 - -1. Problem Statement: - -Google Inc. is looking to recruit three of the Boston runners. - -The criteria for selection are as follows: - -* I. Average final marks in bachelor’s degree (store up to 2 decimal places). - The fifteen candidates have the following grades: 82.30%, 85.10%, 77.77%, 69.93%, 93.03%, 88.61%, 55.99%, 87.49%, 88.00%, 91.20%, 66.79%, 76.65%, 55.89%, 90.01%, and 87.9%. -* II. Ability to communicate as one of the three values – "excellent", "average", and "poor". - The fifteen candidates have the following ability to communicate, respectively: poor, poor, average, average, average, poor, excellent, excellent, excellent, average, excellent, average, excellent, excellent, poor. -* III. Innovation as one of the two values – "brilliant" and "average" (store as a Boolean; brilliant = true and average = false). - The fifteen candidates have the following innovative abilities: brilliant, average, average, average, brilliant, brilliant, average, brilliant, average, brilliant, average, brilliant, brilliant, average, average. -* IV. Ability to regulate one’s own skill as a probability value between 0 and 1.0 – 1.0 implies excellent regulatory capabilities and 0.0 implies no skills to regulate (store as a double). - The fifteen candidates have the following regulatory abilities: 0.5, 1.0, 0.8, 0.0, 1.0, 0.7, 0.8, 0.9, 0.5, 0.6, 0.3, 0.2, 0.5, 0.3, 0.8. - -Store these values for the fifteen candidates in an extended AddressBook class. - -In general, Google will not consider a candidate with average marks of less than 85%. - -Google will consider a candidate with average marks of less than 85% only if the candidate at least has 0.5 regulatory abilities and at least 'average' ability to communicate. - -Google will only consider a candidate with poor communication ability if the candidate has a 'brilliant' innovation capability. - -Write a program that will help Google to programmatically determine eligibility of the fifteen candidates for these positions, and print the output on the console. - -2. Description of the Code: - -I created two classes to solve this problem. The first class is the -`Candidate` class and the second is the `Communication` class. I used -the `Communication` class to control sort order and precedence of one -communication level with another using the `Comparable<T>` interface. - -In the `Candidate` class I created a method called `isEligible` that -calculates if the candidate is eligible for a position at the company. - -3. Errors and Warnings: - -```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 47 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 24 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 24 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.007 sec -Running ca.mokhan.test.CandidateTest -Tests run: 4, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.057 sec -Running ca.mokhan.test.NumberTest -Tests run: 5, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.02 sec -Running ca.mokhan.test.EmployeeSavingsTest -Tests run: 6, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.005 sec -Running ca.mokhan.test.CartesianCoordinateSystemTest -Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0 sec -Running ca.mokhan.test.CommunicationTest -Tests run: 2, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0 sec -Running ca.mokhan.test.TaxReturnTest -Tests run: 12, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.001 sec -Running ca.mokhan.test.BanffMarathonRunnerTest -Tests run: 4, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.001 sec -Running ca.mokhan.test.AddressBookTest -Tests run: 12, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.001 sec -Running ca.mokhan.test.TriangleTest -Tests run: 2, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0 sec -Running ca.mokhan.test.BonusOnSavingsTest -Tests run: 2, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.001 sec -Running ca.mokhan.test.HailstoneSequenceTest -Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0 sec - -Results : - -Tests run: 52, Failures: 0, Errors: 0, Skipped: 0 - -[INFO] ------------------------------------------------------------------------ -[INFO] BUILD SUCCESS -[INFO] ------------------------------------------------------------------------ -[INFO] Total time: 2.907 s -[INFO] Finished at: 2019-05-13T21:37:20-06:00 -[INFO] ------------------------------------------------------------------------ -``` - - -4. Sample Input and Output: - -Tests are available in `CandidateTest.java` and `CommunicationTest.java`. - - -5. Discussion: diff --git a/src/Q9/Number.java b/src/Q9/Number.java deleted file mode 100644 index e80cedf..0000000 --- a/src/Q9/Number.java +++ /dev/null @@ -1,91 +0,0 @@ -/** - * Assignment 1, COMP268 Class: Number.java - * - * @description Provides static methods for operating on numeric values. - * @author: mo khan Student ID: 3431709 - * @date May 8, 2019 - * @version 1.0 - */ -package Q9; - -import java.util.ArrayList; - -public class Number { - /** - * Checks to see if a number is divisible by 5. - * - * @param n the number to check for divisibility - * @return true if the number is evenly divisible by 5 - */ - public static boolean isDivisibleBy5(int n) { - return isDivisibleBy(n, 5); - } - - /** - * Checks to see if a number is divisible by 7 - * - * @param n the number to check for divisibility - * @return true if the number is evenly divisible by 7 - */ - public static boolean isDivisibleBy7(int n) { - return isDivisibleBy(n, 7); - } - - /** - * Checks if a number is odd - * - * @param n the number to check - * @return true if the number is an odd number. - */ - public static boolean isOdd(int n) { - return !isDivisibleBy(n, 2); - } - - /** - * Checks if a number is prime. This is naive implementation of the prime number check that will - * blow the stack for any sufficiently large number. - * - * @param n the number to check - * @return true if the number is a prime number - */ - public static boolean isPrime(int n) { - if (n <= 1) return false; - - for (int i = n - 1; i > 1; i--) if (isDivisibleBy(n, i)) return false; - - return true; - } - - /** - * Checks to see if a number is divisible by denominator - * - * @param n the number to check for divisibility - * @param denominator the number to see if n is evenly divisible by - * @return true if the number is evenly divisible by denominator - */ - public static boolean isDivisibleBy(int n, int denominator) { - return n % denominator == 0; - } - - /** @return a list of strings for each number between 0 and 113 */ - public static ArrayList<String> iterate() { - ArrayList<String> items = new ArrayList<String>(); - ArrayList<String> row = new ArrayList<String>(); - for (Integer i = 0; i < 113; i++) { - row.clear(); - row.add(String.format("%d", i)); - - if (isOdd(i)) row.add(String.format("%d is odd", i)); - if (isDivisibleBy5(i)) row.add("hi five"); - if (isDivisibleBy7(i + (i + 1))) row.add("wow"); - if (isPrime(i)) row.add("prime"); - - items.add(String.join(",", row)); - } - return items; - } - - public static void main(String[] args) { - for (String item : Number.iterate()) System.out.println(item); - } -} diff --git a/src/Q9/NumberTest.java b/src/Q9/NumberTest.java deleted file mode 100644 index 7e012d1..0000000 --- a/src/Q9/NumberTest.java +++ /dev/null @@ -1,59 +0,0 @@ -package ca.mokhan.test; - -import Q9.Number; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.List; -import junit.framework.Test; -import junit.framework.TestCase; -import junit.framework.TestSuite; - -public class NumberTest extends TestCase { - public NumberTest(String testName) { - super(testName); - } - - public static Test suite() { - return new TestSuite(NumberTest.class); - } - - public void testIsDivisibleBy5() { - for (int i = 0; i < 113; i++) - if (i % 5 == 0) assertTrue(Number.isDivisibleBy5(i)); - else assertFalse(Number.isDivisibleBy5(i)); - } - - public void testIsDivisibleBy7() { - for (int i = 0; i < 113; i++) - if (i % 7 == 0) assertTrue(Number.isDivisibleBy7(i)); - else assertFalse(Number.isDivisibleBy7(i)); - } - - public void testIsOdd() { - for (int i = 0; i < 113; i++) - if (i % 2 == 0) assertFalse(Number.isOdd(i)); - else assertTrue(Number.isOdd(i)); - } - - public void testIsPrime() { - List<Integer> primes = - Arrays.asList( - new Integer[] { - 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, - 83, 89, 97, 101, 103, 107, 109, 113 - }); - - for (Integer i = 0; i < 113; i++) - if (primes.contains(i)) assertTrue(Number.isPrime(i)); - else assertFalse(Number.isPrime(i)); - } - - public void testIterate() { - ArrayList<String> results = Number.iterate(); - assertTrue(results.contains("0,hi five")); - assertTrue(results.contains("1,1 is odd")); - assertTrue(results.contains("2,prime")); - assertTrue(results.contains("3,3 is odd,wow,prime")); - assertTrue(results.contains("4")); - } -} diff --git a/src/Q9/README.md b/src/Q9/README.md deleted file mode 100644 index 550cfb7..0000000 --- a/src/Q9/README.md +++ /dev/null @@ -1,107 +0,0 @@ -Learning Profile for Assignment #1, And Question #9 - -Name: Mo Khan -Student ID: 3431709 - -1. Problem Statement: - -Write a program that iterates through numbers from 0 to 113 using a loop. -Print the numbers, one number per line. -As you print each number, say x, also print the following when appropriate, separated by commas: - -If the number is odd, print “x is odd” -If the number is divisible by 5, print “hi five” -If the total of a number (x) and its subsequent number (x+1) is a value divisible by 7, print “wow” -If the number is prime, print “prime”. - -2. Description of the Code: - -Most of the methods implemented required some for of modulus division. I -was able to extract a method called `isDivisibleBy` that I was able to -reuse quite a bit. - -3. Errors and Warnings: - -```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 48 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 24 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 24 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.006 sec -Running ca.mokhan.test.CandidateTest -Tests run: 4, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.053 sec -Running ca.mokhan.test.NumberTest -Tests run: 5, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.023 sec -Running ca.mokhan.test.EmployeeSavingsTest -Tests run: 6, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.006 sec -Running ca.mokhan.test.CartesianCoordinateSystemTest -Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0 sec -Running ca.mokhan.test.CommunicationTest -Tests run: 2, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0 sec -Running ca.mokhan.test.TaxReturnTest -Tests run: 12, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0 sec -Running ca.mokhan.test.BanffMarathonRunnerTest -Tests run: 4, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.001 sec -Running ca.mokhan.test.AddressBookTest -Tests run: 12, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0 sec -Running ca.mokhan.test.TriangleTest -Tests run: 2, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.001 sec -Running ca.mokhan.test.BonusOnSavingsTest -Tests run: 2, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0 sec -Running ca.mokhan.test.HailstoneSequenceTest -Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0 sec - -Results : - -Tests run: 52, Failures: 0, Errors: 0, Skipped: 0 - -[INFO] ------------------------------------------------------------------------ -[INFO] BUILD SUCCESS -[INFO] ------------------------------------------------------------------------ -[INFO] Total time: 2.844 s -[INFO] Finished at: 2019-05-13T21:40:21-06:00 -[INFO] ------------------------------------------------------------------------ -``` - - -4. Sample Input and Output: -[Provide some test cases with sample input and output of your program.] - -Tests are available in `NumberTest.java`. - -5. Discussion: - -The implementation of the `isPrime` method is very naive and will work -for small values of `n`. It uses a brute force approach without any form -of memoization. |
