Write a public static method named insert. The insert method should have the signature insert(String[] words, String newWord, int place), should return a boolean, and should function as described below.
When called, if place does not represent a valid index for words, then the method will return false to indicate the insertion could not be performed and do nothing else. Otherwise the method will insert the String newWord into the array words at the index place, moving each subsequent entry one place further and losing the final String in the array. The method will then return true to indicate the insertion has taken place.
Use the runner class to test this method: do not add a main method to your code in the U6_L4_Activity_One.java file or it will not be scored correctly.
Here is the runner code:
import java.util.Scanner;
public class runner_U6_L4_Activity_One{
public static void main(String[] args){
Scanner scan = new Scanner(System.in);
System.out.println("Enter array length:");
int len = scan.nextInt();
scan.nextLine();
String[] wordList = new String[len];
System.out.println("Enter values:");
for(int i = 0; i < len; i++){
wordList[i] = scan.nextLine();
}
System.out.println("Enter new String:");
String insWord = scan.nextLine();
System.out.println("Enter place:");
int pos = scan.nextInt();
System.out.println("Method return: " + U6_L4_Activity_One.insert(wordList, insWord, pos));
System.out.print("Array contents: {");
for(int i = 0; i < len-1; i++){
System.out.print(wordList[i] + ", ");
}
System.out.println(wordList[len-1]+"}");
}
}

Respuesta :

Answer:

Explanation:

The following code was written in Java and performs the exact requirements listed in the question. It has also been tested by the runner code and works perfectly.

public static boolean insert(String[] words, String newWord, int place) {

               if (place > words.length) {

                   return false;

       } else {

                   for (int x = words.length - 1; x >= 0; x--) {

                       if (place == x) {

                           words[x] = newWord;

                           break;

                       } else {

                           words[x] = words[x-1];

                       }

                   }

                   return true;

               }

   }

Methods are group of code segments that are executed when evoked or called.

The insert public static method in Java, where comments are used to explain each line is as follows:

//This defines the method

public static boolean insert(String[] words, String newWord, int place) {

   //This returns false, if the place value is less than the word length

   if (place > words.length) {

       return false;

   }  

   //If otherwise

   else {

       //This iterates through the characters of the word, in reverse order

       for (int i = words.length - 1; i >= 0; i--) {

           //This inserts the new word into the array

           if (place == i) {

               words[i] = newWord;

               break;

           } else {

               words[i] = words[i-1];

           }

       }

       //This returns true

       return true;

   }

}

Read more about methods at:

https://brainly.com/question/13628934

ACCESS MORE
EDU ACCESS
Universidad de Mexico