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

The below code and its tester throws an exception because of the boldened area-

ID: 3875979 • Letter: T

Question

The below code and its tester throws an exception because of the boldened area- show how you would correct this code and explain both codes in as much detail as possible and why you corrected the code the way you did.

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.
*
* @author parks
*
*/
public class StrCharCounter {

   private ArrayList<String> 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;
   }
}

...............................................................................

public class Kaboom {

   public static void main(String[] args) {
       String[] strs = { "aardvark", "show", "flower", "mantle", "table", "clock", "red", "show", "water" };
       StrCharCounter k = new StrCharCounter();
       for (String s: strs) {
           k.add(s);
       }
       System.out.println("length of all strings " + k.calcTotalLength());
   }

}

Explanation / Answer

The exception occured because of referring to the index which is out of the boundary.

public boolean add(String sToAdd) {
       size++;
       if (!store.contains(sToAdd))
           return store.add(sToAdd);
       else
           return false;
   }

when the add function is invoked to add the string

to avoid this exception :

size should be increment while adding to the string to the arraylist.

public boolean add(String sToAdd) {

if (!store.contains(sToAdd)){
size++;
return store.add(sToAdd);
}

else
return false;
}

.

program:

import java.util.ArrayList;

public class StrCharCounter {

//declare the arraylist to store the string
//size variable hold the number of string stored in the atore
private final ArrayList<String> store = new ArrayList<>();
private int size = 0;

//method add to hold unique word in the store. without repetition
public boolean add(String sToAdd) {
// store.contains(sToAdd) checks whether the string already present the store.
//if the store doesn't have the string then the string is added to store. else returns false
  
if (!store.contains(sToAdd)){
size++;
return store.add(sToAdd);
}

else
return false;
}
//method calcTotalLength returns the total length of the string in store
public int calcTotalLength() {
  
int cnt = 0;//variable to hold length
for (int i = 0; i < size; i++) {//perform until the last element in the store
cnt += store.get(i).length(); // calculates the string(i) length and add to cnt
}
return cnt;//returns the length
}
}

public class Kaboom {

public static void main(String[] args) {
//strs is a string array holds the string
String[] strs = { "aardvark", "show", "flower", "mantle", "table", "clock", "red", "show", "water"};
StrCharCounter k = new StrCharCounter();// creates the object for StrCharCounter
//adds the string to the arraylist
for (String s: strs) {
k.add(s);
}
//prints the total length by calling calcTotalLength method
System.out.println("length of all strings " + k.calcTotalLength());
}

}

output:

run:
length of all strings 42
BUILD SUCCESSFUL (total time: 0 seconds)

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote