Question: Need help in java please!!!! I dont know why I cant get 1 0 0 on the rubric The starter project is provided for the
Need help in java please!!!! I dont know why I cant get on the rubric
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 othermethod 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 othermethod which should merge the current bag with the provided bag other
Ensure your mergemethod works in place, modifying the linked structure without creating additional arrays or collections.
The merged bag should allow duplicates from both bags.
BagInterface.java
public interface BagInterface
boolean addT newEntry;
int getCurrentSize;
boolean isEmpty;
Main.java
public class Main
public static void mainStringargs
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 tempdata ;
temp tempnext;
result null;
return result;
@Override
public boolean addT newEntry
Node newNode new NodenewEntry firstNode;
firstNode newNode;
numberOfEntries;
return true;
@Override
public int getCurrentSize
return numberOfEntries;
@Override
public boolean isEmpty
return numberOfEntries ;
Method to merge another LinkedBag into this LinkedBag
public void mergeLinkedBag other
if otherisEmpty
return; No need to merge if other is empty
if thisisEmpty
this.firstNode other.firstNode; If current bag is empty, just point to the other bag's nodes
else
Node current other.firstNode;
while current null Add all nodes from the other bag to this bag
this.addcurrentdata;
current current.next;
this.numberOfEntries other.numberOfEntries; Update the number of entries
GradingRubric.java
public class GradingRubric
public static void mainStringargs
int totalPoints ;
Test : Merging two nonempty bags Simple Case
LinkedBag bagnew LinkedBag;
LinkedBag bagnew LinkedBag;
bagadd;
bagadd;
bagadd;
bagadd;
bagadd;
bagadd;
bagmergebag;
if bagprintlistequalsnull
System.out.printlnTest passed;
totalPoints ;
else
System.out.printlnTest failed;
Test : Merging a nonempty bag with an empty bag
LinkedBag bagnew LinkedBag;
LinkedBag bagnew LinkedBag;
bagadd;
bagadd;
bagadd;
bagmergebag;
if bagprintlistequalsnull
System.out.printlnTest passed;
totalPoints ;
else
System.out.printlnTest failed;
Test : Merging an empty bag with a nonempty bag
LinkedBag bagnew LinkedBag;
LinkedBag bagnew LinkedBag;
bagadd;
bagadd;
bagadd;
bagmergebag;
if bagprintlistequalsnull
System.out.printlnTest passed;
totalPoints ;
else
System.out.printlnTest failed;
Test : Merging two empty bags
LinkedBag bagnew LinkedBag;
LinkedBag bagnew LinkedBag;
bagmergebag;
if bagprintlistequalsnull
System.out.printlnTest passed;
totalPoints ;
else
System.out.printlnTest failed;
NEW Test : Merging bags with nonoverlapping elements
LinkedBag bagnew LinkedBag;
LinkedBag bagnew 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
