
Patrick B. answered 04/25/21
Math and computer tutor/teacher
How do you know the method works unless you write the main to call it?
class Fencepost
{
public int[] countLengths(String Astrs[])
{
int N = Astrs.length;
String outbuff = " ";
int intArrayReturn[] = new int[N];
//first fencepost
int curLength = intArrayReturn[0] = Astrs[0].length();
outbuff = outbuff + curLength;
for (int iLoop=1; iLoop<N; iLoop++)
{
//link chain
outbuff = outbuff + ",";
//next fencepost
curLength = intArrayReturn[iLoop] = Astrs[iLoop].length();
outbuff = outbuff + curLength;
}
System.out.println(outbuff);
return(intArrayReturn);
}
public void Go()
{
String aStrs[]={"The","quick","brown","fox","jumped","over","the","lazy","dogs"};
int lengths[] = countLengths(aStrs);
for (int iLoop=0; iLoop<lengths.length; iLoop++) { System.out.println(lengths[iLoop]); }
System.out.println("---------------------------------");
String Astrs[] = {"The","earth","is","an","inhabited","endangered","planet"};
lengths= countLengths(Astrs);
for (int iLoop=0; iLoop<lengths.length; iLoop++) { System.out.println(lengths[iLoop]); }
}
public static void main(String args[])
{
Fencepost x = new Fencepost();
x.Go();
}
}