**JAVA LANGUAGE** beginner level import java. util.Arrays; import java.util.Scanner; Suppose that a Scanner object contains a bunch of tokens all on a single line separated by spaces. For example:...


**JAVA LANGUAGE** beginner level


import java. util.Arrays;
import java.util.Scanner;



Suppose that a Scanner object contains a bunch of tokens all on a single line separated by spaces. For example:


100.5 30.4 pizza 56.8 1000.3 8.2


Also suppose that you have a particular item that you know the price of. Let’s use the following example:


Kindle: 99.99


Write a static method:public static double priceIsRight(Scanner s, double price)that takes a Scanner and an item price as parameters and returns the bid that is closest to the item price but not greater than the item price. If all bids are greater than the price, return 0.0. If there are no bids (Scanner is empty) return 0.0. You can assume the Scanner is not null.



In the above example. 56.8 should be returned, since it is the number that is closest to 99.99 but not over 99.99.



Notice that there may be some invalid tokens in the Scanner's input. The above example has pizza as one of its tokens, which is clearly not a valid bid. Your method should throw these tokens away and not consider them as bids.



It is probably easiest to test your method by creating a Scanner with a String instead of System.in.



public static void main(String[] args) {



String bids = "100.5 30.4 pizza 56.8 1000.3 8.2";


Scanner scanner = new Scanner(bids);


double price = 99.99;


double expectedWinningBid = 56.8;


System.out.println("Expected priceIsRight result for the bids \"" + bids + "\" and the price " + price + " to be " + expectedWinningBid +


" and got " + priceIsRight(scanner, price));



{




Tests to include in your main method:



  • Test the above example with “100.5 30.4 pizza 56.8 1000.3 8.2” as the tokens and 99.99 as the price. The result should be 56.8.

  • Test a Scanner that is created using an empty String as the source and any number for the price. The result should be 0.0.

  • Include a test where all the bids are greater than the price. The result should be 0.0.

  • Include some additional tests that verify that invalid (non-numeric) tokens are filtered out. (Make sure InputMismatchExceptions do not occur!).

Jun 03, 2022
SOLUTION.PDF

Get Answer To This Question

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here