diff --git a/.idea/misc.xml b/.idea/misc.xml index 6f29fee..69ace3f 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -1,6 +1,6 @@ - + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..35eb1dd --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/LetterInventory.iml b/LetterInventory.iml index 0f07f6c..943503e 100644 --- a/LetterInventory.iml +++ b/LetterInventory.iml @@ -24,5 +24,31 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/src/driver/Driver.java b/src/driver/Driver.java index 51cd3d9..c0f9b78 100644 --- a/src/driver/Driver.java +++ b/src/driver/Driver.java @@ -1,17 +1,51 @@ package driver; +/* + Everett Hanke + SDEV 301 + 01/16/24 + */ import inventory.LetterInventory; +import inventory.LetterInventoryBits; public class Driver { - public static void main(String[] args) { + public static void main(String[] args) + { LetterInventory inv = new LetterInventory(); - //LetterInventory inv = new LetterInventory("WashingtonState"); - System.out.println(inv); + LetterInventory inv1 = new LetterInventory("WashingtonState"); + //System.out.println(inv1); + System.out.println("Retrieving index for Z: " + inv.getIndex('Z')); - System.out.println(inv.get('e')); - System.out.println(inv.getIndex('e')); + System.out.println("Retrieving count for e: "+ inv.get('e')); + System.out.println("Retrieving index for e: " + inv.getIndex('e')); + //lets add 3 different letters + inv.add('e'); + inv.add('e'); + inv.add('l'); + //count out how many e are in the inventory + System.out.println("Retrieving count for e: " + inv.get('e')); + //inv.set('-', (short) 9); + System.out.println("Retrieving size of list: " + inv.size()); + LetterInventory testingByKen = new LetterInventory("washingtonstate"); + System.out.println(testingByKen.toString()); + + //************************************************************************* + //testing bitwise letter inventory below + LetterInventoryBits inv2 = new LetterInventoryBits(); + //play arround with how much you want to shift and see the changes 32/12/4/etc do you see it wrap? + int temp = 1 << 32; + System.out.println("Testing shift 4: " + temp); + + inv2.add('e'); + inv2.add('p'); + inv2.add('z'); + System.out.println(inv2.isPresent('l')); + System.out.println(inv2.isPresent('e')); + System.out.println(inv2.isPresent('g')); + inv2.add('g'); + System.out.println(inv2.isPresent('g')); } } diff --git a/src/inventory/LetterInventory.java b/src/inventory/LetterInventory.java index 18020ba..3efae29 100644 --- a/src/inventory/LetterInventory.java +++ b/src/inventory/LetterInventory.java @@ -15,6 +15,12 @@ */ public class LetterInventory { + //if inventory was an int[] (int being 32 bits) then it takes up 832 bits of space + //that is due to 32*26. (26 being the letters in the alphabet. + //if inventory is a short[] then it is 16 bits + //16 bits times 26 bits => 416 bits of space which is half the size being saved! + //if this private operator was a byte[] it takes up 8 bits * 26 => 208 bits of space + //you'll only ever want to use bit[] if the letter count is below 127. private short[] inventory; // inventory is null here public static final byte ALPHABET_SIZE = 26; @@ -31,8 +37,14 @@ public LetterInventory(){ * and adds each character in the text to the inventory * @param text */ - public LetterInventory(String text) { - //TODO + public LetterInventory(String text) throws ArrayIndexOutOfBoundsException + { + inventory = new short[ALPHABET_SIZE]; + for (int i = 0; i < text.length(); i++) + { + add(text.charAt(i)); + System.out.println("adding letter " + text.charAt(i)); + } } /** @@ -45,33 +57,52 @@ public LetterInventory(String text) { * @return index of the character */ public int getIndex(char c) { - //TODO - return 0; + int num = c; + int index; + if (num >= 'a' && num <= 'z') + { + index = num-'a'; + } + else if (num >= 'A' && num <= 'Z') + { + index = num-'A'; + } + else + { + throw new IllegalArgumentException("Received a non alphanumeric number"); + } + return index; } /** * Increases the count for the given character in the inventory * @param c a-z or A-Z otherwise an IllegalArgumentException is thrown */ - public void add(char c) { -//TODO + public void add(char c) + { + + inventory[getIndex(c)]++; + } /** * Decreases the count for the given character in the inventory * @param c a-z or A-Z otherwise an IllegalArgumentException is thrown */ - public void subtract(char c) { + public void subtract(char c) + { //TODO + inventory[getIndex(c)]--; } /** * Returns the count for the given character in the inventory * @param c a-z or A-Z otherwise an IllegalArgumentException is thrown */ - public int get(char c) { - //TODO - return 0; + public int get(char c) + { + int num = inventory[getIndex(c)]; + return num; } /** @@ -81,7 +112,14 @@ public int get(char c) { * IllegalArgumentException is thrown */ public void set(char c, short count) { - //TODO + if (count >= 0) + { + inventory[getIndex(c)] = count; + } + else + { + throw new IllegalArgumentException("count must be greater equal to or greater than 0"); + } } /** @@ -91,25 +129,50 @@ public void set(char c, short count) { */ public boolean contains(char c) { //TODO - return false; + if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')) + { + if (get(c) > 0) + { + return true; + } + else + { + return false; + } + } + else + { + throw new IllegalArgumentException(); + } } /** * Return the total count of all letters in the inventory * @return total count */ - public int size() { - //TODO - return 0; + public int size() + { + int total = 0; + for (int i = 0; i < inventory.length ; i++) + { + total += inventory[i]; + } + System.out.println(total); + return total; } /** * Determine if the inventory has zero counts for all letters * @return true, if empty, false otherwise */ - public boolean isEmpty() { - // TODO + public boolean isEmpty() + { + if (size() == 0) + { + return true; + } return false; + } /** @@ -142,7 +205,8 @@ public String toString() { toReturn.append((char) ('a' + i)); } } - return toReturn.append("]").toString(); + toReturn.append("]"); + return toReturn.toString(); } } diff --git a/src/inventory/LetterInventoryBits.java b/src/inventory/LetterInventoryBits.java new file mode 100644 index 0000000..7147089 --- /dev/null +++ b/src/inventory/LetterInventoryBits.java @@ -0,0 +1,39 @@ +package inventory; + +public class LetterInventoryBits +{ + private int inventory; //this will be a small inventory using bits and bytes + public int getIndex(char letter) + { + return letter - 'a'; + } + + public void add(char letter) + { + int position = getIndex(letter); + int temp = 1 << position; + inventory += temp; + System.out.println("Current inventory num: "+ inventory); + } + + public boolean isPresent(char letter) + { + //we will get position of letter then create a temp number of that bit number + int position = getIndex(letter); + //below is how we place that bit num. we will cross reference it with inventory now + int temp = 1 << position; + + int check = inventory & temp; // "&" is actually a bitwise AND + /* + we may have two ints bits and compare now + first: 0001001000 + second:0010111000 + &: ___________ + result:0000001000 + note this is & so there must be two 1's in order for the result to retrieve a 1. this will be considered true + */ + //then we return check over 0 because if there is any trues then we will have a 1 + return check > 0; + } + +} diff --git a/tests/inventory/LetterInventoryTest.java b/tests/inventory/LetterInventoryTest.java index f243715..6ce856b 100644 --- a/tests/inventory/LetterInventoryTest.java +++ b/tests/inventory/LetterInventoryTest.java @@ -6,7 +6,7 @@ import static org.junit.jupiter.api.Assertions.*; class LetterInventoryTest { -/* + static LetterInventory washington; static LetterInventory empty; static LetterInventory atoz; @@ -125,5 +125,5 @@ void isEmpty() { assertFalse(atoz.isEmpty()); assertFalse(washington.isEmpty()); } -*/ + } \ No newline at end of file