Han

Han Ferik

Home | Projects | AP CSA

Silent Auction

Tools: VSCode | Source Code: GitHub

This Java program is designed to determine the winner of a silent auction. The auction involves participants placing bids with their names, and the winner is the person who placed the highest bid. If there is a tie, the person who placed their bid first is chosen as the winner. The program begins by reading the number of bids that were made in the auction. For each bid, it reads the participant's name and the corresponding bid amount.

The program then tracks the highest bid and the name of the person who made it. It initializes two variables: one for the highest bid (highestBid) and one for the winner's name (winner). As the program processes each bid, it compares the current bid with the highest bid so far. If the current bid is greater, the program updates the highest bid and sets the winner to the current participant’s name. If the current bid is equal to the highest bid, the winner remains unchanged, ensuring that the first person to place the highest bid wins in the case of a tie.

Finally, after all the bids have been processed, the program prints the name of the person with the highest bid. The program uses a Scanner object to handle the input and closes it at the end to release resources. The solution runs in linear time, O(n), where n is the number of bids, making it efficient for handling a large number of bids.


SilentAuction Java Code

SilentAuction Java Code


    import java.util.Scanner;
    
    public class SilentAuction {
        public static void main(String[] args) {
            Scanner scanner = new Scanner(System.in);
            
            int n = scanner.nextInt();
            scanner.nextLine();
            
            String winner = "";
            int highestBid = 0;
            
            for (int i = 0; i < n; i++) {
                String name = scanner.nextLine();
                int bid = scanner.nextInt();
                scanner.nextLine();
                
                if (bid > highestBid) {
                    highestBid = bid;
                    winner = name;
                }
            }
            
            System.out.println(winner);
            
            scanner.close();
        }
    }
        

Find me on the interwebs!

Github LinkedIn Instagram Facebook