Texbook: Object-Oriented Data Structures Using Java (Third Edition) by Nell Dale
ID: 3665018 • Letter: T
Question
Texbook: Object-Oriented Data Structures Using Java (Third Edition) by Nell Dale, Daniel T. Joyce, and Chip Weems.
Chapter 2, Page 156, Example #53.
Design, code, and use an interactive test driver for the LinkedStringLog class. (Hint: You could proceed by making a copy of the ITDArrayStringLog class and then implementing the appropriate changes). Please Help me with solving this Question, first and foemost.
Here is my most recent UML diagram:
My Current Souce Code:
package ch02.stringLogs;
/**
* Implements String nodes for a Linked List.
* <br> This class is used only to support link or reference-based implementation,
of the StringLog Advanced Data Type (ADT).
* <p> This is a Self-Referential class, because of it's variable link.
* Self-referential classes are simply, classes that have variables,
that can hold references ("Pointer", "Memory Address") to each other.
* <p> This Enables us to create a chain of references,
where the last variable ("node") is assigned a null value.
*
* @author Nell Dale, Daniel T. Joyce, Chip Weems.
*/
public class LLStringNode {
private String info; // Information stored in a list.
private LLStringNode link; // Reference to a node.
/**
* Constructor that accepts a String as an argument (parameter), and sets
the info variable to that String.
*
* @param info
* @args info
*/
public LLStringNode(String info)
{
this.info = info;
link = null;
}
/**
* Transformer...Sets info String of this LLStringNode.
*
* @param info
*/
public void setInfo(String info)
{
this.info = info;
}
/**
* Transformer...Sets link of this LLStringNode.
*
* @param link
*/
public void setLink(LLStringNode link)
{
this.link = link;
}
/**
* Observer...
*
* @return Returns info String of this LLStringNode
*/
public String getInfo()
{
return info;
}
/**
* Observer...
*
* @return Returns link of this LLStringNode.
*/
public LLStringNode getLink()
{
return link;
}
}
package ch02.stringLogs;
/**
* Interface for a class that implements a log of Strings. A log "remembers" the
elements placed into it. <p> Precondition: A log must have a "name".
*
* @author Nell Dale, Daniel T. Joyce, Chip Weems
*/
public interface StringLogInterface {
/**
* Transformer...Places the String parameter, element, into this StringLog.
* <p> Precondition: This StringLog isn't full.
* @param element The string to be added to this StringLog.
*/
void insert(String element);
/**
* Transformer...This operation resets the StringLog to the empty state;
the StringLog retains its name.
*
*/
void clear();
/**
* Observer...Searches this StringLog for passed in String parameter.
* <p> Ignores case differences when doing a String comparison
* <p> Calls equalsIgnoreCase method from String class
*
* @param element
* @return Return true if String parameter, element, is in this StringLog.
* <br> Otherwise returns false.
* @see java.lang.String
* @see java.lang.Object
*/
boolean contains(String element);
/**
* Observer...
* @return Returns the number of Strings in this StringLog.
*/
int size();
/**
* Observer...Returns whether this StringLog is full.
* <br> If full, the client should no longer invoke the insert operation.
*
* @return Returns true if this StringLog is full, otherwise returns false.
*/
boolean isFull();
/**
* Observer...
*
* @return Returns the name attribute of this StringLog.
*/
String getName();
/**
* Observer...Overrides toString method, from Object class.
*
* @see java.lang.Object
* @return Returns a nicely formatted String,
representing the entire contents of this StringLog.
*/
@Override
String toString();
}
package ch02.stringLogs;
/**
* Implements interface, StringLogInterface.
* Lab 1 (part 1 of 2) for professor Jayakumar Rao.
* <p> Data Structures, COMP-228-801RL (section 24993),
Brookdale Community College; Spring 2016, 15 week Semester.
*
* @author Jack Chkifati, Nell Dale, Daniel T. Joyce, Chip Weems
*/
public class LinkedStringLog implements StringLogInterface {
/**
* Reference to first node of linked list, that holds the StringLog Strings
*/
protected LLStringNode log;
/**
* Name of this StringLog
*/
protected String name;
/**
* Constructor...Instantiates and returns a reference to an empty StringLog
object, whose name is assigned the String value in the parameter
variable, "name".
*
* @param name
*/
public LinkedStringLog(String name)
{
log = null;
this.name = name;
}
/**
* Transformer...Places element into this StringLog.
<br> Precondition: This StringLog is not full.
*
* @param element
*/
@Override
public void insert(String element)
{
LLStringNode newNode = new LLStringNode(element);
newNode.setLink(log);
log = newNode;
}
/**
* Observer...
*
* @return Returns true if this StringLog is full, false otherwise.
*/
@Override
public boolean isFull()
{
return false;
}
/**
* Observer...
*
* @return Returns the number of Strings in this StringLog.
*/
@Override
public int size()
{
int count = 0;
LLStringNode node;
node = log;
while (node != null)
{
count++;
node = node.getLink();
}
return count;
}
/**
* Obeserver...Ignores case difference when doing String comparison.
*
* @param element
* @return Returns true if element is in this StringLog, otherwise returns
false.
*/
@Override
public boolean contains(String element)
{
LLStringNode node;
node = log;
while (node != null)
{
if (element.equalsIgnoreCase(node.getInfo())) // if they match
return true;
else
node = node.getLink();
}
return false;
}
/**
* Transformer...Makes this StringLog empty.
*/
@Override
public void clear()
{
log = null;
}
/**
* Observer...
*
* @return Returns the name of this StringLog.
*/
@Override
public String getName()
{
return name;
}
/**
* Observer...
*
* @return Returns a nicely formatted string representing this StringLog.
*/
@Override
public String toString()
{
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;
}
/* Lab 1 (Part 1 of 2)
--------------------------------------------------------------------------------
*/
/**
* Observer...
*
* @return Returns true if this StringLog is empty, otherwise returns false.
*/ // Example #48
public boolean isEmpty()
{
return null == log.getLink();
}
/**
* Observer...
*
* @param element
* @return Returns integer representing how many times the String parameter,
element, appears in this log.
*/ // Example #49
public int howMany(String element)
{
int count = 0;
LLStringNode node = log;
while (node != null) { /* Only "executes" if there are nodes linked together in
this list log */
if (node.getInfo().contains(element)) {
count++;
node = node.getLink(); // Get next refrence
}
}
return count;
}
/**
* Transformer...
*
* @param element
* @return Returns true if String parameter, element, is not in this StringLog.
* <br> This method ignores Case differences when comparing Strings.
* <p> If true, it inserts the new String into this LinkedStringLog.
* <br> Otherwise returns false.
*/ // Example #50
public boolean uniqInsert(String element)
{
LLStringNode node = log;
boolean inList = false; // Place holder to check for unique String.
while(node != null) {
// While the list isn't empty, transverse it to check for duplicate String(s).
if(contains(element) == true) {
inList = true; /* if duplicate String found set placeholder
to true. */
}
node = node.getLink();
/* Get next Reference (When none are left, this automatically assigns node,
null, effectively breaking the while loop) */
}
if (inList == false) { // Insert String parameter, element, into list.
insert(element);
}
return inList;
}
/**
* Observer...Precondition: StringLog is not empty.
* Transverses LinkedStringLog (log), comparing String variable smallest,
to every String in this log.
* <p> Reassign String variable smallest, the value in info from this reference,
* if comparison finds it lexicographically smaller.
*
* @return Returns smallest String in this log, by lexicographic order.
* @see java.lang.String
*/ // Example #51
public String smallest()
{
LLStringNode node = log;
String smallest = node.getInfo();
/* String from current reference in list. */
node = node.getLink();
/* Increments node's reference, to have something new to compare our String
svariable with. */
while(node != null)
// Loop while theres at least one reference left in this list.
{
if(smallest.compareToIgnoreCase(node.getInfo()) > 0) {
smallest = node.getInfo();
}
/*
If this reference's info variable is lexicographically smaller,vthen our
String variable smallest, reassign smallest that String, and continue with loop.
*/
node = node.getLink(); // Get next reference in list (null if not empty).
}
return smallest;
}
}
This problem is the last in a project. The homework was to complete examples 48, 49, 50, 51, and 53. I have done the others, they are added to the bottom of the authors ListStringLog class; although I feel as if one or even 2 of my methods are flawed, when I compared them to other Q & A on Chegg...
Example # 49
https://www.chegg.com/homework-help/questions-and-answers/design-code-new-method-exported-linkedstringlog-called-howmany-following-signature-public--q1163413?trackid=5a87a55b&strackid=76248aef&ii=4&ii=4&ii=4
Example # 50
https://www.chegg.com/homework-help/questions-and-answers/design-code-new-method-exported-linkedstringlog-called-uniqinsert-following-signature-publ-q1163424?trackid=5a87a55b&strackid=76248aef&ii=5&ii=5&ii=5
Example # 51
https://www.chegg.com/homework-help/questions-and-answers/design-code-new-method-exported-linkedstringlog-called-smallest-following-signature-public-q8155127?trackid=5a87a55b&strackid=76248aef&ii=1&ii=1&ii=1
Sadly this question doesn't seem to have been answered, can you please help me at least solve this example.
LLStringNode LinkedStringLog -String info _LLStringNode link #LLStringNode log tg#String name +LLStringNode (String info) +LinkedStringLog(String name) O +void setlnfo(String info) o +void insert(String element) +void setLink(LLStringNode link) +String getlnfo0 + boolean isFull() +int size() +boolean contains(String element) +void clear() o +LLStringNode getLinkO String getName() +boolean isEmpty() +boolean uninsert(String element) o +String toString0 o +int howMany(String element) +String smallest) ArrayString Log tg#String name #String[] log #int lastIndex StringLoglnterface +ArrayStringLog(String name, int maxSize) +ArrayStringLog(String name) +void insert(String element) +void clear() + boolean isFull() +int size() -void insert(String element) -void clear() -boolean contains(String element) ~int size() )~boolean isFull() ~String getName() ~String toString() String getName() o +String toString0 +boolean contains(String element)Explanation / Answer
/*
* UseStringLog.java
*
* Simple example of the use of a StringLog
*/
import ch02.stringLogs.*;
public class UseStringLog {
public static void main(String[] args) {
StringLogInterface sample = new ArrayStringLog("Example Use");
sample.insert("Elvis");
sample.insert("King Louis XII");
sample.insert("Captain Kirk");
System.out.println(sample);
System.out.println("The size of the log is " + sample.size());
System.out.println("Elvis is in the log: " + sample.contains("Elvis"));
System.out.println("Santa is in the log: " + sample.contains("Santa"));
}
}
/*
* StringLogInterface.java by Dale/Joyce/Weems Chapter 2
*
* Interface for a class that implements a log of strings.
* A log "remembers" the elements placed into it.
*
* A log must have a "name."
*/
package ch02.stringLogs;
public interface StringLogInterface {
void insert(String element);
// Precondition: StringLog is not full.
//
// Places a new element into the StringLog.
boolean contains(String element);
// Returns true when the element exists in the StringLog.
// The contains method ignores case.
int size();
// Returns the number of items in the StringLog.
boolean isFull();
// Returns true if the StringLog is full.
void clear();
// Makes the StringLog empty
String getName();
// Returns the name of the StringLog
String toString();
// Returns the entire StringLog as a single String.
}
/*
*
*
*/
package ch02.stringLogs;
public class LinkedStringLog implements StringLogInterface {
protected LLStringNode log; // reference to the first node
protected String name; // name of this StringLog
public LinkedStringLog(String name) {
log = null;
this.name = name;
}
public void insert(String element) {
LLStringNode newNode = new LLStringNode(element);
newNode.setLink(log);
log = newNode;
}
public void clear() {
log = null;
}
public boolean isFull() {
return false;
}
public String getName() {
return name;
}
public int size() {
int count = 0;
LLStringNode node = log;
while (node != null) {
count++;
node = node.getLink();
}
return count;
}
@Override public String toString() {
String logString = "Log: " + name + " ";
LLStringNode node = log;
int count = 0;
while (node != null) {
count++;
logString += count + ". " + node.getInfo() + " ";
node = node.getLink();
}
return logString;
}
public boolean contains(String element) {
LLStringNode node = log;
while (node != null) {
if (node.getInfo().equalsIgnoreCase(element))
return true;
node = node.getLink();
}
return false;
}
}
/*
* LLStringNode.java
*
*/
package ch02.stringLogs;
public class LLStringNode {
private String info;
private LLStringNode link;
public LLStringNode(String info) {
this.info = info;
link = null;
}
public void setInfo(String info) {
this.info = info
}
public String getInfo() {
return info;
}
public void setLink(LLStringNode link) {
this.link = link;
}
public LLStringNode getLink() {
return link;
}
}
/*
* ITDArrayStringLog.java
*
* Interactive Test Driver for the ArrayStringLog class.
*/
import java.util.*;
import ch02.stringLogs.*;
public class ITDArrayStringLog {
public static void main(String[] args) {
ArrayStringLog test = new ArrayStringLog("Testing");
Scanner in = new Scanner(System.in);
String skip; // skip end of line after reading an integer
boolean keepGoing; // flag for choose operation loop
int constructor; // indicates user's choice of constructor
int operation; // indicates user's choice of operation
System.out.println("What is the name of this test?");
String testName = in.nextLine();
System.out.println(" This is test " + testName + " ");
System.out.println("Chooce a constructor:");
System.out.println(" 1: ArrayStringLog(String name)");
System.out.println(" 2: ArrayStringLog(String name, int maxSize)");
if (in.hasNextInt())
constructor = in.nextInt();
else {
System.out.println("ERROR: You must enter an integer.");
System.out.println("Terminating test.");
return;
}
skip = in.nextLine();
switch (constructor) {
case 1:
test = new ArrayStringLog(testName);
break;
case 2:
System.out.println("Enter a maxSize:");
int maxSize;
if (in.hasNextInt())
maxSize = in.nextInt();
else {
System.out.println("ERROR: You must enter an integer.");
System.out.println("Terminating test.");
return;
}
skip = in.nextLine();
test = new ArrayStringLog(testName, maxSize);
break;
default:
System.out.println("ERROR in constructor choice. Terminating test.");
return;
}
keepGoing = true;
while (keepGoing) {
System.out.println(" Choose an operation:");
System.out.println(" 1: insert(String element)");
System.out.println(" 2: clear()");
System.out.println(" 3: contains(String element)");
System.out.println(" 4: isFull()");
System.out.println(" 5: size()");
System.out.println(" 6: getName()");
System.out.println(" 7: show contents");
System.out.println(" 8: stop testing");
if (in.hasNextInt())
operation = in.nextInt();
else {
System.out.println("ERROR: You must enter an integer.");
System.out.println("Terminating test.");
return;
}
skip = in.nextLine();
switch (operation) {
case 1: // insert
System.out.println("Enter a string to insert");
String insertString = in.nextLine();
test.insert(insertString);
break;
case 2: // clear
test.clear();
break;
case 3: // contains
System.out.println("Enter a string to search for:");
String searchString = in.nextLine();
System.out.println("Result: " + test.contains(searchString));
break;
case 4: // isFull
System.out.println("Result: " + test.isFull());
break;
case 5: // size
System.out.println("Result: " + test.size());
break;
case 6: // getName
System.out.println("Result: " + test.getName());
break;
case 7: // show contents
System.out.println(test);
break;
case 8:
keepGoing = false;
break;
default:
System.out.println("Error in operation choice. Terminating test.");
return;
}
}
System.out.println("Enf of Interactive Test Driver");
}
}
/*
* ArrayStringLog.java
*
*/
//package ch02.stringLogs;
public class ArrayStringLog implements StringLogInterface {
protected String name; // name of this log
protected String[] log; // array that holds the log elements
protected int lastIndex = -1; // index of the last string in the array
public ArrayStringLog(String name, int maxSize) {
// Precondition: maxSize > 0
log = new String[maxSize];
this.name = name;
}
public ArrayStringLog(String name) {
this(name, 100);
}
public void insert(String element) {
// Precondition: This StringLog is not full
lastIndex++;
log[lastIndex] = element;
}
public void clear() {
// Makes the StringLog empty
for (int i = 1; i <= lastIndex; i++) {
log[i] = null;
}
lastIndex = -1;
}
public boolean isFull() {
return (lastIndex == log.length - 1);
}
public int size() {
return (lastIndex + 1);
}
public String getName() {
return name;
}
@Override public String toString() {
String logString = "Log: " + name + " ";
for (int i = 0; i <= lastIndex; i++) {
logString += (i + 1) + ". " + log[i] + " ";
}
return logString;
}
public boolean contains(String element) {
for (int i = 0; i <= lastIndex; i++) {
if (log[i].equalsIgnoreCase(element)) {
return true; // Found element in the StringLog
}
}
return false; // Did not find element in the StringLog
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.