Skip to content
Advertisement

How to create a void method for retire

whole qstn description Need help creating the retireplayer method in this assigmnent

Help needed to create avoid retireplayer method in the image attached above

This is my class so far

public class Cricket implements Sport { int[] playerIds = new int[11]; ;

JavaScript

}

This was my initial code for the method Decptn: To create a retire player method(int id) assign -1 to playerid[id] which is my array used throught the whole class. i should print “player with id:{id} has retired” if the player has already retired i should print “Player already retired”

Advertisement

Answer

This runs through the array to find a matching id. if a match is found. it retires player and prints and returns the method. else if no match is found it prints no match found.

The problem with your code was that for each iteration of for loop, you assign each member of the array to -1, so you retired all players. You only retire the player if the id matches. That is what the if statement is for.

JavaScript
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement