assignments
This commit is contained in:
BIN
assignments/AssigmentS3/StorageBox.class
Normal file
BIN
assignments/AssigmentS3/StorageBox.class
Normal file
Binary file not shown.
44
assignments/AssigmentS3/StorageBox.java
Normal file
44
assignments/AssigmentS3/StorageBox.java
Normal file
@ -0,0 +1,44 @@
|
||||
public class StorageBox<T> {
|
||||
|
||||
private T[] items;
|
||||
private int size;
|
||||
private int capacity;
|
||||
|
||||
public StorageBox(int capacity) {
|
||||
this.capacity = capacity;
|
||||
this.size = 0;
|
||||
this.items = (T[]) new Object[capacity];
|
||||
}
|
||||
|
||||
public void addItem(T item) {
|
||||
if(capacity == size) {
|
||||
System.out.println("StorageBox is full");
|
||||
return;
|
||||
} else {
|
||||
items[size] = item;
|
||||
size++;
|
||||
System.out.println("Item added: "+ item);
|
||||
}
|
||||
}
|
||||
|
||||
public T removeItem() {
|
||||
if(size == 0) {
|
||||
System.out.println("No more item left!");
|
||||
return null;
|
||||
} else {
|
||||
T temp = items[size - 1];
|
||||
items[size - 1] = null;
|
||||
size--;
|
||||
System.out.println("Removed Item: "+ temp);
|
||||
return temp;
|
||||
}
|
||||
}
|
||||
|
||||
public void listItem() {
|
||||
System.out.println("Items in StorageBox: ");
|
||||
for(int i = 0; i < size; i++) {
|
||||
System.out.print(items[i]+ " ");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
36
assignments/AssigmentS3/demo.java
Normal file
36
assignments/AssigmentS3/demo.java
Normal file
@ -0,0 +1,36 @@
|
||||
public class demo {
|
||||
public static void main(String[] args) {
|
||||
StorageBox<String> box = new StorageBox<>(3);
|
||||
StorageBox<Integer> box1 = new StorageBox<>(3);
|
||||
|
||||
box.addItem("Math");
|
||||
box.addItem("Java");
|
||||
box.addItem("Statistics");
|
||||
box.addItem("capacityTest");
|
||||
|
||||
System.out.println();
|
||||
|
||||
box.listItem();
|
||||
|
||||
System.out.println(" ");
|
||||
System.out.println();
|
||||
|
||||
box.removeItem();
|
||||
box.removeItem();
|
||||
box.removeItem();
|
||||
box.removeItem();
|
||||
box.removeItem();
|
||||
box.removeItem();
|
||||
|
||||
System.out.println();;
|
||||
|
||||
box.listItem();
|
||||
|
||||
box1.addItem(1);
|
||||
box1.addItem(2);
|
||||
box1.addItem(3);
|
||||
box1.addItem(4);
|
||||
|
||||
box1.listItem();
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user