Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Using this code: design and code a new method to be exported from ArrayStringLog

ID: 3565130 • Letter: U

Question

Using this code: design and code a new method to be exported from ArrayStringLog called howMany, with the following signature

public int howMany[String element]

howMany method returns how many times an element occurs in StringLog

public class ArrayStringLog implements StringLogInterface
{
protected String name;
protected String[] log;
protected int lastIndex = -1;   

public ArrayStringLog(String name, int maxSize)

{
log = new String[maxSize];
this.name = name;
}

public ArrayStringLog(String name)

{
log = new String[100];
this.name = name;
}

public void insert(String element)

{
lastIndex++;
log[lastIndex] = element;
}

public boolean isFull()

{
if (lastIndex == (log.length - 1))
return true;
else
return false;
}
  
public int size()

{
return (lastIndex + 1);
}
  
public boolean contains(String element)

{   
int location = 0;
while (location <= lastIndex)
{
if (element.equalsIgnoreCase(log[location])) // if they match
return true;
else
location++;
}
return false;
}
  
public void clear()

{
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()

{
String logString = "Log: " + name + " ";
  
for (int i = 0; i <= lastIndex; i++)
logString = logString + (i+1) + ". " + log[i] + " ";
  
return logString;
}
}

Explanation / Answer

public class ArrayStringLog implements StringLogInterface
{
protected String name;
protected String[] log;
protected int lastIndex = -1;   

public ArrayStringLog(String name, int maxSize)

{
log = new String[maxSize];
this.name = name;
}

public ArrayStringLog(String name)

{
log = new String[100];
this.name = name;
}

public void insert(String element)

{
lastIndex++;
log[lastIndex] = element;
}

public boolean isFull()

{
if (lastIndex == (log.length - 1))
return true;
else
return false;
}
  
public int size()

{
return (lastIndex + 1);
}
  
public boolean contains(String element)

{   
int location = 0;
while (location <= lastIndex)
{
if (element.equalsIgnoreCase(log[location])) // if they match
return true;
else
location++;
}
return false;
}
  
public void clear()

{
for (int i = 0; i <= lastIndex; i++)
log[i] = null;
lastIndex = -1;
}

public String getName()
// Returns the name of this StringLog.
{
return name;
}

public int howMany(String element)
{
int count = 0;
  
for(int i = 0; i < log.length; i++)
{
if(element.equals(log[i]))
{
count++;
}
}

return count;

}

public String toString()

{
String logString = "Log: " + name + " ";
  
for (int i = 0; i <= lastIndex; i++)
logString = logString + (i+1) + ". " + log[i] + " ";
  
return logString;
}
}