import java.util.*;
import java.lang.*;
import java.io.*;
public class Bag {
private List<Integer> data; // Declare a List to store elements
private int maxSize;
public Bag(int maxSize) {
this.data = new ArrayList<>(); // Initialize the List
this.maxSize = maxSize; // Set the maximum size of the Bag
}
public void add(int item) {
if (data.size() < maxSize) { // Check if Bag is full
data.add(item); // Add item to the List
} else {
System.out.println("Bag is full, cannot add more items."); // Print message if Bag is full
}
}
public int getCurrentSize() {
return data.size(); // Return the current number of elements
}
public boolean isFull() {
return data.size() >= maxSize; // Check if the Bag is full
}
public boolean isEmpty() {
return data.isEmpty(); // Check if the Bag is empty
}
public String toString() {
StringBuilder builder = new StringBuilder();
builder.append("Bag contents: ");
for (int i = 0; i < data.size(); i++) {
builder.append(data.get(i)); // Append each element of the List
if (i < data.size() - 1) {
builder.append(", ");
}
}
return builder.toString(); // Convert StringBuilder to String
}
public static void main(String[] args) {
int maxSize = 100;
Bag bag = new Bag(maxSize);
bag.add(5);
bag.add(10);
bag.add(5);
bag.add(20);
System.out.println(bag);
// Print the current size, and check if the Bag is full or empty
System.out.println("Current size: " + bag.getCurrentSize());
System.out.println("Is full: " + (bag.isFull() ? "Yes" : "No"));
System.out.println("Is empty: " + (bag.isEmpty() ? "Yes" : "No"));
}
}
To embed this project on your website, copy the following code and paste it into your website's HTML: