class CharCounter implements StringProcessor private int co

class CharCounter implements StringProcessor {
private int count;
public void process(String s) {
System.out.println(\"Counts: \" + s.length());
}
public int getCount(){
return count;
}
}

Write another new class that implements StringProcessor. This class should count the total number of characters in all processed Strings. This new class will need an instance variable to contain the accumulated count, and an accessor to retrieve the count. Add this to the main method you’ve started writing in Driver.

What code would I write for the accumulated count?

Solution

Looks like the class you have started is the new class mentioned in the question and just need the code for accumlated count. So i will cotinue in your class.

class CharCounter implements StringProcessor{

private int count;


public static void process(String s) {

count = s.length()-1; //count variable to hold the accumulated count.}

public static int getCount(){
return count;

}

public static void main(String args[])

{

int acCount;

String str1;

Scanner s = new Scanner(System.in)

str = s.next();

process(str); //functioncall to calculate number of characters in String

acCount = getCount(); //Function call to get the count of characters.

System.out.println(\"The count is \" + acCount);

}

}

class CharCounter implements StringProcessor { private int count; public void process(String s) { System.out.println(\

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site