This lab focus on the \"application programmer hat\" wear. Nevertheless, it make
ID: 3782809 • Letter: T
Question
This lab focus on the "application programmer hat" wear. Nevertheless, it makes good sense to understand there may be some technical differences by the" implementer-hat"wear that the application.For this lab, we will use the array-based, and linked list-based implementation for the STRINGLOG class from chaptet2. A secondary goal is to gain better understanding of data structures, program structures, java tolls and to get better at visualizing what the computer is actually doing. TASK Create and test an ARRAY-BASED string and a LINKED LIST-BASED stringLog.
Explanation / Answer
class ArrayStringLog
{
protected String name; // name of this StringLog
protected String[] log; // array that holds strings
protected int index = -1; // index of last string in array
ArrayStringLog(String name, int maxSize)
{
if(maxSize>0)
{
log = new String[maxSize];
this.name = name;
}
else
{
log = new String[100];
this.name = name;
}
}
public void log(String element)
{
index++;
log[index] = element;
}
public boolean isFull()
{
if (index == (log.length - 1))
return true;
else
return false;
}
public int size()
{
return (index + 1);
}
public boolean contains(String element)
{
int location = 0;
while (location <= index)
{
if (element.equalsIgnoreCase(log[location])) // if they match
return true;
else
location++;
}
return false;
}
public String toString()
{
String logString = "Log: " + name + " ";
for (int i = 0; i <= index; i++)
logString = logString + (i+1) + ". " + log[i] + " ";
return logString;
}
}
////// Linked list based log
//----------------------------------------------------------------------
class LinkedStringLog {
class Node
{
String data;
Node link;
Node(String s)
{
data=s;
link=null;
}
void setLink(Node n)
{
link=n;
}
Node getLink()
{
return link;
}
String getData()
{
return data;
}
}
private String name; // name of this StringLog
private Node log = null ;
public LinkedStringLog(String name)
{
this.name = name;
}
public void log(String s) {
if(s!=null)
{
Node node = new Node(s);
node.setLink(log);
log = node;
}
}
// Return size.
public int size() {
int count = 0;
for (Node node=log; node!=null; node=node.getLink())
count++;
return count;
}
public boolean contains(String element) {
for (Node node=log; node!=null; node=node.getLink())
if (element.equalsIgnoreCase(node.getData())) // they match
return true;
return false;
}
public void clear() { log = null; }
public String getName() { return name; }
// Return a nicely formatted string representing this StringLog.
public String toString() {
String ans = "Log: " + name + " ";
int count = 0;
for (Node node=log; node!=null; node=node.getLink())
ans += (++count) + ". " + node.getData() + " ";
return ans;
}
}
// Main class
public class StringLog
{
public static void main(String a[])
{
ArrayStringLog l= new ArrayStringLog("Array Log", 10);
LinkedStringLog ll=new LinkedStringLog("Linked Log");
l.log("Hello");
l.log("Bull");
l.log("Cart");
l.log("Kashmir");
l.log("Mughals");
ll.log("Hello");
ll.log("Bull");
ll.log("Cart");
ll.log("Kashmir");
ll.log("Mughals");
if(l.contains("Hello")) System.out.println("Yes ");
System.out.println(l);
System.out.println(ll);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.