Skip to content

Instantly share code, notes, and snippets.

Created April 3, 2017 04:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save anonymous/9101b53ebd59250843a2752b02f4c14f to your computer and use it in GitHub Desktop.
Save anonymous/9101b53ebd59250843a2752b02f4c14f to your computer and use it in GitHub Desktop.
Question for Hadyn
/*
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 interface BagInterface<MyType> {
//adds a new entry and returns true or false depending if the addition completed or not
public boolean add(MyType newEntry);
//removes a specific entry and returns true or false depending if the removal completed or not
public boolean remove(MyType 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(MyType anEntry);
//stores all entries in an array
public MyType[] toArray();
}
/*
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){
ClothingBag(10);
}
}
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:
*/
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