diff options
| author | mo <mo.khan@gmail.com> | 2019-05-25 15:27:51 -0600 |
|---|---|---|
| committer | mo <mo.khan@gmail.com> | 2019-05-25 15:27:51 -0600 |
| commit | 856c35dbc7080922bfff7c10a7a844a9d0a1cd65 (patch) | |
| tree | 739b413bc2a29adc35229ef2fd9b30581601a206 /src/Q7 | |
| parent | 46f68298527e0c59b9f34003b687259477093fde (diff) | |
start assignment 2
Diffstat (limited to 'src/Q7')
| -rw-r--r-- | src/Q7/HailstoneSequence.java | 50 | ||||
| -rw-r--r-- | src/Q7/HailstoneSequenceTest.java | 40 | ||||
| -rw-r--r-- | src/Q7/README.md | 135 |
3 files changed, 0 insertions, 225 deletions
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: |
