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

LetterInventory Completed #3

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
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.

15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# LetterInventory

### Description
This repository holds my submission to an assignment for SDEV301 Systems Programming at Green River College. The purpose of this assignment was to become familiar with the memory impact of using bytes, shorts, and ints via creating methods for `LetterInventory`.

`LetterInventory` is a class that has two constructors - one that takes in no arguments, and one that takes in a String argument. The class keeps track of private inventory, a short[] that keeps track of how many letter counts a string has (case-insensitive).

Each letter should correspond to an index (i.e. a -> 0, b -> 1, c -> 2, d -> 3, and so on).


For example:
- "WashingtonState" would result in `[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]`, which wouuld print out as `[aaeghinnosstttw]`.

### How to Run the Project
This code can be run as-is with the IntelliJ IDEA IDE from Jetbrains, as long as [Java has been installed](https://www.oracle.com/java/technologies/downloads/) locally.
15 changes: 12 additions & 3 deletions src/driver/Driver.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,22 @@
public class Driver {

public static void main(String[] args) {
LetterInventory inv = new LetterInventory();
//LetterInventory inv = new LetterInventory("WashingtonState");
LetterInventory inv1 = new LetterInventory();
System.out.println(inv1);

LetterInventory inv = new LetterInventory("WashingtonState");
System.out.println(inv);

System.out.println(inv.get('e'));
System.out.println(inv.getIndex('e'));
System.out.println(inv.getIndex('e')); //4
System.out.println(inv.getIndex('Z')); //25
// System.out.println(inv.getIndex('1')); //Throw exception

//Test add();
inv.add('a');
System.out.println(inv);

int temp = 1 << 4;
System.out.println("testing bit shift: " + temp);
}
}
74 changes: 59 additions & 15 deletions src/inventory/LetterInventory.java
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
/**
* SDEV 301: Systems Programming
*
* @author Kendrick Hang, Tien Han
* @version 1.0
*/

package inventory;

/**
Expand All @@ -14,7 +21,13 @@
*
*/
public class LetterInventory {

//If this is private int[] inventory - it takes up 32 bits * 26 letters => 832 bits of space
//If this is private short[] inventory - it takes upu 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 use byte[] if the letter count < 256
//Because 256 is the biggest value you can store with 8 bits
//Which is 64 (2^6) + 32 (2^5) + 16 (2^4) + 8 (2^3) + 4 (2^2) + 2 (2^1) + 1 (2^0)
//127 bytes per spot in the array (example: so we can only support "a" up to 127)
private short[] inventory; // inventory is null here
public static final byte ALPHABET_SIZE = 26;

Expand All @@ -32,7 +45,14 @@ public LetterInventory(){
* @param text
*/
public LetterInventory(String text) {
//TODO
//this() will call the constructor LetterInventory() to
//initialize our inventory short array.
this();

for (int i = 0; i < text.length(); i++) {
char letter = text.charAt(i);
add(letter);
}
}

/**
Expand All @@ -45,33 +65,45 @@ public LetterInventory(String text) {
* @return index of the character
*/
public int getIndex(char c) {
//TODO
return 0;
if ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z')) {
//Subtract 97 as "a" starts at 97 and we want the index to start at 0
int index = (int) c - 97;
//Adjust for capital letters
if (index < 0) {
return index + 32;
}
return index;
} else {
throw new IllegalArgumentException("The given character " + c + " was a non alpha character.");
}
}

/**
* 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
int index = getIndex(c);
int newCount = this.inventory[index] + 1;
this.inventory[index] = (short) newCount;
}

/**
* 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) {
//TODO
int index = getIndex(c);
int newCount = this.inventory[index] - 1;
this.inventory[index] = (short) newCount;
}

/**
* 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;
return this.inventory[getIndex(c)];
}

/**
Expand All @@ -81,7 +113,11 @@ public int get(char c) {
* IllegalArgumentException is thrown
*/
public void set(char c, short count) {
//TODO
if (count < 0) {
throw new IllegalArgumentException("The given count " + count + " is less than 0.");
}

this.inventory[getIndex(c)] = count;
}

/**
Expand All @@ -90,26 +126,32 @@ public void set(char c, short count) {
* @return true if character is in inventory, false otherwise
*/
public boolean contains(char c) {
//TODO
return false;
return this.inventory[getIndex(c)] > 0;
}

/**
* Return the total count of all letters in the inventory
* @return total count
*/
public int size() {
//TODO
return 0;
int counter = 0;
for (short count : this.inventory) {
counter += count;
}
return counter;
}

/**
* Determine if the inventory has zero counts for all letters
* @return true, if empty, false otherwise
*/
public boolean isEmpty() {
// TODO
return false;
for (short count : this.inventory) {
if (count > 0) {
return false;
}
}
return true;
}

/**
Expand Down Expand Up @@ -142,6 +184,8 @@ public String toString() {
toReturn.append((char) ('a' + i));
}
}
//Join in the closing "]" and convert the StringBuilder
//to a String and return it
return toReturn.append("]").toString();
}

Expand Down
42 changes: 42 additions & 0 deletions src/inventory/LetterPresence.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/**
* SDEV 301: Systems Programming
*
* @author Kendrick Hang, Tien Han
* @version 1.0
*/

package inventory;

public class LetterPresence {
private int inventory;

public int getIndex(char letter) {
//assume letter is valid and lowercase
return letter - 'a';
//letter a returns 0, letter b returns 1, letter c returns 2
}

public void add(char letter) { //ex: e ||||| f
int position = getIndex(letter); //4 ||||| 5
int temp = 1 << position; //1 << 4 is bit shift (00000001 becomes 00010000) ||||| 00100000
//00000001
//00010000 -> shifted 4 places
this.inventory += temp; //Add to temp to keep track of all "1"'s
}

public boolean isPresent(char letter) {
int position = getIndex(letter);
int temp = 1 << position;
int check = inventory & temp; //bitwise and
//Checking for e
//inv -> 0110000
//temp -> 0010000
//check-> 0010000 -> this is saying that there is one e
//Checking for g
//inv -> 0110000
//temp -> 10000000
//check-> 00000000

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

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