Can you please add comments to the code below explaining what each method is doi
ID: 3811246 • Letter: C
Question
Can you please add comments to the code below explaining what each method is doing? Thank you!
PROGRAM CODE:
GroupHolder.java
package groups;
import arrays.ArrayList;
public class GroupHolder {
private class GroupNode
{
String data;
GroupNode next;
public GroupNode(String data, GroupNode next) {
this.data = data;
this.next = next;
}
public GroupNode(String data) {
this.data = data;
next = null;
}
public void setNext(GroupNode next)
{
this.next = next;
}
public String getData()
{
return data;
}
@Override
public String toString() {
String result = "{" + data ;
GroupNode temp = next;
while(temp != null)
{
result += ", " + temp.getData();
temp = temp.next;
}
return result + "}";
}
}
ArrayList<GroupNode> items;
public GroupHolder() {
items = new ArrayList<GroupNode>();
}
public void addItem(String s)
{
for(int i=0; i<items.size(); i++)
{
if(items.get(i).getData().equals(s))
{
return;
}
}
GroupNode newNode = new GroupNode(s);
items.add(newNode);
}
public String getRepresentative(String s)
{
for(int i=0; i<items.size(); i++)
{
if(items.get(i).getData().equals(s))
{
GroupNode nodes = items.get(i);
while(nodes.next != null)
{
nodes = nodes.next;
}
return nodes.getData();
}
}
return null;
}
public ArrayList<GroupNode> getAllRepresentatives()
{
return items;
}
public boolean inSameGroup(String s1, String s2)
{
if(getRepresentative(s1) == null || getRepresentative(s2) == null)
return false;
else
return getRepresentative(s1).equals(getRepresentative(s2));
}
public void union(String s1,String s2)
{
String repS1 = getRepresentative(s1);
String repS2 = getRepresentative(s2);
if(repS1 == null || repS2 == null || !repS1.equals(repS2))
{
int indexOfRepS1 = getRepresentativeIndex(s1);
int indexOfRepS2 = getRepresentativeIndex(s2);
GroupNode reps1Node = items.get(indexOfRepS1);
GroupNode reps2Node = items.get(indexOfRepS2);
GroupNode temp = reps1Node;
while(temp.next != null)
{
temp = temp.next;
}
temp.setNext(reps2Node);
items.set(indexOfRepS1, reps1Node);
}
}
private int getRepresentativeIndex(String s)
{
for(int i=0; i<items.size(); i++)
{
if(items.get(i).getData().equals(s))
{
return i;
}
}
return -1;
}
}
GroupHolderTester.java
package groups;
public class GroupHolderTester {
public static void main(String[] args) {
GroupHolder holder = new GroupHolder();
holder.addItem("a");
holder.addItem("b");
holder.addItem("c");
holder.union("a", "c");
System.out.println(holder.getAllRepresentatives());
}
}
OUTPUT:
[{a, c}, {b}, {c}]
Explanation / Answer
import java.util.ArrayList;
public class GroupHolder {
private class GroupNode
{
String data;
GroupNode next;
//constructor that takes 2 parameters one string variable and other is GroupNode object
public GroupNode(String data, GroupNode next) {
this.data = data;
this.next = next;
}
// constructor that takes only one parameter of type string
public GroupNode(String data) {
this.data = data;
next = null;
}
//setter method for the instance variable next of type GroupNode
public void setNext(GroupNode next)
{
this.next = next;
}
//getter method for the instance variable data
public String getData()
{
return data;
}
//overriden toString() to print the result
@Override
public String toString() {
String result = "{" + data ;
GroupNode temp = next;
while(temp != null)
{
result += ", " + temp.getData();
temp = temp.next;
}
return result + "}";
}
}
ArrayList<GroupNode> items;
//default constructor for the outer class
public GroupHolder() {
items = new ArrayList<GroupNode>();
}
// function to add item in the arraylist items
public void addItem(String s)
{
for(int i=0; i<items.size(); i++)
{
if(items.get(i).getData().equals(s))
{
return;
}
}
GroupNode newNode = new GroupNode(s);
items.add(newNode);
}
//function to read the content of the arraylist if the data matches with the parameter passed in the function
public String getRepresentative(String s)
{
// for loop to traverse the linkedlist
for(int i=0; i<items.size(); i++)
{
// checking if the element at i index matches the parameter passed in the function
if(items.get(i).getData().equals(s))
{
GroupNode nodes = items.get(i);
// loop to get the last element of the nodes
while(nodes.next != null)
{
nodes = nodes.next;
}
return nodes.getData();
}
}
return null;
}
public ArrayList<GroupNode> getAllRepresentatives()
{
return items;
}
// to check whether the string parameters passed s1 and s2 are of the same group
public boolean inSameGroup(String s1, String s2)
{
if(getRepresentative(s1) == null || getRepresentative(s2) == null)
return false;
else
return getRepresentative(s1).equals(getRepresentative(s2));
}
// function to get the union
public void union(String s1,String s2)
{
String repS1 = getRepresentative(s1);
String repS2 = getRepresentative(s2);
if(repS1 == null || repS2 == null || !repS1.equals(repS2))
{
int indexOfRepS1 = getRepresentativeIndex(s1);
int indexOfRepS2 = getRepresentativeIndex(s2);
GroupNode reps1Node = items.get(indexOfRepS1);
GroupNode reps2Node = items.get(indexOfRepS2);
GroupNode temp = reps1Node;
while(temp.next != null)
{
temp = temp.next;
}
temp.setNext(reps2Node);
items.set(indexOfRepS1, reps1Node);
}
}
// function to get the index for the string s passed as the parameter in the Arraylist items
private int getRepresentativeIndex(String s)
{
// for loop to travesre the arraylist
for(int i=0; i<items.size(); i++)
{
// condition to check if the element at index i in arraylist matches the string parameter s
if(items.get(i).getData().equals(s))
{
return i;
}
}
return -1;
}
}
public class GroupHolderTester {
// main method to test the GroupHoder class
public static void main(String[] args) {
GroupHolder holder = new GroupHolder();
holder.addItem("a");
holder.addItem("b");
holder.addItem("c");
holder.union("a", "c");
System.out.println(holder.getAllRepresentatives());
}
}