Skip to content

Instantly share code, notes, and snippets.

@anoited007
Last active January 25, 2019 06:56
Show Gist options
  • Save anoited007/37731d38d2960f9fd42b4c2edab48f6a to your computer and use it in GitHub Desktop.
Save anoited007/37731d38d2960f9fd42b4c2edab48f6a to your computer and use it in GitHub Desktop.
A bagable interface to provide functionality for implementing a bag of M and M.
import java.util.Random;
public class Bag<MAndM> implements Bagable<MAndM> {
private boolean empty;
private boolean full;
private boolean isOpen;
private boolean isClosed;
private MAndM[] items;
private int counter;
public Bag(int capacity) {
items = (MAndM[]) new Object[capacity];
empty = true;
full = false;
isClosed = false;
counter = 0;
}
public Bag(){
items = (MAndM[]) new Object[50];
empty = true;
isClosed = false;
full = false;
counter = 0;
}
public void setEmpty(boolean empty) {
this.empty = empty;
}
public void setFull(boolean full) {
this.full = full;
}
public boolean isOpen() {
return isOpen;
}
public void setIsOpen(boolean isOpen) {
this.isOpen = isOpen;
}
public MAndM[] getItems() {
return items;
}
public void setItems(MAndM[] items) {
this.items = items;
}
public int getCounter() {
return counter;
}
public void setCounter(int counter) {
this.counter = counter;
}
public boolean getIsClosed() {
return isClosed;
}
public void setIsClosed(boolean closed) {
isClosed = closed;
}
public void open(){
if(getIsClosed()){
setIsOpen(true);
setIsClosed(false);
System.out.println("The bag is opened");
}
else
System.out.println("The bag is already opened");
}
public void add(MAndM item){
if(counter < items.length -1){
items[counter] = item;
counter++;
}
}
public MAndM remove(MAndM item){
MAndM removed = null;
if(isEmpty()){
System.out.println("The bag is empty");
}
else{
Random random = new Random();
int number = random.nextInt(items.length);
removed = items[number];
items[number] = null; //Problem is how to fill it after removing from the array. Not catered for.
}
return removed;
}
public MAndM[] empty(){
MAndM[] temp = items; // Setting array to return after clearing array.
if(isEmpty()) {
System.out.println("The bag is already empty");
}
else{
items = null;
}
return temp;
}
public boolean isEmpty(){
return empty;
}
public boolean isFull(){
return full;
}
}
public interface Bagable<T> {
//Open the bag
void open();
//Add item to the bag
void add(T t);
//Remove item from the bag
T remove(T t);
//Empty the bag
T[] empty();
//Check if bag is empty
boolean isEmpty();
//Check if bag is full (since we will use Arrays to store data)
boolean isFull();
}
public class MAndM {
private String color;
public MAndM(String color){
this.color = color;
}
public MAndM(){
this.color = "red";
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
}
@stephennaicken
Copy link

Feedback

Great start. Here's some feedback to help improve the code for the next iteration.

The opposite of empty is full and the opposite of closed it open. This means we can use two boolean variables to represent the states of the bag. Here's an example to help:

boolean full = false;

public boolean isFull(){
    return full;
}

public boolean isEmpty(){
    return !full; // negate the full variable and return the value
}

We can actually improve this further and remove the need for a variable to maintain the state of the bag being full or not:

/* the number of items in the array which you need to 
    keep track of using size++ and size-- in 
    add and remove methods */
int size = 0; 

public boolean isFull(){
    return size == capacity; // capacity would need to become an instance variable
}

public boolean isEmpty(){
    return size == 0; // items array is zero
}

Closed (or open) would still require a single boolean value for representation.

Response to comment at line 97: You need to find a way to add items into the first array element which is null.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment