Question

In: Computer Science

package construction; public class Bid{ private String contractor; private float price; public Bid(String contractor, float price)...

package construction;
public class Bid{
    private String contractor;
    private float price;

    public Bid(String contractor, float price) {
        this.contractor = contractor;
        this.price = price;
    }

    public String getContractor() {        return contractor;     }

    public float getPrice()       {        return price;    }

}
package construction;

public class ContractorBids{
    // assume proper variables and other methods are here

    public void winningBid(Bid[] bids, int numBids){

        /****************************
         *  your code would go here *
         ****************************/

    }
}

You are doing renovations on your building, and multiple contractors have given bids. Given the above Bid and ContractorBids class, write code that should go in the area marked * your code would go here * to complete the “winningBid(Bid[] bids, int numBids)” method. The output should be printed to the console (screen). There are numBids Bids in the array Bid[] bids. These items are in locations 0 to numBids - 1 of the array.

Each Bid item should have its contractor and price output on a single line. There will be marks for properly right justifying the price and outputting two decimals. As you are printing the Bid objects, you should keep track of the Bid object with the lowest price. At the end print the contractor name followed by the price, a finder's fee of 10% and the total price.  For example, if the Bid array had 3 Bids, a Carl's Construction with $40 000, a Pat's Paving for $50 000, and Debbie's Demolition for $30 000, we would get the following output.

 

Top Bids:

Carl's Construction 40000.00

Pat's Paving 50000.00

Debbie's Demolition     30000.00

The winning bid goes to Debbie's Demolition.

Price: $30000.00 Finder's Fee: $3000.00 Total: $33000.00

Solutions

Expert Solution

Solution:

Here is the code for the winningBid method:

public void winningBid(Bid[] bids, int numBids){
        float min_bid = 0f; // Variable to hold minimum Bid price
        float finder_fee = 0f; // Variable to hold the finder fee (10%)
        String min_contract = ""; // Variable to hold the Contractor name for the minimum Bid price
        min_bid = bids[0].getPrice(); // Assume that the first Bid has the minimum Bid price
        // For every subsequent Bid check if the Bid price is less than the minimum Bid price.
        // If yes, then make that Bid price as the minimum Bid price. Also, capture the Contractor's name for the minimum Bid price.
        for (int i = 1; i<numBids; i++){
            if (bids[i].getPrice() < min_bid){
                min_bid = bids[i].getPrice();
                min_contract = bids[i].getContractor();
            }
        }
        finder_fee = 0.1f * min_bid; // Calculating the 10% finder fee on the minimum Bid price.
        float total = finder_fee + min_bid; //Calculating the total
        System.out.println("Top Bids:");
        // Print the Contractor's name and the bid price for every Bid in the array.
        for(Bid num : bids){
            System.out.println(num.getContractor() + " " + String.format ("%.2f",num.getPrice()));
        }
        // Print the winning bid with the minimum Bid price.
        System.out.println("The winning bid goes to " + min_contract + ".");
        // Print the Price, Finder's Fee and the Total.
        System.out.println("Price: $" + String.format ("%.2f",min_bid) + " Finder's Fee: $" + String.format ("%.2f",finder_fee) + " Total: $" + String.format ("%.2f",total));
    }

Also, for testing purpose, here is the complete code with the example provided in the question:

package construction;

public class Bid {
    private String contractor;
    private float price;

    public Bid(String contractor, float price) {
        this.contractor = contractor;
        this.price = price;
    }

    public String getContractor() {        return contractor;     }

    public float getPrice()       {        return price;    }
}

package construction;

public class ContractorBids {


    public void winningBid(Bid[] bids, int numBids){
        float min_bid = 0f; // Variable to hold minimum Bid price
        float finder_fee = 0f; // Variable to hold the finder fee (10%)
        String min_contract = ""; // Variable to hold the Contractor name for the minimum Bid price
        min_bid = bids[0].getPrice(); // Assume that the first Bid has the minimum Bid price
        // For every subsequent Bid check if the Bid price is less than the minimum Bid price.
        // If yes, then make that Bid price as the minimum Bid price. Also, capture the Contractor's name for the minimum Bid price.
        for (int i = 1; i<numBids; i++){
            if (bids[i].getPrice() < min_bid){
                min_bid = bids[i].getPrice();
                min_contract = bids[i].getContractor();
            }
        }
        finder_fee = 0.1f * min_bid; // Calculating the 10% finder fee on the minimum Bid price.
        float total = finder_fee + min_bid; //Calculating the total
        System.out.println("Top Bids:");
        // Print the Contractor's name and the bid price for every Bid in the array.
        for(Bid num : bids){
            System.out.println(num.getContractor() + " " + String.format ("%.2f",num.getPrice()));
        }
        // Print the winning bid with the minimum Bid price.
        System.out.println("The winning bid goes to " + min_contract + ".");
        // Print the Price, Finder's Fee and the Total.
        System.out.println("Price: $" + String.format ("%.2f",min_bid) + " Finder's Fee: $" + String.format ("%.2f",finder_fee) + " Total: $" + String.format ("%.2f",total));
    }


}

package construction;

public class ContractBidInput {
    public static void main(String[] args) {
        Bid c1 = new Bid("Carl's Construction", 40000);
        Bid c2 = new Bid("Pat's Paving", 50000);
        Bid c3 = new Bid("Debbie's Demolition", 30000);
        ContractorBids cbid = new ContractorBids();

        Bid arr[] = new Bid[]{c1, c2, c3};
        cbid.winningBid(arr,3);
    }
}

Below is the output of the program:

Top Bids:
Carl's Construction 40000.00
Pat's Paving 50000.00
Debbie's Demolition 30000.00
The winning bid goes to Debbie's Demolition.
Price: $30000.00 Finder's Fee: $3000.00 Total: $33000.00


Related Solutions

package compstore; public class Desktop{ private String brand; private float price; public Desktop(String brand, float price)...
package compstore; public class Desktop{ private String brand; private float price; public Desktop(String brand, float price) { this.brand = brand; this.price = price; } public String getBrand() { return brand; } public float getPrice() { return price; } } package compstore; public class DeskTopDeals{ // assume proper variables and other methods are here public void dealOfTheDay(Desktop[] items, int numItems){ /**************************** * your code would go here * ****************************/ } } Given the above Desktop class, write code that should go...
package dealership; public abstract class Vehicle { private float dealerPrice; private int year; public Vehicle(float d,...
package dealership; public abstract class Vehicle { private float dealerPrice; private int year; public Vehicle(float d, int y) { dealerPrice = d; year = y; } public float getDealerPrice() { return dealerPrice; } public int getYear() { return year; } public abstract float getStickerPrice(); } In the space below write a concrete class Car in the dealership package that is a subclass of Vehicle class given above. You will make two constructors, both of which must call the superclass constructor....
package mac286.LinkedLists; public class Postfix { private rStack<String> S; private String inSt; private ourLinkedList<String> inList, outList;...
package mac286.LinkedLists; public class Postfix { private rStack<String> S; private String inSt; private ourLinkedList<String> inList, outList; public Postfix(String s) { S = new rStack<String>(); inSt = s; outList = new ourLinkedList<String>(); inList = new ourLinkedList<String>(); } public void reset(String s) { S = new rStack<String>(); inSt = s; outList = new ourLinkedList<String>(); inList = new ourLinkedList<String>(); } private boolean isOperator(char c) { if(c=='-'||c=='+'||c=='*'||c=='/') return true; return false; } private boolean isParenthesis(char c) { if(c == '(' || c==')') return true;...
public class StringNode { private String item; private StringNode next; } public class StringLL { private...
public class StringNode { private String item; private StringNode next; } public class StringLL { private StringNode head; private int size; public StringLL(){ head = null; size = 0; } public void add(String s){ add(size,s); } public boolean add(int index, String s){ ... } public String remove(int index){ ... } } In the above code add(int index, String s) creates a StringNode and adds it to the linked list at position index, and remove(int index) removes the StringNode at position...
java programing Q: Given the following class: public class Student { private String firstName; private String...
java programing Q: Given the following class: public class Student { private String firstName; private String lastName; private int age; private University university; public Student(String firstName, String lastName, int age, University university) { this.firstName = fisrtName; this.lastName = lastName; this.age = age; this.university = university; } public String getFirstName(){ return firstName; } public String getLastName(){ return lastName; } public int getAge(){ return age; } public University getUniversity(){ return university; } public String toString() { return "\nFirst name:" + firstName +...
In the following class public class Single { private float unique; ... } Create constructor, setter...
In the following class public class Single { private float unique; ... } Create constructor, setter and getter methods, and toString method. Write a program that request a time interval in seconds and display it in hours, minutes, second format. NEED THESE ASAP
Room.java: public class Room { // fields private String roomNumber; private String buildingName; private int capacity;...
Room.java: public class Room { // fields private String roomNumber; private String buildingName; private int capacity; public Room() { this.capacity = 0; } /** * Constructor for objects of class Room * * @param rN the room number * @param bN the building name * @param c the room capacity */ public Room(String rN, String bN, int c) { setRoomNumber(rN); setBuildingName(bN); setCapacity(c); }    /** * Mutator method (setter) for room number. * * @param rN a new room number...
What is a package? What package is the Scanner class in? What package is the String class in?
What is a package? What package is the Scanner class in? What package is the String class in? 
public class Graph { private ST<String, SET<String>> st; public Graph() { st = new ST<String, SET<String>>();...
public class Graph { private ST<String, SET<String>> st; public Graph() { st = new ST<String, SET<String>>(); } public void addEdge(String v, String w) { // Put v in w's SET and w in v's SET. if (!st.contains(v)) st.put(v, new SET<String>()); if (!st.contains(w)) st.put(w, new SET<String>()); st.get(v).add(w); st.get(w).add(v); } public Iterable<String> adjacentTo(String v) { return st.get(v); } public Iterable<String> vertices() { return st.keys(); } // See Exercises 4.5.1-4 for V(), E(), degree(), // hasVertex(), and hasEdge(). public static void main(String[] args)...
public class Person { private String name; public Person() { name = "No name yet"; }...
public class Person { private String name; public Person() { name = "No name yet"; } public Person(String initialName) { name = initialName; } public void setName(String newName) { name = newName; } public String getName() { return name; } public void writeOutput() { System.out.println("Name: " + name); } public boolean hasSameName(Person otherPerson) { return this.name.equalsIgnoreCase(otherPerson.name); } } 2- Write a Program Patient. Java Class that extends Person to include  Social security Gender  Appropriate construtors, accessors, and mutators....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT