From 670a25492a90fa962094de0c3e0da7fb78aa94de Mon Sep 17 00:00:00 2001 From: "Pedro V.R" Date: Tue, 16 Jan 2024 10:50:01 -0800 Subject: [PATCH 1/3] Completed getIndex method --- .idea/vcs.xml | 6 +++++ LetterInventory.iml | 26 +++++++++++++++++++ src/driver/Driver.java | 3 ++- src/inventory/LetterInventory.java | 32 +++++++++++++++++++++++- tests/inventory/LetterInventoryTest.java | 4 +-- 5 files changed, 67 insertions(+), 4 deletions(-) create mode 100644 .idea/vcs.xml 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..e9c42bd 100644 --- a/src/driver/Driver.java +++ b/src/driver/Driver.java @@ -10,7 +10,8 @@ public static void main(String[] args) { System.out.println(inv); System.out.println(inv.get('e')); - System.out.println(inv.getIndex('e')); + System.out.println(inv.getIndex('b')); + System.out.println(inv.getIndex('z')); } diff --git a/src/inventory/LetterInventory.java b/src/inventory/LetterInventory.java index 18020ba..d93b0af 100644 --- a/src/inventory/LetterInventory.java +++ b/src/inventory/LetterInventory.java @@ -33,6 +33,8 @@ public LetterInventory(){ */ public LetterInventory(String text) { //TODO + + } /** @@ -46,7 +48,26 @@ public LetterInventory(String text) { */ public int getIndex(char c) { //TODO - return 0; + + int num = (int)c; + int index = 0; + + if(num >= 'a' && num <= 'z') + { + index = num - 'a'; + } + + else if(num >= 'A' && num <= 'Z') + { + index = num - 'A'; + } + + else + { + throw new IllegalArgumentException("Received a non alpha character"); + } + + return index; } /** @@ -55,6 +76,15 @@ public int getIndex(char c) { */ public void add(char c) { //TODO + + try + { + inventory[getIndex(c)]++; + } + catch(NullPointerException e) + { + inventory[getIndex(c)] = 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 From 09ea29bde18311142316ef6a700b1232d0b4092c Mon Sep 17 00:00:00 2001 From: "Pedro V.R" Date: Thu, 18 Jan 2024 11:57:37 -0800 Subject: [PATCH 2/3] Completed set method, continue with numbers 7-10 --- src/inventory/LetterInventory.java | 70 ++++++++++++++++++++++++++++-- 1 file changed, 67 insertions(+), 3 deletions(-) diff --git a/src/inventory/LetterInventory.java b/src/inventory/LetterInventory.java index d93b0af..9cd98a6 100644 --- a/src/inventory/LetterInventory.java +++ b/src/inventory/LetterInventory.java @@ -15,6 +15,13 @@ */ public class LetterInventory { + // if this was private int[] inventory - it takes up 32 bits * 26 letter => 832 bits of space + + // if this is private short[] inventory - it takes up 16 bits * 26 letters => 416 bits of space + + // if this is private byte[] inventory - it takes up 8 bits * 26 letters => 208 bits of space + // only want to do this if the letter count < 127 + private short[] inventory; // inventory is null here public static final byte ALPHABET_SIZE = 26; @@ -34,7 +41,20 @@ public LetterInventory(){ public LetterInventory(String text) { //TODO + //This is okay... + //inventory = new short[ALPHABET_SIZE]; + + //This is better, as it calls the default constructor + this(); + for (int i = 0; i < text.length(); i++) + { + char currentChar = text.charAt(i); + if((currentChar >= 'a' && currentChar <= 'z') || (currentChar >= 'A' && currentChar <= 'Z')) + { + add(currentChar); + } + } } /** @@ -83,7 +103,7 @@ public void add(char c) { } catch(NullPointerException e) { - inventory[getIndex(c)] = 0; + inventory[getIndex(c)] = 1; } } @@ -93,6 +113,16 @@ public void add(char c) { */ public void subtract(char c) { //TODO + + try + { + inventory[getIndex(c)]--; + } + catch(NullPointerException e) + { + inventory[getIndex(c)] = 0; + } + } /** @@ -101,7 +131,15 @@ public void subtract(char c) { */ public int get(char c) { //TODO - return 0; + + int index = getIndex(c); + if (index >= 0 && index < ALPHABET_SIZE) { + return inventory[index]; + } + else + { + throw new IllegalArgumentException("Character is not in the a-z or A-Z range"); + } } /** @@ -112,6 +150,25 @@ public int get(char c) { */ public void set(char c, short count) { //TODO + + int index = getIndex(c); + + if(index >= 0 && index < ALPHABET_SIZE) + { + if(count >= 0) + { + inventory[index] = count; + } + else + { + throw new IllegalArgumentException("Count can't be negative"); + } + } + else + { + throw new IllegalArgumentException("Character is not in the a-z or A-Z range"); + } + } /** @@ -130,6 +187,9 @@ public boolean contains(char c) { */ public int size() { //TODO + + + return 0; } @@ -172,7 +232,11 @@ public String toString() { toReturn.append((char) ('a' + i)); } } - return toReturn.append("]").toString(); + //join in the closing ] + toReturn.append("]"); + + //convert the StringBuilder to a String and return it + return toReturn.toString(); } } From d19afab9ee30e030287b9a62309bd2a940c6924c Mon Sep 17 00:00:00 2001 From: "Pedro V.R" Date: Thu, 18 Jan 2024 12:14:52 -0800 Subject: [PATCH 3/3] Completed LetterInventory --- src/inventory/LetterInventory.java | 28 +++++++++++++++++++++++++--- 1 file changed, 25 insertions(+), 3 deletions(-) diff --git a/src/inventory/LetterInventory.java b/src/inventory/LetterInventory.java index 9cd98a6..4586993 100644 --- a/src/inventory/LetterInventory.java +++ b/src/inventory/LetterInventory.java @@ -1,3 +1,9 @@ +/** + * SDEV 301 + * @author Pedro Valdovinos-Reyes, Everett Hanke + * @version 1.0 + */ + package inventory; /** @@ -178,7 +184,17 @@ public void set(char c, short count) { */ public boolean contains(char c) { //TODO - return false; + + int index = getIndex(c); + + if(index >= 0 && index < ALPHABET_SIZE) + { + return inventory[index] > 0; + } + else + { + throw new IllegalArgumentException("Element is not in a-z or A-Z range"); + } } /** @@ -188,9 +204,14 @@ public boolean contains(char c) { public int size() { //TODO + int completeSize = 0; + for(int count : inventory) + { + completeSize += count; + } - return 0; + return completeSize; } /** @@ -199,7 +220,8 @@ public int size() { */ public boolean isEmpty() { // TODO - return false; + + return size() == 0; } /**