Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Method-Array Activity 1. Write a method called xTimeArray. The method takes an a

ID: 3820458 • Letter: M

Question

Method-Array Activity

1. Write a method called xTimeArray. The method takes an array which holds String, and a parameter of type integer (called num). It “multiples” each String in the array and then returns the modified array.

Example:

strArray before xTimeArray: {“I”, “love”, “java”}

strArray after xTimeArray with 3, {“I I I”, “love love love”, “java java java”}

2. Write a method called createArrayWithString. The method takes a String (myStr) and a integer (num). The method then creates a String with num size and then fills it with the given String (myStr).

Both should be coded in one class.

These are for code in Java Oracle. Any help would be appreciated, thanks!

Explanation / Answer

DemoXTimesArray.java
import java.util.Arrays;

public class DemoXTimesArray {

   public static void main(String[] args) {
       //Declaring String array
       String arr[]={"i","love","java"};
       int num=3;
      
       //Displaying String array before calling xTimeArray() method
       System.out.println("--- Before xTimeArray ---");
       System.out.println(Arrays.toString(arr));
      
       //calling the method
       arr=xTimeArray(arr,num);
       System.out.println("--- After xTimeArray ---");
      
       //Displaying String array after calling xTimeArray() method
       System.out.println(Arrays.toString(arr));
      
       //declaring a string
String str="Hello";
num=5;
String arr2[]=null;

//calling the method
       arr2=createArrayWithString(str,num);
       System.out.println("--- After createArrayWithString ---");
      
       //Displaying the string array
       System.out.println(Arrays.toString(arr2));
   }

   //This method creates a String array with the given string based num size
   private static String[] createArrayWithString(String str, int num) {
   String arr[]=new String[num];
   for(int i=0;i<num;i++)
   {
       arr[i]=str;
   }
       return arr;
   }

   //This method multiplies each string of string array with a number
   private static String[] xTimeArray(String[] arr, int num) {
       String str="";
       for(int i=0;i<arr.length;i++)
       {
           for(int j=0;j<num;j++)
           {
               str+=arr[i]+" ";              
           }
arr[i]=str;
str="";
       }
       return arr;
   }

}

___________________

Output:

--- Before xTimeArray ---
[i, love, java]
--- After xTimeArray ---
[i i i , love love love , java java java ]
--- After createArrayWithString ---
[Hello, Hello, Hello, Hello, Hello]

_____________Thank You