From 376e0a76d15d4fec43bd75ea0e49c88135ee447a Mon Sep 17 00:00:00 2001 From: Chris Date: Thu, 25 May 2017 18:06:21 -0400 Subject: [PATCH 1/5] Downloaded files from original lab --- .gitignore | 6 + pom.xml | 25 +++ .../java/kim/christopher/LightSwitcher.java | 95 ++++++++++ .../kim/christopher/LightSwitcherTest.java | 172 ++++++++++++++++++ 4 files changed, 298 insertions(+) create mode 100644 .gitignore create mode 100644 pom.xml create mode 100644 src/main/java/kim/christopher/LightSwitcher.java create mode 100644 src/test/java/kim/christopher/LightSwitcherTest.java diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..b7bd5f4 --- /dev/null +++ b/.gitignore @@ -0,0 +1,6 @@ +.idea/* +*.iml +target/* +*.iws +.DS_Store +log/* \ No newline at end of file diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..6e185bd --- /dev/null +++ b/pom.xml @@ -0,0 +1,25 @@ + + 4.0.0 + + kim.christopher + BitSetMicroLab + 1.0-SNAPSHOT + jar + + BitSetMicroLab + http://maven.apache.org + + + UTF-8 + + + + + junit + junit + 4.12 + test + + + diff --git a/src/main/java/kim/christopher/LightSwitcher.java b/src/main/java/kim/christopher/LightSwitcher.java new file mode 100644 index 0000000..485c417 --- /dev/null +++ b/src/main/java/kim/christopher/LightSwitcher.java @@ -0,0 +1,95 @@ +package kim.christopher; + +/** + * This lab is just a practice in bitwise operations. Though all of the return and arg types are ints, + * pretend that they're bytes. That is, all of the tests are passing in values that are only 8 bits. + * Use only bitwise operations (&, |, ^, ~, >>>, >>, <<) and return. Nothing else. + * + * The tests are written for you, so look at those for inspiration on how to solve these. All you need to do is write + * the functions to make the tests pass. + */ +public class LightSwitcher { + + /** + * Take a given input of switches, and the switches to turn on, and return the new state of the switches. + * NOTE: If the initial state of a switch is already turned on, do not turn it off. + */ + public static int turnOnSwitches(int switches, int switchesToTurnOn) { + + } + + /** + * Take a given input of switches and turn them all to on. + * Remember to use bit notation (0bxxxxxxxx) and a bit operator. + */ + public static int turnOnAllSwitches(int switches) { + + } + + /** + * Take a given input of switches and the switches to turn off, and return the new state of the switches. + * NOTE: If a switch is already off, do not turn it on. + * And a '1' in a position in 'switchesToTurnOff' means to turn that switch to off. + */ + public static int turnOffSwitches(int switches, int switchesToTurnOff) { + + } + + /** + * Take a given input of switches and turn them all off. + * Remember to use bit notation and a bit operator. + */ + public static int turnOffAllSwitches(int switches) { + + } + + /** + * Take a given input of switches and the switches to flip, and return the new state of the switches. + * NOTE: Wherever there is a '1' in switchesToFlip, flip the state of that switch in switches. + * I.E switches = 1 0 1 and switchesToFlip = 1 1 0 should return 0 1 1. + */ + public static int flipSwitches(int switches, int switchesToFlip) { + + } + + /** + * Take a given input of switches and flip them all. + * NOTE: An integer has more than 8 bits, so find a way to only return the rightmost 8 bits. + */ + public static int flipAllSwitches(int switches) { + + } + + /** + * Return the state of a switch in a given position. + * Count switches from 0, and from right to left. + * So, a byte reads 76543210 + */ + public static int getSwitchPositionAt(int switches, int position) { + + } + + /** + * Move all the the bits to the right `count` places. + */ + public static int moveRightBy(int switches, int count) { + + } + + /** + * Move all the the bits to the left `count` places. + * NOTE: An integer has more than 8 bits, so find a way to only return the rightmost 8 bits. + */ + public static int moveLeftBy(int switches, int count){ + + } + + /** + * This is written for you to help with debugging. If you call System.out.println(viewSwitches(switches)), + * you can see the rightmost 8 bits of a given integer. + */ + public static String viewSwitches(int switches) { + + return String.format("%8s", Integer.toBinaryString((switches & 0b11111111))).replace(' ', '0'); + } +} \ No newline at end of file diff --git a/src/test/java/kim/christopher/LightSwitcherTest.java b/src/test/java/kim/christopher/LightSwitcherTest.java new file mode 100644 index 0000000..85a2885 --- /dev/null +++ b/src/test/java/kim/christopher/LightSwitcherTest.java @@ -0,0 +1,172 @@ +package kim.christopher; + +import org.junit.Assert; +import org.junit.Test; + +import static org.junit.Assert.*; + +public class LightSwitcherTest { + private int baseByte = 0b10101010; + private int flippedBaseByte = 0b01010101; + + @Test + public void testTurnOnSwitches() { + int result1 = LightSwitcher.turnOnSwitches(baseByte, 0b11110000); + int expected1 = 0b11111010; + int result2 = LightSwitcher.turnOnSwitches(flippedBaseByte, 0b11110000); + int expected2 = 0b11110101; + int result3 = LightSwitcher.turnOnSwitches(baseByte, 0b00001111); + int expected3 = 0b10101111; + int result4 = LightSwitcher.turnOnSwitches(flippedBaseByte, 0b00001111); + int expected4 = 0b01011111; + assertEquals(result1, expected1); + assertEquals(result2, expected2); + assertEquals(result3, expected3); + assertEquals(result4, expected4); + } + + @Test + public void testTurnOnAllSwitches() { + int result1 = LightSwitcher.turnOnAllSwitches(baseByte); + int result2 = LightSwitcher.turnOnAllSwitches(flippedBaseByte); + int result3 = LightSwitcher.turnOnAllSwitches(0b11111111); + int result4 = LightSwitcher.turnOnAllSwitches(0b00000000); + assertEquals(result1, 0b11111111); + assertEquals(result2, 0b11111111); + assertEquals(result3, 0b11111111); + assertEquals(result4, 0b11111111); + } + + @Test + public void testTurnOffSwitches() { + int result1 = LightSwitcher.turnOffSwitches(baseByte, 0b11110000); + int expected1 = 0b00001010; + int result2 = LightSwitcher.turnOffSwitches(flippedBaseByte, 0b11110000); + int expected2 = 0b00000101; + int result3 = LightSwitcher.turnOffSwitches(baseByte, 0b00001111); + int expected3 = 0b10100000; + int result4 = LightSwitcher.turnOffSwitches(flippedBaseByte, 0b00001111); + int expected4 = 0b01010000; + assertEquals(result1, expected1); + assertEquals(result2, expected2); + assertEquals(result3, expected3); + assertEquals(result4, expected4); + } + + @Test + public void testTurnOffAllSwitches() { + int result1 = LightSwitcher.turnOffAllSwitches(baseByte); + int result2 = LightSwitcher.turnOffAllSwitches(flippedBaseByte); + int result3 = LightSwitcher.turnOffAllSwitches(0b11111111); + int result4 = LightSwitcher.turnOffAllSwitches(0b00000000); + assertEquals(result1, 0b00000000); + assertEquals(result2, 0b00000000); + assertEquals(result3, 0b00000000); + assertEquals(result4, 0b00000000); + } + + @Test + public void testFlipSwitches() { + int result1 = LightSwitcher.flipSwitches(baseByte, 0b11110000); + int expected1 = 0b01011010; + int result2 = LightSwitcher.flipSwitches(flippedBaseByte, 0b11110000); + int expected2 = 0b10100101; + int result3 = LightSwitcher.flipSwitches(baseByte, 0b00001111); + int expected3 = 0b10100101; + int result4 = LightSwitcher.flipSwitches(flippedBaseByte, 0b00001111); + int expected4 = 0b01011010; + assertEquals(result1, expected1); + assertEquals(result2, expected2); + assertEquals(result3, expected3); + assertEquals(result4, expected4); + } + + @Test + public void testFlipAllSwitches() { + int result1 = LightSwitcher.flipAllSwitches(baseByte); + int result2 = LightSwitcher.flipAllSwitches(flippedBaseByte); + int result3 = LightSwitcher.flipAllSwitches(0b11111111); + int result4 = LightSwitcher.flipAllSwitches(0b00000000); + assertEquals(result1, 0b01010101); + assertEquals(result2, 0b10101010); + assertEquals(result3, 0b00000000); + assertEquals(result4, 0b11111111); + } + + @Test + public void testGetSwitchPositionAt() { + for(int i = 0; i < 8; i++) { + assertEquals(i%2, LightSwitcher.getSwitchPositionAt(baseByte, i)); + } + for(int i = 0; i < 8; i++) { + assertEquals((i + 1) % 2, LightSwitcher.getSwitchPositionAt(flippedBaseByte, i)); + } + assertEquals(0, LightSwitcher.getSwitchPositionAt(0b11111110, 0)); + assertEquals(1, LightSwitcher.getSwitchPositionAt(0b11111110, 1)); + assertEquals(1, LightSwitcher.getSwitchPositionAt(0b11111110, 7)); + } + + @Test + public void testMoveRightBy() { + int result1 = LightSwitcher.moveRightBy(baseByte, 1); + int result2 = LightSwitcher.moveRightBy(baseByte, 2); + int result3 = LightSwitcher.moveRightBy(baseByte, 3); + int result4 = LightSwitcher.moveRightBy(baseByte, 4); + int result5 = LightSwitcher.moveRightBy(baseByte, 5); + int result6 = LightSwitcher.moveRightBy(baseByte, 6); + int result7 = LightSwitcher.moveRightBy(baseByte, 7); + int result8 = LightSwitcher.moveRightBy(baseByte, 8); + int expected1 = 0b01010101; + int expected2 = 0b00101010; + int expected3 = 0b00010101; + int expected4 = 0b00001010; + int expected5 = 0b00000101; + int expected6 = 0b00000010; + int expected7 = 0b00000001; + int expected8 = 0b00000000; + assertEquals(result1, expected1); + assertEquals(result2, expected2); + assertEquals(result3, expected3); + assertEquals(result4, expected4); + assertEquals(result5, expected5); + assertEquals(result6, expected6); + assertEquals(result7, expected7); + assertEquals(result8, expected8); + } + + @Test + public void testMoveLeftBy() { + int result1 = LightSwitcher.moveLeftBy(baseByte, 1); + int result2 = LightSwitcher.moveLeftBy(baseByte, 2); + int result3 = LightSwitcher.moveLeftBy(baseByte, 3); + int result4 = LightSwitcher.moveLeftBy(baseByte, 4); + int result5 = LightSwitcher.moveLeftBy(baseByte, 5); + int result6 = LightSwitcher.moveLeftBy(baseByte, 6); + int result7 = LightSwitcher.moveLeftBy(baseByte, 7); + int result8 = LightSwitcher.moveLeftBy(baseByte, 8); + int expected1 = 0b01010100; + int expected2 = 0b10101000; + int expected3 = 0b01010000; + int expected4 = 0b10100000; + int expected5 = 0b01000000; + int expected6 = 0b10000000; + int expected7 = 0b00000000; + int expected8 = 0b00000000; + assertEquals(result1, expected1); + assertEquals(result2, expected2); + assertEquals(result3, expected3); + assertEquals(result4, expected4); + assertEquals(result5, expected5); + assertEquals(result6, expected6); + assertEquals(result7, expected7); + assertEquals(result8, expected8); + } + + @Test + public void testViewSwitches() { + assertEquals("11111111", LightSwitcher.viewSwitches(0b111111111111)); + assertEquals("00000000", LightSwitcher.viewSwitches(0)); + assertEquals("01010101", LightSwitcher.viewSwitches(0b111101010101)); + } + +} \ No newline at end of file From 60401366c8630e15441afc08015db6bffeacb799 Mon Sep 17 00:00:00 2001 From: Chris Date: Thu, 25 May 2017 21:56:07 -0400 Subject: [PATCH 2/5] almost done --- pom.xml | 12 + .../java/kim/christopher/LightSwitcher.java | 109 +++++-- .../kim/christopher/LightSwitcherTest.java | 301 +++++++++++------- 3 files changed, 276 insertions(+), 146 deletions(-) diff --git a/pom.xml b/pom.xml index 6e185bd..625b977 100644 --- a/pom.xml +++ b/pom.xml @@ -5,6 +5,18 @@ kim.christopher BitSetMicroLab 1.0-SNAPSHOT + + + + org.apache.maven.plugins + maven-compiler-plugin + + 1.7 + 1.7 + + + + jar BitSetMicroLab diff --git a/src/main/java/kim/christopher/LightSwitcher.java b/src/main/java/kim/christopher/LightSwitcher.java index 485c417..1a6f1f6 100644 --- a/src/main/java/kim/christopher/LightSwitcher.java +++ b/src/main/java/kim/christopher/LightSwitcher.java @@ -1,12 +1,16 @@ package kim.christopher; +import sun.jvm.hotspot.utilities.Bits; + +import java.util.BitSet; + /** - * This lab is just a practice in bitwise operations. Though all of the return and arg types are ints, - * pretend that they're bytes. That is, all of the tests are passing in values that are only 8 bits. + * This lab is just a practice in bitwise operations. Though all of the return and arg types are BitSets, + * pretend that they're bytes. That is, all of the tests are passing in values that are only 8 bits. * Use only bitwise operations (&, |, ^, ~, >>>, >>, <<) and return. Nothing else. - * + *

* The tests are written for you, so look at those for inspiration on how to solve these. All you need to do is write - * the functions to make the tests pass. + * the functions to make the tests pass. */ public class LightSwitcher { @@ -14,16 +18,20 @@ public class LightSwitcher { * Take a given input of switches, and the switches to turn on, and return the new state of the switches. * NOTE: If the initial state of a switch is already turned on, do not turn it off. */ - public static int turnOnSwitches(int switches, int switchesToTurnOn) { - + public static BitSet turnOnSwitches(BitSet switches, BitSet switchesToTurnOn) { + BitSet result = (BitSet) switches.clone(); + result.or(switchesToTurnOn); + return result; } /** * Take a given input of switches and turn them all to on. * Remember to use bit notation (0bxxxxxxxx) and a bit operator. */ - public static int turnOnAllSwitches(int switches) { - + public static BitSet turnOnAllSwitches(BitSet switches) { + BitSet result = (BitSet) switches.clone(); + result.or(fromString("11111111")); + return result; } /** @@ -31,16 +39,18 @@ public static int turnOnAllSwitches(int switches) { * NOTE: If a switch is already off, do not turn it on. * And a '1' in a position in 'switchesToTurnOff' means to turn that switch to off. */ - public static int turnOffSwitches(int switches, int switchesToTurnOff) { - + public static BitSet turnOffSwitches(BitSet switches, BitSet switchesToTurnOff) { + BitSet result = (BitSet) switches.clone(); + result.andNot(switchesToTurnOff); + return result; } /** * Take a given input of switches and turn them all off. * Remember to use bit notation and a bit operator. */ - public static int turnOffAllSwitches(int switches) { - + public static BitSet turnOffAllSwitches(BitSet switches) { + return new BitSet(); } /** @@ -48,16 +58,26 @@ public static int turnOffAllSwitches(int switches) { * NOTE: Wherever there is a '1' in switchesToFlip, flip the state of that switch in switches. * I.E switches = 1 0 1 and switchesToFlip = 1 1 0 should return 0 1 1. */ - public static int flipSwitches(int switches, int switchesToFlip) { - + public static BitSet flipSwitches(BitSet switches, BitSet switchesToFlip) { + BitSet result = (BitSet) switches.clone(); + for (int i = 0; i < 8; i++) { + if (switchesToFlip.get(i)) { + result.flip(i); + } + } + return result; } /** * Take a given input of switches and flip them all. - * NOTE: An integer has more than 8 bits, so find a way to only return the rightmost 8 bits. + * NOTE: An BitSeteger has more than 8 bits, so find a way to only return the rightmost 8 bits. */ - public static int flipAllSwitches(int switches) { - + public static BitSet flipAllSwitches(BitSet switches) { + BitSet result = (BitSet) switches.clone(); + for (int i = 0; i < 8; i++) { + result.flip(i); + } + return result; } /** @@ -65,31 +85,62 @@ public static int flipAllSwitches(int switches) { * Count switches from 0, and from right to left. * So, a byte reads 76543210 */ - public static int getSwitchPositionAt(int switches, int position) { - + public static int getSwitchPositionAt(BitSet switches, int position) { + return switches.get(position)? 1 : 0; } /** * Move all the the bits to the right `count` places. */ - public static int moveRightBy(int switches, int count) { - + public static BitSet moveRightBy(BitSet switches, int count) { + BitSet result = (BitSet) switches.clone(); + for(int i = 0; i < count; i++){ + for(int j = 1; j < 9; j++){ + if(result.get(j)){ + result.set(j - 1); + } else result.set(j - 1, false); + } + } + return result; } /** * Move all the the bits to the left `count` places. - * NOTE: An integer has more than 8 bits, so find a way to only return the rightmost 8 bits. + * NOTE: An BitSeteger has more than 8 bits, so find a way to only return the rightmost 8 bits. */ - public static int moveLeftBy(int switches, int count){ - + public static BitSet moveLeftBy(BitSet switches, int count) { + BitSet result = (BitSet) switches.clone(); + for(int i = 0; i < count; i++){ + for(int j = 6; j >= 0; j--){ + if(result.get(j)){ + result.set(j + 1); + } else result.set(j + 1, false); + } + result.set(0, false); + } + return result; } - /** - * This is written for you to help with debugging. If you call System.out.println(viewSwitches(switches)), - * you can see the rightmost 8 bits of a given integer. - */ public static String viewSwitches(int switches) { - return String.format("%8s", Integer.toBinaryString((switches & 0b11111111))).replace(' ', '0'); } + + public static BitSet fromString(final String s) { + return BitSet.valueOf(new long[]{Long.parseLong(s, 2)}); + } + + public static String toString(BitSet bs) { + return Long.toString(bs.toLongArray()[0], 2); + } + + public static int leftShiftSteps(BitSet bs){ + return 0; + } + + public static int rightShiftSteps(BitSet bs){ + return 0; + } + + + } \ No newline at end of file diff --git a/src/test/java/kim/christopher/LightSwitcherTest.java b/src/test/java/kim/christopher/LightSwitcherTest.java index 85a2885..aa6a95e 100644 --- a/src/test/java/kim/christopher/LightSwitcherTest.java +++ b/src/test/java/kim/christopher/LightSwitcherTest.java @@ -1,165 +1,178 @@ package kim.christopher; -import org.junit.Assert; +import org.junit.Before; import org.junit.Test; -import static org.junit.Assert.*; +import java.util.BitSet; + +import static kim.christopher.LightSwitcher.*; +import static org.junit.Assert.assertEquals; public class LightSwitcherTest { - private int baseByte = 0b10101010; - private int flippedBaseByte = 0b01010101; + private BitSet baseByte; + private BitSet flippedBaseByte; + private BitSet allOnes; + private BitSet allZeros; + + @Before + public void initialize() { + baseByte = fromString("10101010"); + flippedBaseByte = fromString("01010101"); + allOnes = fromString("11111111"); + allZeros = fromString("00000000"); + } @Test public void testTurnOnSwitches() { - int result1 = LightSwitcher.turnOnSwitches(baseByte, 0b11110000); - int expected1 = 0b11111010; - int result2 = LightSwitcher.turnOnSwitches(flippedBaseByte, 0b11110000); - int expected2 = 0b11110101; - int result3 = LightSwitcher.turnOnSwitches(baseByte, 0b00001111); - int expected3 = 0b10101111; - int result4 = LightSwitcher.turnOnSwitches(flippedBaseByte, 0b00001111); - int expected4 = 0b01011111; - assertEquals(result1, expected1); - assertEquals(result2, expected2); - assertEquals(result3, expected3); - assertEquals(result4, expected4); + BitSet result1 = turnOnSwitches(baseByte, fromString("11110000")); + BitSet expected1 = fromString("11111010"); + BitSet result2 = turnOnSwitches(flippedBaseByte, fromString("11110000")); + BitSet expected2 = fromString("11110101"); + BitSet result3 = turnOnSwitches(baseByte, fromString("00001111")); + BitSet expected3 = fromString("10101111"); + BitSet result4 = turnOnSwitches(flippedBaseByte, fromString("00001111")); + BitSet expected4 = fromString("01011111"); + assertEquals(expected1, result1); + assertEquals(expected2, result2); + assertEquals(expected3, result3); + assertEquals(expected4, result4); } @Test public void testTurnOnAllSwitches() { - int result1 = LightSwitcher.turnOnAllSwitches(baseByte); - int result2 = LightSwitcher.turnOnAllSwitches(flippedBaseByte); - int result3 = LightSwitcher.turnOnAllSwitches(0b11111111); - int result4 = LightSwitcher.turnOnAllSwitches(0b00000000); - assertEquals(result1, 0b11111111); - assertEquals(result2, 0b11111111); - assertEquals(result3, 0b11111111); - assertEquals(result4, 0b11111111); + BitSet result1 = turnOnAllSwitches(baseByte); + BitSet result2 = turnOnAllSwitches(flippedBaseByte); + BitSet result3 = turnOnAllSwitches(allOnes); + BitSet result4 = turnOnAllSwitches(allZeros); + assertEquals(allOnes, result1); + assertEquals(allOnes, result2); + assertEquals(allOnes, result3); + assertEquals(allOnes, result4); } @Test public void testTurnOffSwitches() { - int result1 = LightSwitcher.turnOffSwitches(baseByte, 0b11110000); - int expected1 = 0b00001010; - int result2 = LightSwitcher.turnOffSwitches(flippedBaseByte, 0b11110000); - int expected2 = 0b00000101; - int result3 = LightSwitcher.turnOffSwitches(baseByte, 0b00001111); - int expected3 = 0b10100000; - int result4 = LightSwitcher.turnOffSwitches(flippedBaseByte, 0b00001111); - int expected4 = 0b01010000; - assertEquals(result1, expected1); - assertEquals(result2, expected2); - assertEquals(result3, expected3); - assertEquals(result4, expected4); + BitSet result1 = turnOffSwitches(baseByte, fromString("11110000")); + BitSet expected1 = fromString("00001010"); + BitSet result2 = turnOffSwitches(flippedBaseByte, fromString("11110000")); + BitSet expected2 = fromString("00000101"); + BitSet result3 = turnOffSwitches(baseByte, fromString("00001111")); + BitSet expected3 = fromString("10100000"); + BitSet result4 = turnOffSwitches(flippedBaseByte, fromString("00001111")); + BitSet expected4 = fromString("01010000"); + assertEquals(expected1, result1); + assertEquals(expected2, result2); + assertEquals(expected3, result3); + assertEquals(expected4, result4); } @Test public void testTurnOffAllSwitches() { - int result1 = LightSwitcher.turnOffAllSwitches(baseByte); - int result2 = LightSwitcher.turnOffAllSwitches(flippedBaseByte); - int result3 = LightSwitcher.turnOffAllSwitches(0b11111111); - int result4 = LightSwitcher.turnOffAllSwitches(0b00000000); - assertEquals(result1, 0b00000000); - assertEquals(result2, 0b00000000); - assertEquals(result3, 0b00000000); - assertEquals(result4, 0b00000000); + BitSet result1 = turnOffAllSwitches(baseByte); + BitSet result2 = turnOffAllSwitches(flippedBaseByte); + BitSet result3 = turnOffAllSwitches(allOnes); + BitSet result4 = turnOffAllSwitches(allZeros); + assertEquals(allZeros, result1); + assertEquals(allZeros, result2); + assertEquals(allZeros, result3); + assertEquals(allZeros, result4); } @Test public void testFlipSwitches() { - int result1 = LightSwitcher.flipSwitches(baseByte, 0b11110000); - int expected1 = 0b01011010; - int result2 = LightSwitcher.flipSwitches(flippedBaseByte, 0b11110000); - int expected2 = 0b10100101; - int result3 = LightSwitcher.flipSwitches(baseByte, 0b00001111); - int expected3 = 0b10100101; - int result4 = LightSwitcher.flipSwitches(flippedBaseByte, 0b00001111); - int expected4 = 0b01011010; - assertEquals(result1, expected1); - assertEquals(result2, expected2); - assertEquals(result3, expected3); - assertEquals(result4, expected4); + BitSet result1 = flipSwitches(baseByte, fromString("11110000")); + BitSet expected1 = fromString("01011010"); + BitSet result2 = flipSwitches(flippedBaseByte, fromString("11110000")); + BitSet expected2 = fromString("10100101"); + BitSet result3 = flipSwitches(baseByte, fromString("00001111")); + BitSet expected3 = fromString("10100101"); + BitSet result4 = flipSwitches(flippedBaseByte, fromString("00001111")); + BitSet expected4 = fromString("01011010"); + assertEquals(expected1, result1); + assertEquals(expected2, result2); + assertEquals(expected3, result3); + assertEquals(expected4, result4); } @Test public void testFlipAllSwitches() { - int result1 = LightSwitcher.flipAllSwitches(baseByte); - int result2 = LightSwitcher.flipAllSwitches(flippedBaseByte); - int result3 = LightSwitcher.flipAllSwitches(0b11111111); - int result4 = LightSwitcher.flipAllSwitches(0b00000000); - assertEquals(result1, 0b01010101); - assertEquals(result2, 0b10101010); - assertEquals(result3, 0b00000000); - assertEquals(result4, 0b11111111); + BitSet result1 = flipAllSwitches(baseByte); + BitSet result2 = flipAllSwitches(flippedBaseByte); + BitSet result3 = flipAllSwitches(allOnes); + BitSet result4 = flipAllSwitches(allZeros); + assertEquals(flippedBaseByte, result1); + assertEquals(baseByte, result2); + assertEquals(allZeros, result3); + assertEquals(allOnes, result4); } @Test public void testGetSwitchPositionAt() { for(int i = 0; i < 8; i++) { - assertEquals(i%2, LightSwitcher.getSwitchPositionAt(baseByte, i)); + assertEquals(i%2, getSwitchPositionAt(baseByte, i)); } for(int i = 0; i < 8; i++) { - assertEquals((i + 1) % 2, LightSwitcher.getSwitchPositionAt(flippedBaseByte, i)); + assertEquals((i + 1) % 2, getSwitchPositionAt(flippedBaseByte, i)); } - assertEquals(0, LightSwitcher.getSwitchPositionAt(0b11111110, 0)); - assertEquals(1, LightSwitcher.getSwitchPositionAt(0b11111110, 1)); - assertEquals(1, LightSwitcher.getSwitchPositionAt(0b11111110, 7)); + assertEquals(0, getSwitchPositionAt(fromString("11111110"), 0)); + assertEquals(1, getSwitchPositionAt(fromString("11111110"), 1)); + assertEquals(1, getSwitchPositionAt(fromString("11111110"), 7)); } @Test public void testMoveRightBy() { - int result1 = LightSwitcher.moveRightBy(baseByte, 1); - int result2 = LightSwitcher.moveRightBy(baseByte, 2); - int result3 = LightSwitcher.moveRightBy(baseByte, 3); - int result4 = LightSwitcher.moveRightBy(baseByte, 4); - int result5 = LightSwitcher.moveRightBy(baseByte, 5); - int result6 = LightSwitcher.moveRightBy(baseByte, 6); - int result7 = LightSwitcher.moveRightBy(baseByte, 7); - int result8 = LightSwitcher.moveRightBy(baseByte, 8); - int expected1 = 0b01010101; - int expected2 = 0b00101010; - int expected3 = 0b00010101; - int expected4 = 0b00001010; - int expected5 = 0b00000101; - int expected6 = 0b00000010; - int expected7 = 0b00000001; - int expected8 = 0b00000000; - assertEquals(result1, expected1); - assertEquals(result2, expected2); - assertEquals(result3, expected3); - assertEquals(result4, expected4); - assertEquals(result5, expected5); - assertEquals(result6, expected6); - assertEquals(result7, expected7); - assertEquals(result8, expected8); + BitSet result1 = moveRightBy(baseByte, 1); + BitSet result2 = moveRightBy(baseByte, 2); + BitSet result3 = moveRightBy(baseByte, 3); + BitSet result4 = moveRightBy(baseByte, 4); + BitSet result5 = moveRightBy(baseByte, 5); + BitSet result6 = moveRightBy(baseByte, 6); + BitSet result7 = moveRightBy(baseByte, 7); + BitSet result8 = moveRightBy(baseByte, 8); + BitSet expected1 = fromString("01010101"); + BitSet expected2 = fromString("00101010"); + BitSet expected3 = fromString("00010101"); + BitSet expected4 = fromString("00001010"); + BitSet expected5 = fromString("00000101"); + BitSet expected6 = fromString("00000010"); + BitSet expected7 = fromString("00000001"); + BitSet expected8 = fromString("00000000"); + assertEquals(expected1, result1); + assertEquals(expected2, result2); + assertEquals(expected3, result3); + assertEquals(expected4, result4); + assertEquals(expected5, result5); + assertEquals(expected6, result6); + assertEquals(expected7, result7); + assertEquals(expected8, result8); } @Test public void testMoveLeftBy() { - int result1 = LightSwitcher.moveLeftBy(baseByte, 1); - int result2 = LightSwitcher.moveLeftBy(baseByte, 2); - int result3 = LightSwitcher.moveLeftBy(baseByte, 3); - int result4 = LightSwitcher.moveLeftBy(baseByte, 4); - int result5 = LightSwitcher.moveLeftBy(baseByte, 5); - int result6 = LightSwitcher.moveLeftBy(baseByte, 6); - int result7 = LightSwitcher.moveLeftBy(baseByte, 7); - int result8 = LightSwitcher.moveLeftBy(baseByte, 8); - int expected1 = 0b01010100; - int expected2 = 0b10101000; - int expected3 = 0b01010000; - int expected4 = 0b10100000; - int expected5 = 0b01000000; - int expected6 = 0b10000000; - int expected7 = 0b00000000; - int expected8 = 0b00000000; - assertEquals(result1, expected1); - assertEquals(result2, expected2); - assertEquals(result3, expected3); - assertEquals(result4, expected4); - assertEquals(result5, expected5); - assertEquals(result6, expected6); - assertEquals(result7, expected7); - assertEquals(result8, expected8); + BitSet result1 = moveLeftBy(baseByte, 1); + BitSet result2 = moveLeftBy(baseByte, 2); + BitSet result3 = moveLeftBy(baseByte, 3); + BitSet result4 = moveLeftBy(baseByte, 4); + BitSet result5 = moveLeftBy(baseByte, 5); + BitSet result6 = moveLeftBy(baseByte, 6); + BitSet result7 = moveLeftBy(baseByte, 7); + BitSet result8 = moveLeftBy(baseByte, 8); + BitSet expected1 = fromString("01010100"); + BitSet expected2 = fromString("10101000"); + BitSet expected3 = fromString("01010000"); + BitSet expected4 = fromString("10100000"); + BitSet expected5 = fromString("01000000"); + BitSet expected6 = fromString("10000000"); + BitSet expected7 = fromString("00000000"); + BitSet expected8 = fromString("00000000"); + assertEquals(expected1, result1); + assertEquals(expected2, result2); + assertEquals(expected3, result3); + assertEquals(expected4, result4); + assertEquals(expected5, result5); + assertEquals(expected6, result6); + assertEquals(expected7, result7); + assertEquals(expected8, result8); } @Test @@ -169,4 +182,58 @@ public void testViewSwitches() { assertEquals("01010101", LightSwitcher.viewSwitches(0b111101010101)); } + @Test + public void leftShiftStepsTest(){ + //Given + int expected1 = 6; + int expected2 = 10; + int expected3 = 12; + int expected4 = 4; + int expected5 = 16; + int expected6 = 8; + + //When + int actual1 = leftShiftSteps(baseByte); + int actual2 = leftShiftSteps(flippedBaseByte); + int actual3 = leftShiftSteps(fromString("00110011")); + int actual4 = leftShiftSteps(fromString("11001100")); + int actual5 = leftShiftSteps(fromString("00001111")); + int actual6 = leftShiftSteps(fromString("00111100")); + + //Then + assertEquals("Number of steps should be 6", expected1, actual1); + assertEquals("Number of steps should be 10", expected1, actual1); + assertEquals("Number of steps should be 12", expected1, actual1); + assertEquals("Number of steps should be 4", expected1, actual1); + assertEquals("Number of steps should be 16", expected1, actual1); + assertEquals("Number of steps should be 8", expected1, actual1); + + } + + public void rightShiftStepsTest(){ + //Given + int expected1 = 10; + int expected2 = 6; + int expected3 = 4; + int expected4 = 12; + int expected5 = 0; + int expected6 = 8; + + //When + int actual1 = rightShiftSteps(baseByte); + int actual2 = rightShiftSteps(flippedBaseByte); + int actual3 = rightShiftSteps(fromString("00110011")); + int actual4 = rightShiftSteps(fromString("11001100")); + int actual5 = rightShiftSteps(fromString("00001111")); + int actual6 = rightShiftSteps(fromString("00111100")); + + //Then + assertEquals("Number of steps should be 10", expected1, actual1); + assertEquals("Number of steps should be 6", expected1, actual1); + assertEquals("Number of steps should be 4", expected1, actual1); + assertEquals("Number of steps should be 12", expected1, actual1); + assertEquals("Number of steps should be 0", expected1, actual1); + assertEquals("Number of steps should be 8", expected1, actual1); + } + } \ No newline at end of file From a70aa2db3ca495d313ebf4885a073b3f797dc3f4 Mon Sep 17 00:00:00 2001 From: Chris Date: Fri, 26 May 2017 11:17:38 -0400 Subject: [PATCH 3/5] Finished BitSet Lab --- .../java/kim/christopher/LightSwitcher.java | 88 +++++-------------- .../kim/christopher/LightSwitcherTest.java | 1 + 2 files changed, 24 insertions(+), 65 deletions(-) diff --git a/src/main/java/kim/christopher/LightSwitcher.java b/src/main/java/kim/christopher/LightSwitcher.java index 1a6f1f6..06ff201 100644 --- a/src/main/java/kim/christopher/LightSwitcher.java +++ b/src/main/java/kim/christopher/LightSwitcher.java @@ -1,120 +1,71 @@ package kim.christopher; -import sun.jvm.hotspot.utilities.Bits; - import java.util.BitSet; -/** - * This lab is just a practice in bitwise operations. Though all of the return and arg types are BitSets, - * pretend that they're bytes. That is, all of the tests are passing in values that are only 8 bits. - * Use only bitwise operations (&, |, ^, ~, >>>, >>, <<) and return. Nothing else. - *

- * The tests are written for you, so look at those for inspiration on how to solve these. All you need to do is write - * the functions to make the tests pass. - */ public class LightSwitcher { - /** - * Take a given input of switches, and the switches to turn on, and return the new state of the switches. - * NOTE: If the initial state of a switch is already turned on, do not turn it off. - */ public static BitSet turnOnSwitches(BitSet switches, BitSet switchesToTurnOn) { BitSet result = (BitSet) switches.clone(); result.or(switchesToTurnOn); return result; } - /** - * Take a given input of switches and turn them all to on. - * Remember to use bit notation (0bxxxxxxxx) and a bit operator. - */ public static BitSet turnOnAllSwitches(BitSet switches) { BitSet result = (BitSet) switches.clone(); result.or(fromString("11111111")); return result; } - /** - * Take a given input of switches and the switches to turn off, and return the new state of the switches. - * NOTE: If a switch is already off, do not turn it on. - * And a '1' in a position in 'switchesToTurnOff' means to turn that switch to off. - */ public static BitSet turnOffSwitches(BitSet switches, BitSet switchesToTurnOff) { BitSet result = (BitSet) switches.clone(); result.andNot(switchesToTurnOff); return result; } - /** - * Take a given input of switches and turn them all off. - * Remember to use bit notation and a bit operator. - */ public static BitSet turnOffAllSwitches(BitSet switches) { return new BitSet(); } - /** - * Take a given input of switches and the switches to flip, and return the new state of the switches. - * NOTE: Wherever there is a '1' in switchesToFlip, flip the state of that switch in switches. - * I.E switches = 1 0 1 and switchesToFlip = 1 1 0 should return 0 1 1. - */ + public static BitSet flipSwitches(BitSet switches, BitSet switchesToFlip) { BitSet result = (BitSet) switches.clone(); for (int i = 0; i < 8; i++) { - if (switchesToFlip.get(i)) { + if (switchesToFlip.get(i)) result.flip(i); - } } return result; } - /** - * Take a given input of switches and flip them all. - * NOTE: An BitSeteger has more than 8 bits, so find a way to only return the rightmost 8 bits. - */ public static BitSet flipAllSwitches(BitSet switches) { BitSet result = (BitSet) switches.clone(); - for (int i = 0; i < 8; i++) { + for (int i = 0; i < 8; i++) result.flip(i); - } return result; } - /** - * Return the state of a switch in a given position. - * Count switches from 0, and from right to left. - * So, a byte reads 76543210 - */ public static int getSwitchPositionAt(BitSet switches, int position) { return switches.get(position)? 1 : 0; } - /** - * Move all the the bits to the right `count` places. - */ public static BitSet moveRightBy(BitSet switches, int count) { BitSet result = (BitSet) switches.clone(); for(int i = 0; i < count; i++){ for(int j = 1; j < 9; j++){ - if(result.get(j)){ + if(result.get(j)) result.set(j - 1); - } else result.set(j - 1, false); + else result.set(j - 1, false); } } return result; } - /** - * Move all the the bits to the left `count` places. - * NOTE: An BitSeteger has more than 8 bits, so find a way to only return the rightmost 8 bits. - */ public static BitSet moveLeftBy(BitSet switches, int count) { BitSet result = (BitSet) switches.clone(); for(int i = 0; i < count; i++){ for(int j = 6; j >= 0; j--){ - if(result.get(j)){ + if(result.get(j)) result.set(j + 1); - } else result.set(j + 1, false); + else result.set(j + 1, false); } result.set(0, false); } @@ -129,18 +80,25 @@ public static BitSet fromString(final String s) { return BitSet.valueOf(new long[]{Long.parseLong(s, 2)}); } - public static String toString(BitSet bs) { - return Long.toString(bs.toLongArray()[0], 2); - } - public static int leftShiftSteps(BitSet bs){ - return 0; + int count = 0; + int result = 0; + for(int i = 7; i >= 0; i--){ + if(bs.get(i)) + result += count; + else count++; + } + return result; } public static int rightShiftSteps(BitSet bs){ - return 0; + int count = 0; + int result = 0; + for(int i = 0; i < 8; i++){ + if(bs.get(i)) + result += count; + else count++; + } + return result; } - - - } \ No newline at end of file diff --git a/src/test/java/kim/christopher/LightSwitcherTest.java b/src/test/java/kim/christopher/LightSwitcherTest.java index aa6a95e..7c765d3 100644 --- a/src/test/java/kim/christopher/LightSwitcherTest.java +++ b/src/test/java/kim/christopher/LightSwitcherTest.java @@ -210,6 +210,7 @@ public void leftShiftStepsTest(){ } + @Test public void rightShiftStepsTest(){ //Given int expected1 = 10; From 0b941de92826c74b562025292daa733bbef3011b Mon Sep 17 00:00:00 2001 From: David Ginzberg Date: Fri, 26 May 2017 11:21:59 -0400 Subject: [PATCH 4/5] Update README.md --- README.md | 31 ++++++++++++++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 3aa6b74..4903349 100644 --- a/README.md +++ b/README.md @@ -4,4 +4,33 @@ 2. Write a function that takes a BitSet and returns the number of steps needed to shift all `1` bits to the left (maintaining the same `length()` of the BitSet). -3. Write a function that takes a BitSet and returns the number of steps needed to shift all `1` bits to the right. \ No newline at end of file +3. Write a function that takes a BitSet and returns the number of steps needed to shift all `1` bits to the right. + +## Stack Microlab + +A stack is a data structure that stores a list of elements. Each element is added with the `push(element)` method and the most recent element can be retreived with the `pop()` method. The `peek()` method returns the top element on the stack without removing it from the stack. The **last** element put **in** the stack is the **first** one to come **out** -- this is why it's called a **Last In First Out** (LIFO) data structure. + +Create your own Stack for Strings called `StringStack` and implement these three methods. + +**Part 2**: + +Generalize your StringStack to a generic Stack implementation. It should include appropriate type parameters and allow storage of any object type. + +*hint*: There may be another existing collection that is useful for implementing a stack. + +## Listbuilder + +Create a ListBuilder interface with one method: `buildList(Array a):List` + +Implement the interface and its method with the following classes: + +- `ArrayListBuilder` -- Creates an ArrayList from the given array +- `LinkedListBuilder` -- Creates a LinkedList from the given array + +Part 2: Override `buildList` with `buildList(Collection c):List` to take any collection type and produce the corresponding List type to the implementing class (just like part one). You may want to look at the `Collection.toArray()` method for this. + + +## Iterator + +Create an `Iterator` that produces the Fibonacci series; call it `FibonacciIterator`. The `hasNext()` method should always return true because the Fibonacci series is an infinite set. + From d024a7b899a8a0877ca28e53c020143b45ecd59b Mon Sep 17 00:00:00 2001 From: Chris Date: Fri, 26 May 2017 16:09:57 -0400 Subject: [PATCH 5/5] Finished Stack MicroLab --- .../{ => bitwise}/LightSwitcher.java | 28 ++++----- .../kim/christopher/stack/StringStack.java | 24 ++++++++ .../{ => bitwise}/LightSwitcherTest.java | 5 +- .../christopher/stack/StringStackTest.java | 61 +++++++++++++++++++ 4 files changed, 102 insertions(+), 16 deletions(-) rename src/main/java/kim/christopher/{ => bitwise}/LightSwitcher.java (81%) create mode 100644 src/main/java/kim/christopher/stack/StringStack.java rename src/test/java/kim/christopher/{ => bitwise}/LightSwitcherTest.java (98%) create mode 100644 src/test/java/kim/christopher/stack/StringStackTest.java diff --git a/src/main/java/kim/christopher/LightSwitcher.java b/src/main/java/kim/christopher/bitwise/LightSwitcher.java similarity index 81% rename from src/main/java/kim/christopher/LightSwitcher.java rename to src/main/java/kim/christopher/bitwise/LightSwitcher.java index 06ff201..57c5533 100644 --- a/src/main/java/kim/christopher/LightSwitcher.java +++ b/src/main/java/kim/christopher/bitwise/LightSwitcher.java @@ -1,4 +1,4 @@ -package kim.christopher; +package kim.christopher.bitwise; import java.util.BitSet; @@ -44,14 +44,14 @@ public static BitSet flipAllSwitches(BitSet switches) { } public static int getSwitchPositionAt(BitSet switches, int position) { - return switches.get(position)? 1 : 0; + return switches.get(position) ? 1 : 0; } public static BitSet moveRightBy(BitSet switches, int count) { BitSet result = (BitSet) switches.clone(); - for(int i = 0; i < count; i++){ - for(int j = 1; j < 9; j++){ - if(result.get(j)) + for (int i = 0; i < count; i++) { + for (int j = 1; j < 9; j++) { + if (result.get(j)) result.set(j - 1); else result.set(j - 1, false); } @@ -61,9 +61,9 @@ public static BitSet moveRightBy(BitSet switches, int count) { public static BitSet moveLeftBy(BitSet switches, int count) { BitSet result = (BitSet) switches.clone(); - for(int i = 0; i < count; i++){ - for(int j = 6; j >= 0; j--){ - if(result.get(j)) + for (int i = 0; i < count; i++) { + for (int j = 6; j >= 0; j--) { + if (result.get(j)) result.set(j + 1); else result.set(j + 1, false); } @@ -80,22 +80,22 @@ public static BitSet fromString(final String s) { return BitSet.valueOf(new long[]{Long.parseLong(s, 2)}); } - public static int leftShiftSteps(BitSet bs){ + public static int leftShiftSteps(BitSet bs) { int count = 0; int result = 0; - for(int i = 7; i >= 0; i--){ - if(bs.get(i)) + for (int i = 7; i >= 0; i--) { + if (bs.get(i)) result += count; else count++; } return result; } - public static int rightShiftSteps(BitSet bs){ + public static int rightShiftSteps(BitSet bs) { int count = 0; int result = 0; - for(int i = 0; i < 8; i++){ - if(bs.get(i)) + for (int i = 0; i < 8; i++) { + if (bs.get(i)) result += count; else count++; } diff --git a/src/main/java/kim/christopher/stack/StringStack.java b/src/main/java/kim/christopher/stack/StringStack.java new file mode 100644 index 0000000..bbfa935 --- /dev/null +++ b/src/main/java/kim/christopher/stack/StringStack.java @@ -0,0 +1,24 @@ +package kim.christopher.stack; + +import java.util.LinkedList; + +public class StringStack { + + LinkedList stack; + + public StringStack() { + stack = new LinkedList<>(); + } + + public void push(String input){ + stack.push(input); + } + + public String pop(){ + return stack.pop(); + } + + public String peek(){ + return stack.peek(); + } +} diff --git a/src/test/java/kim/christopher/LightSwitcherTest.java b/src/test/java/kim/christopher/bitwise/LightSwitcherTest.java similarity index 98% rename from src/test/java/kim/christopher/LightSwitcherTest.java rename to src/test/java/kim/christopher/bitwise/LightSwitcherTest.java index 7c765d3..56639cb 100644 --- a/src/test/java/kim/christopher/LightSwitcherTest.java +++ b/src/test/java/kim/christopher/bitwise/LightSwitcherTest.java @@ -1,11 +1,12 @@ -package kim.christopher; +package kim.christopher.bitwise; +import kim.christopher.bitwise.LightSwitcher; import org.junit.Before; import org.junit.Test; import java.util.BitSet; -import static kim.christopher.LightSwitcher.*; +import static kim.christopher.bitwise.LightSwitcher.*; import static org.junit.Assert.assertEquals; public class LightSwitcherTest { diff --git a/src/test/java/kim/christopher/stack/StringStackTest.java b/src/test/java/kim/christopher/stack/StringStackTest.java new file mode 100644 index 0000000..67f8606 --- /dev/null +++ b/src/test/java/kim/christopher/stack/StringStackTest.java @@ -0,0 +1,61 @@ +package kim.christopher.stack; + + +import org.junit.Before; +import org.junit.Test; +import static org.junit.Assert.*; + +public class StringStackTest { + + StringStack myStack; + + @Before + public void initialize() { + myStack = new StringStack(); + } + + @Test + public void pushTest(){ + //Given + String expected = "Chris"; + + //When + myStack.push("Anthony"); + myStack.push("Chris"); + String actual = myStack.peek(); + + //Then + assertEquals("Chris should have been pushed", expected, actual); + } + + @Test + public void popTest(){ + //Given + String expected = "Stephen"; + + //When + myStack.push("Ziggy"); + myStack.push("Stephen"); + String actual = myStack.pop(); + + //Then + assertEquals("Stephen should have been popped", expected, actual); + } + + @Test + public void peekTest(){ + //Given + String expected = "Dennis"; + String expected1 = "Paco"; + + //When + myStack.push("Dennis"); + String actual = myStack.peek(); + myStack.push("Paco"); + String actual1 = myStack.peek(); + + //Then + assertEquals("Dennis should be on top", expected, actual); + assertEquals("Dennis should be on top", expected1, actual1); + } +}