Write a static method named longer that accepts two arrays of strings a1 and a2
ID: 3643873 • Letter: W
Question
Write a static method named longer that accepts two arrays of strings a1 and a2 as parameters and returns a new array a3 such that each element of a3 at each index i stores whichever string has greater length (more characters) between the elements at that same index i in arrays a1 and a2. If there is a tie, take the element from a1.For example, if a1 and a2 store the following elements:
String[] a1 = {"star", "pie", "jelly bean", "car"};
String[] a2 = {"cookie", "fig", "banana", "soda"};
Then your method should return the new array {"cookie", "pie", "jelly bean", "soda"}.
If the arrays a1 and a2 are not the same length, the result returned by your method should have as many elements as the larger of the two arrays. If a given index i is in bounds of a1 but not a2 (or vice versa), there are not two elements to compare, so your result array's element at index i should store the value "oops". For example, if a1 and a2 store
the following elements:
String[] a1 = {"Splinter", "Leo", "April", "Don", "Raph"};
String[] a2 = {"Krang", "Shredder", "Bebop"};
Then your method should return the new array {"Splinter", "Shredder", "April", "oops", "oops"}.
Do not modify the elements of a1 or a2. Do not make any assumptions about the length of a1 or a2 or
the length of the strings. You may assume that neither array is null and no element of either array is null.
Hint: initially fill the new array with "oops" and then overwrite where the input arrays both have values
import java.util.*;
public class Longer {
public static void main(String[] args) {
String[] a1 = {"star", "pie", "jelly bean", "car"};
String[] a2 = {"cookie", "fig", "banana", "soda"};
String[] a3 = longer(a1, a2); // [cookie, pie, jelly bean, soda]
System.out.println(Arrays.toString(a3));
String[] a4 = {"Splinter", "Leo", "April", "Don", "Raph"};
String[] a5 = {"Krang", "Shredder", "Bebop"};
String[] a6 = longer(a4, a5); // [Splinter, Shredder, April, oops, oops]
System.out.println(Arrays.toString(a6));
}
// *** Your method code goes here ***
} // End of Longer class
Explanation / Answer
Please rate...
import java.util.Arrays;
class LongerWords
{
public static void main(String[] args) {
String[] a1 = {"star", "pie", "jelly bean", "car"};
String[] a2 = {"cookie", "fig", "banana", "soda"};
String[] a3 = longer(a1, a2); // [cookie, pie, jelly bean, soda]
System.out.println(Arrays.toString(a3));
String[] a4 = {"Splinter", "Leo", "April", "Don", "Raph"};
String[] a5 = {"Krang", "Shredder", "Bebop"};
String[] a6 = longer(a4, a5); // [Splinter, Shredder, April, oops, oops]
System.out.println(Arrays.toString(a6));
}
public static String[] longer(String a1[],String a2[])
{
int l1,l2,l,i,s;
l1=a1.length;
l2=a2.length;
if(l1>=l2){l=l1;s=l2;}
else {l=l2;s=l1;}
String a3[]=new String[l];
for(i=0;i<l;i++)
{
a3[i]="oops";
}
for(i=0;i<s;i++)
{
if(a1[i].length()>a2[i].length())a3[i]=a1[i];
else if(a1[i].length()<a2[i].length())a3[i]=a2[i];
else if(a1[i].length()==a2[i].length())a3[i]=a1[i];
}
return a3;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.