Question
Hopefully someone can help me with my assignment. I tried to add some code but not sure I'm doing it right.. Thanks! import java.io.PrintWriter; import
Hopefully someone can help me with my assignment. I tried to add some code but not sure I'm doing it right.. Thanks!
import java.io.PrintWriter; import java.util.Scanner; public class VickreyAuction { public static void main(String[] args) { try (Scanner input = new Scanner(System.in)) { try (PrintWriter pw = new PrintWriter(System.out)) { run(input,pw); } } } public static void run(Scanner input, PrintWriter output) { // TODO: // 1. read bids
Bid bids[] = Bid.readBids(input, output);
// 2. check that we have enough bids
// 3. figure out the winner and print the price they pay
import java.io.PrintWriter; import java.util.Scanner;
public class Bid {
private String bidder; private double price;
public Bid(String bidder, double price) { this.bidder = bidder; this.price = price; }
public double getPrice() { return price; }
public String getBidder() { return bidder; }
public static Bid fromString(String str) {
}
public static Bid[] readBids(Scanner scanner, PrintWriter output) {
}
Program Input: The programs by accepting bids as input lines on the console. Each bid has the form price name-or-email Blank lines are ignored, and end-of-file ends the auction. The price is any number, perhaps with a decimal point. Even negative bids are legal. There can be spaces or tabs before and after the price and must be at least one after. Any spaces or tabs at the end of the line are ignored (removed). If the price includes any illegal characters or otherwise cannot be parsed as a double, an error message is printed, but the auction continues. There must be a name or email (of any form - accept any sequences of characters) after the spaces/tabs after the price. Anonymous bids will also be flagged as errors, and not be entered into the auction. When the auction is over, the program will report the winner and the price they will pay, for example Winner: Charlie Price: 200.0 Make sure that the winner is the name of the highest bidder but the price reported is the price of the second highest bid. Design: The design for this program is that you will implement a class Bid in the edu.uwm.apc430 package (do not put it in a different package!) which has two fields, price and bidder, each with getters. The class needs a constructor taking a bidder name and a price. It also needs a static method fromString which takes a string and returns an instance of the class, or throws a NumberFormatException explaining the problem. Sometimes students are tempted to try to use split to get the name and bid from the string. This won't work! The problem is that the name can have spaces and tabs withing it. Instead you will need to examine individual characters (charAt) and find bounds for a substring request. Class Bid needs another static method readBids that takes a Scanner instance (for input) and a PrintWriter (for output) and reads lines from the scanner, creating a Bid object for each line (using fromString, of course). It will return an array of all the bids read, and print a message (to the PrintWriter) for each problem encountered (essentially, each time it "catch"es an error thrown by fromString). How does it know how big to make the array? It does not. So, first it creates an ArrayList and adds each bid as it is read to the list. Then it uses toArray to fill an array that is passed in and which has the correct size. We also have a class VickreyAuction with two static methods: one called run that (as with readBids) takes a Scanner and a PrintWriter. It reads bids from the scanner and then performs a Vickrey auction with the bids read. If there were no bids or only bid, it should print a message to that effect. Otherwise it should print two lines: 1 . The text "Winner: " followed by the name of the winning bidder. 2. The text "Price:" followed by the price they pay (usually hot their bid). The class VickreyAuction also has a main method that simply runs the auction on the system input and output, so you can run it on the console. That last main method we have written for you in a "skeleton" file for the class-a partially written class. Places where you need to modify/add things are marked as // TODO which will be highlighted in Eclipse. It's usually a bad idea to change the existing code or add other things, other than to the "import" section of the file
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started