import java.util.ArrayList; /** * This class keeps a set of unique strings, and
ID: 3875018 • Letter: I
Question
import java.util.ArrayList;
/**
* This class keeps a set of unique strings, and lets the user
* ask the total length of all Strings in the store.
*
*
*
*/
public class StrCharCounter {
private ArrayList store = new ArrayList<>();
private int size = 0;
public boolean add(String sToAdd) {
size++;
if (!store.contains(sToAdd))
return store.add(sToAdd);
else
return false;
}
public int calcTotalLength() {
int cnt = 0;
for (int i = 0; i < size; i++) {
cnt += store.get(i).length();
}
return cnt;
}
}
provide a screenshot using the debugger of the IDE of your choice showing all appropriate local and instance variables JUST BEFORE THE EXCEPTION.
identify the error, and how to fix it.
Explanation / Answer
StrCharCounter.java
import java.util.ArrayList;
/**
*
* This class keeps a set of unique strings, and lets the user
*
* ask the total length of all Strings in the store.
*
*
*
*
*
*
*/
public class StrCharCounter {
private ArrayList < String > store = new ArrayList < String > ();
private int size = 0;
public boolean add(String sToAdd) {
if (!store.contains(sToAdd))
{
size++;
return store.add(sToAdd);
} else
return false;
}
public int calcTotalLength() {
int cnt = 0;
for (int i = 0; i < size; i++) {
cnt += store.get(i).length();
}
return cnt;
}
}
___________________
Test.java
public class Test {
public static void main(String[] args) {
StrCharCounter ctr=new StrCharCounter();
//Adding the Unique Strings to the ArrayList
ctr.add("Hello");
ctr.add("Beautiful");
ctr.add("Bobby");
ctr.add("Hello");
//Displaying the total no of characters in the ArrayList
System.out.println("Tot Length = "+ctr.calcTotalLength());
}
}
____________________
Output:
Tot Length = 19
------------------------------
Could you please check the output .If you required any changes Just intimate.I will modify it.Thank You.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.