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 mergeLinkedBag 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 mergeLinkedBag other method, which should merge the current bag with the
provided bag other
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.
Example
Given the following two linked bags:
Bag :
Bag :
After calling merge the result should be:
Merged Bag:
BagInterface.java
public interface BagInterface
boolean addT newEntry;
int getCurrentSize;
boolean isEmpty;
Main.java
public class Main
public static void mainString 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 NodeT data, Node next
this.data data;
this.next next;
instance variables
private Node firstNode;
private int numberOfEntries;
public LinkedBag
firstNode null;
numberOfEntries ;
public String printlist
Node temp firstNode;
String result ;
while temp null
result temp.data ;
temp temp.next;
result "null";
return result;
@Override
public boolean addT newEntry
Node newNode new NodenewEntry null;
newNode.next firstNode;
firstNode newNode;
this.numberOfEntries;
return true;
@Override
public int getCurrentSize
return numberOfEntries;
@Override
public boolean isEmpty
return numberOfEntries ;
public void mergeLinkedBag other
TODO
GradingRubric.java
public class GradingRubric
public static void mainString args
int totalPoints ;
Test : Merging two nonempty bags Simple Case
LinkedBag bag new LinkedBag;
LinkedBag bag new LinkedBag;
bagadd;
bagadd;
bagadd;
bagadd;
bagadd;
bagadd;
bagmergebag;
if bagprintlistequals null"
System.out.printlnTest passed";
totalPoints ;
else
System.out.printlnTest failed";
Test : Merging a nonempty bag with an empty bag
LinkedBag bag new LinkedBag;
LinkedBag bag new LinkedBag;
bagadd;
bagadd;
bagadd;
bagmergebag;
if bagprintlistequals null"
System.out.printlnTest passed";
totalPoints ;
else
System.out.printlnTest failed";
Test : Merging an empty bag with a nonempty bag
LinkedBag bag new LinkedBag;
LinkedBag bag new LinkedBag;
bagadd;
bagadd;
bagadd;
bagmergebag;
if bagprintlistequals null"
System.out.printlnTest passed";
totalPoints ;
else
System.out.printlnTest failed";
Test : Merging two empty bags
LinkedBag bag new LinkedBag;
LinkedBag bag new LinkedBag;
bagmergebag;
if bagprintlistequalsnull
System.out.printlnTest passed";
totalPoints ;
else
System.out.printlnTest failed";
NEW Test : Merging bags with nonoverlapping elements
LinkedBag bag new LinkedBag;
LinkedBag bag new LinkedBag;
bagadd;
bagadd;
bagadd;
bag
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
