Question: Need help in java please!!!! In this homework, you will implement the merge ( ) method to merge two linked bags in Java using the

Need help in java please!!!!
In this homework, you will implement the merge() method to merge two linked bags in Java using the
provided LinkedBag class. You will also test your implementation to ensure the merged bag contains
all the elements from both bags, including duplicates.
The starter project is provided for the LinkedBag class. The class includes a method for adding
elements to the bag. Your task is to complete the merge(LinkedBag other) method in this class,
which takes another bag as an argument and merges it with the current bag.
You are required to run the GradingRubric class to make sure your method works as intended.
You are required to:
Understand the provided LinkedBag class.
Implement the merge(LinkedBag other) method, which should merge the current bag with the
provided bag other.
1
Ensure your merge() method works in place, modifying the linked structure without creating addi-
tional arrays or collections.
The merged bag should allow duplicates from both bags.
5Example
Given the following two linked bags:
Bag 1: 1,2,3
Bag 2: 3,4,5
After calling merge(), the result should be:
Merged Bag: 1,2,3,3,4,
BagInterface.java
public interface BagInterface {
boolean add(T newEntry);
int getCurrentSize();
boolean isEmpty();
}
Main.java
public class Main {
public static void main(String[] args){
// init an instance of a bag.
LinkedBag myLBag = new LinkedBag<>();
}
}
LinkedBag.java
public class LinkedBag implements BagInterface {
// node class
private class Node {
// instance variables
T data;
Node next;
// constructor
public Node(T data, Node next){
this.data = data;
this.next = next;
}
}
//instance variables
private Node firstNode;
private int numberOfEntries;
public LinkedBag(){
firstNode = null;
numberOfEntries =0;
}
public String print_list(){
Node temp = firstNode;
String result ="";
while (temp != null){
result += temp.data +"==>";
temp = temp.next;
}
result += "null";
return result;
}
@Override
public boolean add(T newEntry){
Node newNode = new Node(newEntry, null);
newNode.next = firstNode;
firstNode = newNode;
this.numberOfEntries++;
return true;
}
@Override
public int getCurrentSize(){
return numberOfEntries;
}
@Override
public boolean isEmpty(){
return numberOfEntries ==0;
}
public void merge(LinkedBag other){
// TODO
}
}
GradingRubric.java
public class GradingRubric {
public static void main(String[] args){
int totalPoints =0;
// Test 1: Merging two non-empty bags (Simple Case)
LinkedBag bag1= new LinkedBag<>();
LinkedBag bag2= new LinkedBag<>();
bag1.add(1);
bag1.add(2);
bag1.add(3);
bag2.add(4);
bag2.add(5);
bag2.add(6);
bag1.merge(bag2);
if (bag1.print_list().equals("3==>2==>1==>6==>5==>4==> null")){
System.out.println("Test 1 passed");
totalPoints +=10;
} else {
System.out.println("Test 1 failed");
}
// Test 2: Merging a non-empty bag with an empty bag
LinkedBag bag3= new LinkedBag<>();
LinkedBag bag4= new LinkedBag<>();
bag3.add(1);
bag3.add(2);
bag3.add(3);
bag3.merge(bag4);
if (bag3.print_list().equals("3==>2==>1==> null")){
System.out.println("Test 2 passed");
totalPoints +=10;
} else {
System.out.println("Test 2 failed");
}
// Test 3: Merging an empty bag with a non-empty bag
LinkedBag bag5= new LinkedBag<>();
LinkedBag bag6= new LinkedBag<>();
bag6.add(4);
bag6.add(5);
bag6.add(6);
bag5.merge(bag6);
if (bag5.print_list().equals("6==>5==>4==> null")){
System.out.println("Test 3 passed");
totalPoints +=10;
} else {
System.out.println("Test 3 failed");
}
// Test 4: Merging two empty bags
LinkedBag bag7= new LinkedBag<>();
LinkedBag bag8= new LinkedBag<>();
bag7.merge(bag8);
if (bag7.print_list().equals("null")){
System.out.println("Test 4 passed");
totalPoints +=10;
} else {
System.out.println("Test 4 failed");
}
// NEW Test 5: Merging bags with non-overlapping elements
LinkedBag bag9= new LinkedBag<>();
LinkedBag bag10= new LinkedBag<>();
bag9.add(100);
bag9.add(200);
bag10.add(300);
bag1

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!