Complete the Words class. The Words class has a constructor that takes an array
ID: 3714985 • Letter: C
Question
Complete the Words class. The Words class has a constructor that takes an array of Strings as a parameter.
Provide the instance variable and the constructor.
Provide a method Seconds() which returns a string consisting of the second character of each string in the array separated by "*". That is an asterisk. You can assume that the array is not empty and each element has at least 2 character.
I'm stuck on this step, this is my code so far
public class Words
{
private String[] array;
public Words()
{
this.array = array;
}
public String Seconds()
{
String seconds = "";
for (int a = 0; a < array.length; a++)
{
seconds =
}
return seconds;
}
}
Explanation / Answer
public class Words {
private String[] array = {"Apple", "Banana", "Orange"};
public Words() {
this.array = array;
}
public String Seconds()
{
String seconds = "";
/* for (String value : array) {
System.out.println( value.substring(0, 1)+"*"+value.substring(2, value.length()));
seconds= value.substring(0, 1)+"*"+value.substring(2, value.length());
}
*/
for (int i=0 ; i<array.length;i++) {
System.out.println( array[i].substring(0, 1)+"*"+array[i].substring(2, array[i].length()));
seconds= array[i].substring(0, 1)+"*"+array[i].substring(2, array[i].length());
}
return seconds;
}
public static void main(String[] args) {
Words words= new Words();
words.Seconds();
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.