2 Replies - 1648 Views - Last Post: 12 October 2013 - 12:53 PM Rate Topic: -----

#1 gobs06   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 08-October 13

Implement remove() method in 2 classes

Posted 08 October 2013 - 07:23 PM

Question: Implement the remove () method in both ArrayStringLog.java and LinkedStringLog.java. The addition of the new method has serious implication of the current code in both ArrayStringLog.java and LinkedStringLog.java. For example, say the current size (n) of the ADT is returned by invoking size () method, after the call of remove (String element) method which actually removes an existing element in StringLog, then the method size () should return the updated size which is now n-1. Other methods in ArrayStringLog class will need to change their implementation as well. For example, the insert () method and the size () method and other methods may have to change as well. Double-check the implementation of StringLog ADT and make sure that all methods are correct.

Attempts: Extensively Googled LinkedLists and ADT. Need dire help.


package ...;



public class ArrayStringLog implements StringLogInterface

{

protected String name; // name of this StringLog

protected String[] log; // array that holds strings

protected int lastIndex = -1; // index of last string in array



public ArrayStringLog(String name, int maxSize)

// Precondition: maxSize > 0

//

// Instantiates and returns a reference to an empty ArrayStringLog

// object with name "name" and room for maxSize strings.

{

log = new String[maxSize];

this.name = name;

}



public ArrayStringLog(String name)

// Instantiates and returns a reference to an empty ArrayStringLog

// object with name "name" and room for 100 strings.

{

log = new String[100];

this.name = name;

}



public void insert(String element)

// Precondition: This StringLog is not full.

//

// Places element into this StringLog.

{

lastIndex++;

log[lastIndex] = element;

}



public boolean isFull()

// Returns true if this StringLog is full, otherwise returns false.

{

if (lastIndex == (log.length - 1))

return true;

else

return false;

}

public int size()

// Returns the number of Strings in this StringLog.

{

return (lastIndex + 1);

}

public boolean contains(String element)

// Returns true if element is in this StringLog,

// otherwise returns false.

// Ignores case differences when doing string comparison.

{

int location = 0;

while (location <= lastIndex)

{

if (element.equalsIgnoreCase(log[location])) // if they match

return true;

else

location++;

}

return false;

}

public void clear()

// Makes this StringLog empty.

{

for (int i = 0; i <= lastIndex; i++)

log[i] = null;

lastIndex = -1;

}



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 + "\n\n";

for (int i = 0; i <= lastIndex; i++)

logString = logString + (i+1) + ". " + log[i] + "\n";

return logString;

}

public void remove(String element)

{

}

}




package ...;



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;



while (node != null)

{

if (element.equalsIgnoreCase(node.getInfo())) // if they match

return true;

else

node = node.getLink();

}



return false;

}

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 + "\n\n";

LLStringNode node;

node = log;

int count = 0;

while (node != null)

{

count++;

logString = logString + count + ". " + node.getInfo() + "\n";

node = node.getLink();

}

return logString;

}

public void remove(String element)

{

}

}



package ...;



public class LLStringNode

{

private String info;

private LLStringNode link;

public LLStringNode(String info)

{

this.info = info;

link = null;

}

public void setInfo(String info)

// Sets info string of this LLStringNode.

{

this.info = info;

}



public String getInfo()

// Returns info string of this LLStringNode.

{

return info;

}

public void setLink(LLStringNode link)

// Sets link of this LLStringNode.

{

this.link = link;

}


public LLStringNode getLink()

// Returns link of this LLStringNode.

{

return link;

}

}




package ...;



public interface StringLogInterface

{

/**

* Remove the specified element in the StringLog if it exists

*

* @param element: String, to be removed if it is present in the StringLog

*/

public void remove(String element);

void insert(String element);

// Precondition: This StringLog is not full.

//

// Places element into this StringLog.



boolean isFull();

// Returns true if this StringLog is full, otherwise returns false.



int size();

// Returns the number of Strings in this StringLog.



boolean contains(String element);

// Returns true if element is in this StringLog,

// otherwise returns false.

// Ignores case differences when doing string comparison.

void clear();

// Makes this StringLog empty.



String getName();

// Returns the name of this StringLog.



String toString();

// Returns a nicely formatted string representing this StringLog.




Is This A Good Question/Topic? 0
  • +

Replies To: Implement remove() method in 2 classes

#2 pbl   User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8379
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: Implement remove() method in 2 classes

Posted 09 October 2013 - 09:37 PM

Why are you using bold we are not shorthsighted ?

and the question is ?
Was This Post Helpful? 0
  • +
  • -

#3 gobs06   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 08-October 13

Re: Implement remove() method in 2 classes

Posted 12 October 2013 - 12:53 PM

How to implement the remove() method in ArrayStringLog.java and LinkedStringLog.java
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1