Write a method priceIsRight in JAVA that accepts an array of integers bids and a
ID: 3557052 • Letter: W
Question
Write a method priceIsRight in JAVA 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:
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
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
import java.util.*;
public class tester
{
public static int priceIsRight (int[] array,int bid)
{
int count = 0;
for(int i=0; i<array.length; i++)
{
if(array[i] > bid) count++;
}
if(count==array.length) return -1;
return 1;
}
public static void main(String[] args)
{
int[] array = new int[]{900, 885, 989, 1};
System.out.println("Value of priceIsRight is "+ priceIsRight(array,800));
int[] array1 = new int[]{200};
System.out.println("Value of priceIsRight is "+ priceIsRight(array1,120));
int[] array2 = new int[] {500, 300, 241, 99, 501};
System.out.println("Value of priceIsRight is "+ priceIsRight(array2,50));
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.