public class Accumulator {
private int[] A;
public Accumulator(int[] X) {
A= new int[X.length];
for (int i=0; i<X.length; i++){
A[i] = X[i];
}
}
public int getOverM(int m){
for(int j=0; j<A.length;j++){
if(m < A[j])
{
}
}
}
public static void main(String args[]){ // you can use the main method to test your code
int[] A = {2,4,3,5,8};
int r=new Accumulator(A).getOverM(3); //change argument to test different cases
System.out.println(r);
}
}
Hi I have been working on this for so long i know it seems easy but i just cant get my head around it....I can only change the code inside the method getOverM(), I cannot edit other methods. I thought of using an if statement,but I just do not know how to write a code that shows the next biggest index number compared to the m.
Question:Consider the following Accumulator class with missing code for the method 'getOverM(int m)'. getOverM should return the index of the first element of the array A whose value is greater or equal to m.
If no element in A has index greater or equal to m then the method should return -1. For example if A is the array {2,4,3,5,8} then getOverM(3) will return 1 getOverM(2) will return 0 getOverM(7) will return 4 getOverM(20) will return -1
(Hint: the length of an array A is given by A.length)
Insert the code for the body of the method getOverM.
Aucun commentaire:
Enregistrer un commentaire