Skip to content
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
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>
3 changes: 2 additions & 1 deletion src/driver/Driver.java
Original file line number Diff line number Diff line change
Expand Up @@ -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'));


}
Expand Down
128 changes: 122 additions & 6 deletions src/inventory/LetterInventory.java
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
/**
* SDEV 301
* @author Pedro Valdovinos-Reyes, Everett Hanke
* @version 1.0
*/

package inventory;

/**
Expand All @@ -15,6 +21,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;

Expand All @@ -33,6 +46,21 @@ 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);
}
}
}

/**
Expand All @@ -46,7 +74,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;
}

/**
Expand All @@ -55,6 +102,15 @@ public int getIndex(char c) {
*/
public void add(char c) {
//TODO

try
{
inventory[getIndex(c)]++;
}
catch(NullPointerException e)
{
inventory[getIndex(c)] = 1;
}
}

/**
Expand All @@ -63,6 +119,16 @@ public void add(char c) {
*/
public void subtract(char c) {
//TODO

try
{
inventory[getIndex(c)]--;
}
catch(NullPointerException e)
{
inventory[getIndex(c)] = 0;
}

}

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

/**
Expand All @@ -82,6 +156,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");
}

}

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

/**
Expand All @@ -100,7 +203,15 @@ public boolean contains(char c) {
*/
public int size() {
//TODO
return 0;

int completeSize = 0;

for(int count : inventory)
{
completeSize += count;
}

return completeSize;
}

/**
Expand All @@ -109,7 +220,8 @@ public int size() {
*/
public boolean isEmpty() {
// TODO
return false;

return size() == 0;
}

/**
Expand Down Expand Up @@ -142,7 +254,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();
}

}
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());
}
*/

}