Write a complete program called markLength4 that takes an ArrayList of Strings a
ID: 3643045 • Letter: W
Question
Write a complete program called markLength4 that takes an ArrayList of Strings as a parameter and that places a String of four asterisks ("****") in front of every String of length 4. For example, suppose that an ArrayList called "list" contains the following values:(this, is, lots, of, fun, for, every, Java, programmer)
And you make the following call:
markLength4(list);
Then list should store the following values after the call:
(****, this, is, ****, lots, of, fun, for, every, ****, Java, programmer)
Notice that you leave the original Strings in the list (this, lots, Java) but include the four-asterisk String in front of each to mark it. You may assume that the ArrayList contains only String values, but it might be empty.
Explanation / Answer
Please rate...
Program MarkLength4.java
========================================================
import java.util.ArrayList;
class MarkLength4
{
public static void main(String args[])
{
ArrayList s1=new ArrayList();
s1.add("this");s1.add("is");s1.add("lots");
s1.add("of");s1.add("fun");s1.add("for");
s1.add("every");s1.add("java");
s1.add("programmer");
int i;
System.out.println("Before Marking Length 4:");
System.out.print("s1=[");
for(i=0;i<s1.size();i++)
{
System.out.print(s1.get(i)+" ");
}
System.out.print("] ");
markLength4(s1);
System.out.println("After Marking Length 4:");
System.out.print("s1=[");
for(i=0;i<s1.size();i++)
{
System.out.print(s1.get(i)+" ");
}
System.out.print("] ");
}
public static void markLength4(ArrayList s1)
{
ArrayList t1=new ArrayList();
int size=s1.size();
int i;
for(i=0;i<size;i++)
{
String t=s1.get(i).toString();
if(t.length()==4)t1.add("****");
t1.add(s1.get(i));
}
s1.clear();
s1.addAll(t1);
}
}
==========================================================
Sample output:
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.