Menu

1. Write a generic Bag class using an array to store any objects. Test the Bag class with a Demo class to store, find, delete, and display objects of Integers and String, respectively.

Code in java, please

1. Write a generic Bag class using an array to store any objects. Test the Bag class with a Demo class to store, find, delete, and display objects of Integers and String, respectively.

2. Write another class called Student, which contains name (String), id (String), and gpa (double) fields. Modify the generic Bag class from the previous problem to store five students with automatically generated id numbers. You will then find, delete, and display any student by his/her id number.

3. Write another class called Faculty, which contains name, id, and salary fields. Modify the generic Bag class from the previous problems, if necessary, to store five faculty and five students, and then find, delete, and display either faculty or student by id number. All id numbers should be automatically generated and there should have no duplicated id values between Students and Faculty.

Expert Answer

Step 1/3
1. The code consists of a generic class Bag and a demo class to test the Bag class. The Bag class uses an array list to store objects of any type. It has the following methods: add method: This method takes an object as a parameter and adds it to the array list. find method: This method takes a string as a parameter and returns the object from the array list that has the same string value.
delete method: This method takes a string as a parameter and deletes the object from the array list that has the same string value. display method: This method displays the contents of the array list. The demo class tests the Bag class by creating an instance of the Bag class, adding integer and string objects to it, finding and deleting an object, and finally displaying the contents of the array list. The demo class shows how the Bag class can be used to store and manipulate objects of any type.
import java.util.ArrayList;
public class Bag<T> {
private ArrayList<T> bagArray;
public Bag() {
this.bagArray = new ArrayList<T>();
}
public void add(T item) {
bagArray.add(item);
}
public T find(T item) {
int index = bagArray.indexOf(item);
if (index == 1) {
return null;
} else {
return bagArray.get(index);
}
}
public boolean delete(T item) {
int index = bagArray.indexOf(item);
if (index == 1) {
return false;
} else {
bagArray.remove(index);
return true;
}
}
public void display() {
System.out.println(bagArray);
}
}
public class Demo {
public static void main(String[] args) {
Bag<Integer> intBag = new Bag<Integer>();
intBag.add(1);
intBag.add(2);
intBag.add(3);
System.out.println(“Int bag: “);
intBag.display();
int item = 2;
System.out.println(“Finding “ + item + “: “ + intBag.find(item));
System.out.println(“Deleting “ + item + “: “ + intBag.delete(item));
System.out.println(“Int bag after deletion: “);
intBag.display();
Bag<String> stringBag = new Bag<String>();
stringBag.add(“hello”);
stringBag.add(“world”);
stringBag.add(“!”);
System.out.println(“String bag: “);
stringBag.display();
String str = “world”;
System.out.println(“Finding “ + str + “: “ + stringBag.find(str));
System.out.println(“Deleting “ + str + “: “ + stringBag.delete(str));
System.out.println(“String bag after deletion: “);
stringBag.display();
}
}

OR