Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Using the ArrayStringLog class developed in Week 0 1 Lab as a starting point, implement an UnbndStringLog class that uses an ArrayList as the underlying

Using the ArrayStringLog class developed in Week 01 Lab as a starting point, implement an UnbndStringLog class that uses an ArrayList as the underlying structure. Include all methods from Lab 01.
package stringLog;
public interface StringLogInterface {
void insert(String element);
boolean contains(String element);
void remove(String Element);
int Size();
void clear();
public boolean isEmpty();
public boolean isFull();
String toString();
}
package stringLog;
public class ArrayStringLog implements StringLogInterface {
protected String[] list = null;
protected int count =0;
protected int index =-1;
public ArrayStringLog(){
list = new String[100];
}
public ArrayStringLog(int count){
list = new String[count];
}
public int howMany(String element){
int occurrences =0;
for(int i =0; i < count; i++){
if(list[i].equalsIgnoreCase(element)){
occurrences++;
}
}
return occurrences;
}
public boolean uniqueInsert(String element){
if(!contains(element)){
insert(element);
return true;
}
return false;
}
public String smallest(){
if(count ==0){
return null;
}
String smallest = list[0];
for(int i =1; i < count; i++){
if(list[i].compareTo(smallest)<0){
smallest = list[i];
}
}
return smallest;
}
public int removeAll(String element){
int removedCount =0;
for(int i =0; i < count; i++){
if(list[i].equalsIgnoreCase(element)){
remove(element);
removedCount++;
i--;
}
}
return removedCount;
}
@Override
public void insert(String element){
if(count < list.length){
list[count]= element;
count++;
}
}
@Override
public boolean contains(String element){
index =-1;
for(int i =0; i < count && index ==-1; i++){
if(list[i].equalsIgnoreCase(element)){
index = i;
}
}
return index !=-1;
}
@Override
public void remove(String element){
if(contains(element)){
count--;
list[index]= list[count];
list[count]= null;
}
}
@Override
public int Size(){
return count;
}
@Override
public boolean isEmpty(){
return (count ==0);
}
@Override
public boolean isFull(){
return (count == list.length);
}
@Override
public void clear(){
for(int i =0; i < count; i++){
list[i]= null;
}
count =0;
}
public String toString(){
String result = "String Log:
";
int num =1;
for(int i =0; i < count; i++){
result += num +"."+ list[i]+"
";
num++;
}
return result;
}
}
package stringLog;
import stringLog.ArrayStringLog;
import stringLog.StringLogInterface;
public class StringLog_Test {
public static void main(String[] args){
StringLogInterface log = new ArrayStringLog();
System.out.println("Empty string log");
System.out.println ("Log size; "+ log.Size());
System.out.println(log);
log.insert("Anne");
log.insert("Bob");
log.insert("Charlie");
log.insert("Diane");
System.out.println("Log size: "+ log.Size());
System.out.println("The gang's all here");
System.out.println(log);
log.clear();
System.out.println("Empty String Log");
System.out.println("Log size: "+ log.Size());
System.out.println(log);
}
}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Modern Database Management

Authors: Jeffrey A. Hoffer Fred R. McFadden

4th Edition

0805360476, 978-0805360479

More Books

Students also viewed these Databases questions