From 856c35dbc7080922bfff7c10a7a844a9d0a1cd65 Mon Sep 17 00:00:00 2001 From: mo Date: Sat, 25 May 2019 15:27:51 -0600 Subject: start assignment 2 --- src/Q9/Number.java | 91 ----------------------------------------- src/Q9/NumberTest.java | 59 --------------------------- src/Q9/README.md | 107 ------------------------------------------------- 3 files changed, 257 deletions(-) delete mode 100644 src/Q9/Number.java delete mode 100644 src/Q9/NumberTest.java delete mode 100644 src/Q9/README.md (limited to 'src/Q9') 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 iterate() { - ArrayList items = new ArrayList(); - ArrayList row = new ArrayList(); - 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 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 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. -- cgit v1.2.3