assignments

This commit is contained in:
2025-04-09 02:32:02 +03:00
parent ec083c6254
commit f0b5074ac7
14 changed files with 798 additions and 0 deletions

Binary file not shown.

View 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]+ " ");
}
}
}

View 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();
}
}