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

ahinkson0342 - Andrew Hinkson #8

Open
wants to merge 6 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
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>
58 changes: 43 additions & 15 deletions src/inventory/LetterInventory.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,47 +31,61 @@ public LetterInventory(){
* and adds each character in the text to the inventory
* @param text
*/
//Fills the elements of the inventory array by looping through a string parameter
public LetterInventory(String text) {
//TODO
this();
for (int i = 0; i < text.length(); i++) {
add(text.charAt(i));
}
}

/**
* Identifies the index for the given character within the inventory array , throws an
* IIegalArgumentException if the character is not in the a-z or A-Z range.
* IllegalArgumentException if the character is not in the a-z or A-Z range.
* For example: if the given character is 'c' or 'C', then the index returned is 2
* if the given character is '?', then an IllegalArgumentException is thrown
*
* @param c a-z or A-Z character
* @return index of the character
*/
//Gets the index where a character c would belong within the inventory array, which
// is 26 elements in alphabetical order. if the given character is not an alpha character,
// it throws an IllegalArgumentException.
public int getIndex(char c) {
//TODO
return 0;
if((int)Character.toLowerCase(c) >=97 && (int)Character.toLowerCase(c) <= 122) {
return (int) Character.toLowerCase(c) - 'a';
}
else
{
throw new IllegalArgumentException("Please enter a valid input (a-z)");
}
}

/**
* Increases the count for the given character in the inventory
* @param c a-z or A-Z otherwise an IllegalArgumentException is thrown
*/
//Adds a given character c to increment the appropriate index in the inventory array
public void add(char c) {
//TODO
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
*/
//decrements the appropriate index for the given character c within the inventory array
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
*/
//gets the index of a given character c
public int get(char c) {
//TODO
return 0;
return inventory[getIndex(c)];
}

/**
Expand All @@ -80,36 +94,50 @@ public int get(char c) {
* @param count the number of occurrences of the character c; if count < 0
* IllegalArgumentException is thrown
*/
//sets the value of the index for a given character c to a value "count" given as a parameter. If the character is
//not alpha or if the count is less than 0, it throws an IllegalArgumentException
public void set(char c, short count) {
//TODO
if((int)Character.toLowerCase(c) >=97 && (int)Character.toLowerCase(c) <= 122 && count>=0)
{
inventory[getIndex(c)] = count;
}
else
{
throw new IllegalArgumentException("Please enter a valid input (a-z) and count must be a positive integer.");
}
}

/**
* Determines if a character's count is in the inventory
* @param c a-z or A-Z otherwise an IllegalArgumentException is thrown
* @return true if character is in inventory, false otherwise
*/
//Finds the given character c within the inventory array, and returns the index of it.
public boolean contains(char c) {
//TODO
return false;
return inventory[getIndex(c)] > 0;
}

/**
* Return the total count of all letters in the inventory
* @return total count
*/
//returns the size of the string that was passed in
public int size() {
//TODO
return 0;
short count = 0;
for (int i = 0; i < inventory.length; i++)
{
count += inventory[i];
}
return count;
}

/**
* Determine if the inventory has zero counts for all letters
* @return true, if empty, false otherwise
*/
//checks if the string that was originally passed in is empty
public boolean isEmpty() {
// TODO
return false;
return size() == 0;
}

/**
Expand Down
52 changes: 52 additions & 0 deletions src/inventory/LetterPresence.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package inventory;

/*
* [2,0,0,0,1,0,1,1,1,0,0,0,0,2,1,0,0,0,2,3,0,0,1,0,0,0] --> inventory count array
* [a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z] --> corresponding letters
*
* [1,0,0,0,1,0,1,1,1,0,0,0,0,1,1,0,0,0,1,1,0,0,1,0,0,0] --> presence count array
* [a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z] --> corresponding letters
*
* can use an int to represent this -- bitwise ops
* zyxwvutsrqponmlkjihgfedcba
* 00010011000110000111010001 -- some bits left off of start since we are only using 26
*/

public class LetterPresence {
private int inventory;

public int getIndex(char letter)
{
//assume letter is valid and lowercase
return letter - 'a';
}

public void add(char letter)
{
int position = getIndex(letter);
/*
<< or >> operator shifts position of number
ex: 1 << 4 makes 10000
0000001 -- 0010000
*/
int temp = 1 << position;
inventory += temp;
}

public boolean isPresent(char letter)
{
int position = getIndex(letter);
int temp = 1 << position;
/* ANDs the two numbers together - checks if any of the values match
* 0110000 & 1 << 4
* 0010000
* 0010000 - AND result
*
* 0110000 & 1 << 2
* 0110100
* 0000000 - AND result
*/
int check = inventory & temp;
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());
}
*/

}