-
-
Save Hadyn/f4ee790a4131f2a834d16a5aa68c9712 to your computer and use it in GitHub Desktop.
Question for Hadyn
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Name: Gino Di Gregorio | |
Project Name: CSC400-Module 2 Clothing Boutique | |
Project Purpose: Understanding working with bags by implementing a class that implements a baginterface class | |
Algorithm Used: implementing a class called ClothingBag that implements the BagInterface<MyType> | |
Program Inputs: | |
Program Outputs: | |
Program Limitations: | |
Program Errors: | |
*/ | |
// Hadyn: NEVER make generics more than one letter. Also name them after the first letter of what they imply. | |
public interface BagInterface<T> { | |
//adds a new entry and returns true or false depending if the addition completed or not | |
public boolean add(T newEntry); | |
//removes a specific entry and returns true or false depending if the removal completed or not | |
public boolean remove(T newEntry); | |
//removes an arbitrary entry and returns true or false depending if the removal completed or not | |
public boolean remove(); | |
//checks if bag is empty | |
public boolean isEmpty(); | |
//removes all entries from bag | |
public void clear(); | |
//returns current number of entries in bag | |
public int getCurrentSize(); | |
//tests if the bag contains a specific entry | |
public boolean contains(T anEntry); | |
//stores all entries in an array | |
public T[] toArray(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Name: Gino Di Gregorio | |
Project Name: CSC400-Module 2 Clothing Boutique | |
Project Purpose: Understanding working with bags by implementing a class that implements a baginterface class | |
Algorithm Used: implementing a class called ClothingBag that implements the BagInterface<MyType> | |
Program Inputs: | |
Program Outputs: | |
Program Limitations: | |
Program Errors: | |
*/ | |
public class BoutiqueBagDemo { | |
public static void main(String[] args){ | |
// Hadyn: You didnt call the constructor you dummy | |
BagInterface<Integer> bag = new ClothingBag<Integer>(10); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Arrays; | |
/* | |
Name: Gino Di Gregorio | |
Project Name: CSC400-Module 2 Clothing Boutique | |
Project Purpose: Understanding working with bags by implementing a class that implements a baginterface class | |
Algorithm Used: implementing a class called ClothingBag that implements the BagInterface<MyType> | |
Program Inputs: | |
Program Outputs: | |
Program Limitations: | |
Program Errors: | |
*/ | |
// Hadyn: AGAIN IN THIS FILE RENAME MyType TO T | |
public class ClothingBag<MyType> implements BagInterface<MyType> { | |
MyType[] bag; | |
int numberOfEntries; | |
int size; | |
static final int DEFAULT_SIZE = 2; | |
static final int MAX_SIZE = 500; | |
//create default bag with default size | |
public ClothingBag(){ | |
this(DEFAULT_SIZE); | |
} | |
//create bag with specific size | |
public ClothingBag(int size) { | |
checkSize(size); | |
MyType[] tempBag = (MyType[])new Object[size]; | |
bag = tempBag; | |
numberOfEntries = 0; | |
} | |
public ClothingBag(MyType[] contents) { | |
checkSize(contents.length); | |
bag = Arrays.copyOf(contents, contents.length); | |
numberOfEntries = contents.length; | |
} | |
private void checkSize(int size) { | |
if(size > MAX_SIZE) | |
throw new IllegalStateException("Desired bag size exceeds maximum bag size of " + MAX_SIZE); | |
} | |
//adds a new entry to bag | |
@Override | |
public boolean add(MyType newEntry) { | |
// TODO Auto-generated method stub | |
if(isArrayFull()) { | |
doubleSize(); | |
} | |
bag[numberOfEntries] = newEntry; | |
numberOfEntries++; | |
return true; | |
} | |
private void doubleSize() { | |
int newLength = 2 * bag.length; | |
checkSize(newLength); | |
bag = Arrays.copyOf(bag, newLength); | |
} | |
//removes one instance of specified entry | |
@Override | |
public boolean remove(MyType newEntry) { | |
// TODO Auto-generated method stub | |
int index = getIndexOf(newEntry); | |
MyType result = removeEntry(index); | |
return newEntry.equals(result); | |
} | |
//removes random entry | |
@Override | |
public MyType remove() { | |
// TODO Auto-generated method stub | |
MyType result = removeEntry(numberOfEntries - 1); | |
return result; | |
} | |
private MyType removeEntry(int givenIndex) { | |
MyType result = null; | |
if(!isEmpty() && givenIndex >= 0) { | |
result = bag[givenIndex]; | |
int lastIndex = numberOfEntries - 1; | |
bag[givenIndex] = bag[lastIndex]; | |
bag[lastIndex] = null; | |
numberOfEntries--; | |
} | |
return result; | |
} | |
//tests if bag is empty | |
@Override | |
public boolean isEmpty() { | |
// TODO Auto-generated method stub | |
return numberOfEntries == 0; | |
} | |
//removes all entries in bag | |
@Override | |
public void clear() { | |
// TODO Auto-generated method stub | |
while(!isEmpty()) | |
remove(); | |
} | |
//returns current number of entries in bag | |
@Override | |
public int getCurrentSize() { | |
// TODO Auto-generated method stub | |
return numberOfEntries; | |
} | |
//tests if bag contains a certain entry | |
@Override | |
public boolean contains(MyType anEntry) { | |
// TODO Auto-generated method stub | |
return getIndexOf(anEntry) > -1; | |
} | |
private boolean isArrayFull() { | |
return numberOfEntries >= bag.length; | |
} | |
//stores all entries in bag to an array | |
@Override | |
public MyType[] toArray() { | |
// TODO Auto-generated method stub | |
MyType[] result = (MyType[])new Object[numberOfEntries]; | |
for(int index = 0; index < numberOfEntries; index++) { | |
result[index] = bag[index]; | |
} | |
return result; | |
} | |
private int getIndexOf(MyType anEntry){ | |
int where = -1; | |
boolean found = false; | |
int index = 0; | |
while(!found && (index < numberOfEntries)) { | |
if(anEntry.equals(bag[index])) { | |
found = true; | |
where = index; | |
} | |
index++; | |
} | |
return where; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment