Answer:
Here is the method :
public static int priceIsRight(int[] bids, int correctPrice) { /*definition of method priceIsRight that accepts as parameters an array of integers representing the contestants’ bids and an integer representing a correct price */
int nearest = -1; //initializes the closest value to -1
for(int i = 0; i < bids.length; i++) { //iterates through the bids array until length of the bids array reaches
if(bids[i] <= correctPrice && bids[i] > nearest) //checks the element of the bids array that is closest (nearest) in value to the correct price without being larger than that price
nearest = bids[i]; } //sets that element of bids at index i which is closest to correcPrice to nearest variable
return nearest; } //returns the element in the bids array that is nearest in value to the correctPrice
Step-by-step explanation:
To check the working of the above method you can call this function in main()
public static void main(String[] args) { //start of main function
int[] bids = {200, 300, 250, 1, 950, 40}; //declares and array and assigns elements to it
System.out.println(priceIsRight(bids,280)); } } //calls priceIsRight method by passing the bids array and 280 as correct price
The method works as follows:
Suppose we have an array called bids with elements {200, 300, 250, 1, 950, 40}
nearest = -1
correctPrice = 280
The for loop for(int i = 0; i < bids.length; i++) works as follows:
At first iteration:
i = 0
i< bids.length is true because i=0 and bids.length = 6
so program control moves inside the loop body:
if(bids[i] <= correctPrice && bids[i] > nearest) this if statement becomes:
if(bids[0] <= correctPrice && bids[0] > nearest)
This if condition has two parts and it uses && AND logical operator between them so both of the conditions should hold for the if condition to evaluate to true.
bids[0] <= correctPrice
the element at 0-th index of bids array is 200 and correctPrice= 280 So 200 is less than 280. This part of if condition is true.
bids[0] > nearest
the element at 0-th index of bids array is 200 and nearest = -1. So 200 is greater than -1. Hence this part of if condition is also true.
Hence both the parts of the if statement are true and if condition evaluates to true. So the program control moves to the statement:
nearest = bids[i];
This becomes:
nearest = bids[0]
nearest = 200
At second iteration:
i = 1
i< bids.length is true as i=1 and bids.length = 6
so program control moves inside the loop body:
if(bids[1] <= correctPrice && bids[1] > nearest) this if statement becomes:
if(bids[1] <= correctPrice && bids[1] > nearest)
bids[1] <= correctPrice
the element at 1st index of bids array is 300 and correctPrice= 280 So 300 is greater than 280. This part of if condition is false.
So the if condition evaluates to false. So the value of nearest remains the same.
nearest = 200
At third iteration:
i = 2
i< bids.length is true as i=2 and bids.length = 6
if statement becomes:
if(bids[2] <= correctPrice && bids[2] > nearest)
bids[0] <= correctPrice
the element at 2nd index of bids array is 250 and correctPrice= 280 So 250 is less than 280. This part of if condition is true.
bids[2] > nearest
the element at 2nd index of bids array is 250 and nearest = 200. So 250 is greater than 200. Hence this part of if condition is also true.
Hence if condition evaluates to true. So the program control moves to the statement:
nearest = bids[i];
nearest = bids[2]
nearest = 250
At fourth iteration:
i = 3
i< bids.length is true because i=1 and bids.length = 6
if statement becomes:
if(bids[3] <= correctPrice && bids[1] > nearest)
bids[3] <= correctPrice
the element at third index of bids array is 1 and correctPrice= 280 So 1 is less than 280. This part of if condition is true.
bids[3] > nearest
the element at 3rd index of bids array is 1 and nearest = 250. So 1 is less than 200. Hence this part of if condition is false. So value of nearest remains the same
nearest = 250
At fifth iteration:
i = 4
i< bids.length is true because i=4 and bids.length = 6
if statement becomes:
if(bids[4] <= correctPrice && bids[4] > nearest)
bids[4] <= correctPrice
the element at 4th index of bids array is 950 and correctPrice= 280 So 950 is greater than 280. This part of if condition is false.
So the if condition evaluates to false. Hence
nearest = 250
At sixth iteration:
i = 5
i< bids.length is true because i=5 and bids.length = 6
so program control moves inside the loop body:
if statement becomes:
if(bids[5] <= correctPrice && bids[5] > nearest)
bids[5] <= correctPrice
the element at fifth index of bids array is 40 and correctPrice= 280 So 40 is less than 280. This part of if condition is true.
bids[5] > nearest
the element at 5th index of bids array is 40 and nearest = 250. So 40 is less than 250. Hence this part of if condition is false. So value of nearest remains the same
nearest = 250
Now the loop breaks at i = 6 because i < bids.length condition evaluates to false.
The statement return nearest; executes now. This returns the value of nearest. So this method returns 250.