Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a method priceIsRight that accepts an array of integers bids and an intege

ID: 3532506 • Letter: W

Question

Write a method priceIsRight that accepts an array of integers bids and an integer price as parameters. The method returns the element in the bids array that is closest in value to price without being larger than price. For example, if bids stores the elements {200, 300, 250, 999, 40}, then priceIsRight(bids, 280) should return 250, since 250 is the bid closest to 280 without going over 280. If all bids are larger than price, then your method should return -1.

The following table shows some calls to your method and their expected results:

You may assume there is at least 1 element in the array, and you may assume that the price and the values in bids will all be greater than or equal to 1. Do not modify the contents of the array passed to your method as a parameter.

Arrays Returned Value int[] a1 = {900, 885, 989, 1}; priceIsRight(a1, 800) returns 1 int[] a2 = {200}; priceIsRight(a2, 120) returns -1 int[] a3 = {500, 300, 241, 99, 501}; priceIsRight(a3, 50) returns -1

Explanation / Answer

public class BidTest {

public static void main(String[] args) {

int[] bids = {200, 300, 250, 999, 40};
System.out.println("Highest Bid: " + new BidTest().priceIsRight(bids, 280));
}
public int priceIsRight(int[] bids, int price) {
int highestBid = -1;
for(int bid: bids) {
if(price - bid >0 && highestBid < bid) {
highestBid = bid;
}
}
return highestBid;
}
}

output:

Highest Bid: 250


Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote