Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Letter Inventory Complete #14

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 26 additions & 0 deletions LetterInventory.iml
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,31 @@
<SOURCES />
</library>
</orderEntry>
<orderEntry type="module-library" scope="TEST">
<library name="JUnit4">
<CLASSES>
<root url="jar://$MAVEN_REPOSITORY$/junit/junit/4.13.1/junit-4.13.1.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/hamcrest/hamcrest-core/1.3/hamcrest-core-1.3.jar!/" />
</CLASSES>
<JAVADOC />
<SOURCES />
</library>
</orderEntry>
<orderEntry type="module-library" scope="TEST">
<library name="JUnit5.8.1">
<CLASSES>
<root url="jar://$MAVEN_REPOSITORY$/org/junit/jupiter/junit-jupiter/5.8.1/junit-jupiter-5.8.1.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/junit/jupiter/junit-jupiter-api/5.8.1/junit-jupiter-api-5.8.1.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/opentest4j/opentest4j/1.2.0/opentest4j-1.2.0.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/junit/platform/junit-platform-commons/1.8.1/junit-platform-commons-1.8.1.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/apiguardian/apiguardian-api/1.1.2/apiguardian-api-1.1.2.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/junit/jupiter/junit-jupiter-params/5.8.1/junit-jupiter-params-5.8.1.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/junit/jupiter/junit-jupiter-engine/5.8.1/junit-jupiter-engine-5.8.1.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/junit/platform/junit-platform-engine/1.8.1/junit-platform-engine-1.8.1.jar!/" />
</CLASSES>
<JAVADOC />
<SOURCES />
</library>
</orderEntry>
</component>
</module>
44 changes: 39 additions & 5 deletions src/driver/Driver.java
Original file line number Diff line number Diff line change
@@ -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'));
}
}
100 changes: 82 additions & 18 deletions src/inventory/LetterInventory.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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));
}
}

/**
Expand All @@ -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;
}

/**
Expand All @@ -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");
}
}

/**
Expand All @@ -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;

}

/**
Expand Down Expand Up @@ -142,7 +205,8 @@ public String toString() {
toReturn.append((char) ('a' + i));
}
}
return toReturn.append("]").toString();
toReturn.append("]");
return toReturn.toString();
}

}
39 changes: 39 additions & 0 deletions src/inventory/LetterInventoryBits.java
Original file line number Diff line number Diff line change
@@ -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;
}

}
4 changes: 2 additions & 2 deletions tests/inventory/LetterInventoryTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import static org.junit.jupiter.api.Assertions.*;

class LetterInventoryTest {
/*

static LetterInventory washington;
static LetterInventory empty;
static LetterInventory atoz;
Expand Down Expand Up @@ -125,5 +125,5 @@ void isEmpty() {
assertFalse(atoz.isEmpty());
assertFalse(washington.isEmpty());
}
*/

}