Design and code a new method to be exported from LinkedStringLog called uniqInse
ID: 3623667 • Letter: D
Question
Design and code a new method to be exported from LinkedStringLog called uniqInsert, with the following signature:public boolean uniqInsert ( String element )
The method inserts element into the StringLog unless an identical string already exists in the StringLog, in which case it has no effect on the StringLog. If it does insert the string, it returns true; otherwise, it returns false.
public class LinkedStringLog implements StringLogInterface
{
protected LLStringNode log; // reference to first node of linked
// list that holds the StringLog strings
protected String name; // name of this StringLog
public LinkedStringLog(String name)
// Instantiates and returns a reference to an empty StringLog object
// with name "name".
{
log = null;
this.name = name;
}
public void insert(String element)
// Precondition: This StringLog is not full.
//
// Places element into this StringLog.
{
LLStringNode newNode = new LLStringNode(element);
newNode.setLink(log);
log = newNode;
}
public boolean isFull()
// Returns true if this StringLog is full, false otherwise.
{
return false;
}
public int size()
// Returns the number of Strings in this StringLog.
{
int count = 0;
LLStringNode node;
node = log;
while (node != null)
{
count++;
node = node.getLink();
}
return count;
}
public boolean contains(String element)
// Returns true if element is in this StringLog,
// otherwise returns false.
// Ignores case difference when doing string comparison.
{
LLStringNode node;
node = log;
boolean found = false;
boolean moreToSearch;
moreToSearch = (node != null);
while (moreToSearch && !found)
{
if (element.equalsIgnoreCase(node.getInfo())) // if they match
found = true;
else
{
node = node.getLink();
moreToSearch = (node != null);
}
}
return found;
}
public void clear()
// Makes this StringLog empty.
{
log = null;
}
public String getName()
// Returns the name of this StringLog.
{
return name;
}
public String toString()
// Returns a nicely formatted string representing this StringLog.
{
String logString = "Log: " + name + " ";
LLStringNode node;
node = log;
int count = 0;
while (node != null)
{
count++;
logString = logString + count + ". " + node.getInfo() + " ";
node = node.getLink();
}
return logString;
}
}
Explanation / Answer
Dear.. public class LinkedStringLog implements StringLogInterface { protected LLStringNode log; protected String name; public LinkedStringLog(String name) { log = null; this.name = name; } public LinkedStringLog(String name) { log = null; this.name = name; } public int howMany(String element) // this method returns an int value how many times it occurs in StringLog { int elemtCount = 0; LLStringNode node; node = log; while (node != null) { if (element.equalsIgnoreCase(node.getInfo())) { elemtCount ++; node = node.getLink(); } else { node = node.getLink(); } } return eleCount; }Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.