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 100 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 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.
Ensure your merge()method 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 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, firstNode);
firstNode = newNode;
numberOfEntries++;
return true;
}
@Override
public int getCurrentSize(){
return numberOfEntries;
}
@Override
public boolean isEmpty(){
return numberOfEntries ==0;
}
// Method to merge another LinkedBag into this LinkedBag
public void merge(LinkedBag other){
if (other.isEmpty()){
return; // No need to merge if other is empty
}
if (this.isEmpty()){
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.add(current.data);
current = current.next;
}
}
this.numberOfEntries += other.numberOfEntries; // Update the number of entries
}
}
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 1passed");
totalPoints +=10;
}else {
System.out.println("Test 1failed");
}
//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 2passed");
totalPoints +=10;
}else {
System.out.println("Test 2failed");
}
//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 3passed");
totalPoints +=10;
}else {
System.out.println("Test 3failed");
}
//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 4passed");
totalPoints +=10;
}else {
System.out.println("Test 4failed");
}
//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!