In JAVA,
Given:
1-an int variable k,
2-an int array currentMembers that has been declared and initialized,
3-an int variable memberID that has been initialized,
4-and an boolean variable isAMember,
write a code that assigns true to isAMember if the value of memberID can be found in currentMembers, and that assigns false to isAMember otherwise. Use only k, currentMembers, memberID, and isAMember.

Respuesta :

Answer:

The program to this question as follows:

Program:

public class Main //define class main

{

public static void main(String[] args)  //define main method

{

boolean isAMember = false; //define boolean variable isAMember

int currentMembers[]={1,72,36,43,51,61,72,80}; //declare and initialized array.

int memberID=72; //define integer variable memberID.

for(int k = 0; k < currentMembers.length; k++) //loop.

{

if(currentMembers[k] == memberID) //conditional statement.

{

isAMember = true; //assign value true.

break;

}

System.out.print("true"); //print value.

}

}

}

Output:

true

Explanation:

The above java program to this question can be described as:

  • In java program we define a class that is "Main" inside this class we define a main method in the main method we define variables that are "k, memberID, isAMember, currentMembers".
  • The variable k and memberID is an integer variable that is used in the loop and use for match condition. and currentMembers is an integer array that is declared and initialized. and isAMember is a boolean variable that returns only true or false value.
  • Then we define a (for) loop in this loop we check condition we use if block. In if block, we check that currentMembers array value is equal to memberID. if this condition is true so we assign value true in "isAMember" variable and print its value.