Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Implement StringLog, adding the following methods int howMany ( String element ) ; Returns count of occurrences of element boolean uniquelnsert ( String element )

Implement StringLog, adding the following methods
int howMany(String element);
Returns count of occurrences of element
boolean uniquelnsert(String element);
Inserts element and returns true if element not already present
String smallest();
Returns "earliest" (in alphabetical terms) String
int removeAll(String element);
Removes all occurrences of element and returns count
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];
}
@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

Students also viewed these Databases questions