40.5k views
2 votes
Create a method named replaceCastMember that allows the name of a castMember at an index in the castMembers array to be changed. The method should take in 2 arguments, the int index and the String castMemberName. The method should return a true if the replacement was successful, false otherwise. (Note: Only update the castMember in the array if the index is valid)

1 Answer

3 votes

Answer:

Step-by-step explanation:

The following code is written in Java. It is a method that takes in the two parameters as requested and checks whether or not the index exists in the array. If it does then it replaces the String in that Index with the new castMemberName String parameter that was given. If the replacement is successful then it returns a True boolean, if not then it returns a False boolean. The array being used must be named castNames and have a global scope to be accessible by the method since it is not passed as a parameter.

public static boolean replaceCastMember(int index, String castMemberName) {

System.out.println(castNames.size());

if (index <= castNames.size()) {

castNames.remove(index);

castNames.add(index, castMemberName);

return true;

} else {

return false;

}

}

User Augustas
by
4.9k points