Question

Please solve this problem in java. import java.util.Arrays; public class PriorityQueue { /* This class is...

Please solve this problem in java.

import java.util.Arrays;

public class PriorityQueue {

/* This class is finished for you.
*/
private static class Customer implements Comparable {
private double donation;

public Customer(double donation) {
this.donation = donation;
}

public double getDonation() {
return donation;
}

public void donate(double amount) {
donation += amount;
}

public int compareTo(Customer other) {
double diff = donation - other.donation;
if (diff < 0) {
return -1;
} else if (diff > 0) {
return 1;
} else {
return 0;
}
}

public String toString() {
return String.format("$%.2f", donation);
}
}

private Customer[] data;
private int size;

public PriorityQueue(int capacity) {
data = new Customer[capacity];
size = 0;
}
  
public PriorityQueue() {
this(10);
}

/* Add a customer to the queue.
* Remember to do so in a way that keeps the queue in sorted order!
*/
public void push(Customer customer) {

}

/* Remove and return the highest priority customer from the queue.
*/
public Customer pop() {
return null;
}

/* Return, but don't remove, the highest priority item from the queue.
*/
public Customer peek() {
return null;
}

/* Given the index of a customer, increase their donation amount, letting
* them cut in line if necessary.
*
* Refer to the Customer class to remind yourself the operations you can do
* on a customer.
*/
public void bump(int customerIndex, double amount) {
  
}

public String toString() {
return Arrays.toString(data);
}

public static void main(String[] args) {
PriorityQueue line = new PriorityQueue(6);
  
line.push(new Customer(5.00));
line.push(new Customer(10.00));
line.push(new Customer(1.00));

System.out.println(line.pop());
System.out.println(line.pop());

line.push(new Customer(20.00));
line.push(new Customer(15.00));
line.push(new Customer(2.00));

line.bump(1, 30.00);
line.bump(3, 60.00);
System.out.println(line.pop());
System.out.println(line.peek());
System.out.println(line.pop());
System.out.println(line.pop());
System.out.println(line.pop());
}
}

RESULT

$10.00
$5.00
$61.00
$45.00
$45.00
$20.00
$2.00

Homework Answers

Answer #1

import java.util.Arrays;

public class PriorityQueue {

/* This class is finished for you.
*/
private static class Customer implements Comparable<Customer> {
private double donation;

public Customer(double donation) {
this.donation = donation;
}

public double getDonation() {
return donation;
}

public void donate(double amount) {
donation += amount;
}

public int compareTo(Customer other) {
double diff = donation - other.donation;
if (diff < 0) {
return -1;
} else if (diff > 0) {
return 1;
} else {
return 0;
}
}

// If following changes can be made in compareTo() function then reversing of an array is not required

// public int compareTo(Customer other) {
// double diff = donation - other.donation;
// if (diff < 0) {
// return 1;
// } else if (diff > 0) {
// return -1;
// } else {
// return 0;
// }
// }

public String toString() {
return String.format("$%.2f", donation);
}
}

private Customer[] data;
private int size;

public PriorityQueue (int capacity) {
data = new Customer[capacity];
size = 0;
}
  
public PriorityQueue () {
this(10);
}
public void push(Customer customer) {

// Pushed data in queue and sorted the queue
data[size]=customer;

// After pushing each element the size is increased
size++;

// Arrays sorted in ascending order due to the given compareTo() function and
// so the array is reversed   
Arrays.sort(data,0,size);
int i; Customer t;
for (i = 0; i < size / 2; i++) {
t = data[i];
data[i] = data[size - i - 1];
data[size - i - 1] = t;
}
}

public Customer pop() {
  

Customer cust=data[0];

// Shifting all the elements after popping the top element and decreasing the size
for(int i=0;i<size-1;i++)
data[i]=data[i+1];
size--;
return cust;

}

public Customer peek() {
return data[0];
}


public void bump(int customerIndex, double amount) {

// Called function donate in customer class to further
// add the amount

data[customerIndex].donate(amount);


// Now the value has changed of an element hence, may not be sorted
// So sorted again and then reversed


Arrays.sort(data,0,size);
int i; Customer t;
for (i = 0; i < size / 2; i++) {
t = data[i];
data[i] = data[size - i - 1];
data[size - i - 1] = t;
}
}

public String toString() {
return Arrays.toString(data);
}

public static void main(String[] args) {
PriorityQueue line = new PriorityQueue (6);
  
line.push(new Customer(5.00));
line.push(new Customer(10.00));
line.push(new Customer(1.00));

System.out.println(line.pop());
System.out.println(line.pop());

line.push(new Customer(20.00));
line.push(new Customer(15.00));
line.push(new Customer(2.00));

line.bump(1, 30.00);
line.bump(3, 60.00);
System.out.println(line.pop());
System.out.println(line.peek());
System.out.println(line.pop());
System.out.println(line.pop());
System.out.println(line.pop());
}
}

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
please fix code to delete by studentname import java.util.Scanner; public class COurseCom666 {     private String...
please fix code to delete by studentname import java.util.Scanner; public class COurseCom666 {     private String courseName;     private String[] students = new String[1];     private int numberOfStudents;     public COurseCom666(String courseName) {         this.courseName = courseName;     }     public String[] getStudents() {         return students;     }     public int getNumberOfStudents() {         return numberOfStudents;     }     public void addStudent(String student) {         if (numberOfStudents == students.length) {             String[] a = new String[students.length + 1];            ...
Please explain code 1 and code 2 for each lines code 1 public class MyQueue {...
Please explain code 1 and code 2 for each lines code 1 public class MyQueue {    public static final int DEFAULT_SIZE = 10;    private Object data[];    private int index; code 2 package test; import java.util.*; /* Class Node */ class Node { protected Object data; protected Node link; /* Constructor */ public Node() { link = null; data = 0; } /* Constructor */ public Node(Object d,Node n) { data = d; link = n; } /*...
Which method is correct to access the value of count? public class Person { private String...
Which method is correct to access the value of count? public class Person { private String name; private int age; private static int count = 0; } A. private int getCount() {return (static)count;} B. public static int getCount() {return count;} C. public int getCount() {return static count;} D. private static int getCount() {return count;} How can you print the value of count? public class Person { private String name; private int age; private static int count=0; public Person(String a, int...
import java.io.PrintStream; import java.util.Arrays; public class joker { public static int smallest(int[] v1, int[] v2) {...
import java.io.PrintStream; import java.util.Arrays; public class joker { public static int smallest(int[] v1, int[] v2) { return 0; } public static int[] convert1(String str) { return new int[1]; } public static String convert2(int[] v) { return ""; } public static void main(String[] args) { testSmallest(); testConvert(); } public static void testSmallest() { System.out.println("Testing your method smallest(...)"); int[][] testVectors1 = new int[][]{{1, 2, 3}, {1, 2, 3, 4}, {1, 2, 3}, {1, 2, 3}, {2, 3, 4}}; int[][] testVectors2 = new...
Provide A UML for the Following CODE public class Employee{ public String strName, strSalary; public Employee(){...
Provide A UML for the Following CODE public class Employee{ public String strName, strSalary; public Employee(){    strName = " ";    strSalary = "$0";    } public Employee(String Name, String Salary){    strName = Name;    strSalary = Salary;    } public void setName(String Name){    strName = Name;    } public void setSalary(String Salary){    strSalary = Salary;    } public String getName(){    return strName;    } public String getSalary(){    return strSalary;    } public String...
JAVA public class Purchase { private String name; private int groupCount; //Part of price, like the...
JAVA public class Purchase { private String name; private int groupCount; //Part of price, like the 2 in 2 for $1.99. private double groupPrice; //Part of price, like the $1.99 in 2 for $1.99. private int numberBought; //Total number being purchased. public Purchase () { name = "no name"; groupCount = 0; groupPrice = 0; numberBought = 0; } public Purchase (String name, int groupCount, double groupPrice, int numberBought) { this.name = name; this.groupCount = groupCount; this.groupPrice = groupPrice; this.numberBought...
How do I make this: public class Country {     private String name;     private double area;     private...
How do I make this: public class Country {     private String name;     private double area;     private int population;     public Country(String name, double area, int population) {         this.name = name;         this.area = area;         this.population = population;     }     public double getPopulationDensity() {         return population / area;     }     public String getName() {         return name;     }     public void setName(String name) {         this.name = name;     }     public double getArea() {         return area;     }     public void setArea(double area) {         this.area = area;     }     public int getPopulation()...
import java.util.Scanner; public class AroundTheClock {    public static void main(String[] args)    {       ...
import java.util.Scanner; public class AroundTheClock {    public static void main(String[] args)    {        Scanner input = new Scanner(System.in);        int departureTime = input.nextInt();        int travelTime = input.nextInt();        int arrivalTime;            departureTime =12;            departureTime = 0;            arrivalTime = (int) (departureTime + travelTime);            (arrivalTime = (arrivalTime >=12));            if (arrivalTime = arrivalTime %12)            {            System.out.println(arrivalTime);...
import java.util.Scanner; import java.util.Random; public class DiceRoll {    public static final int SIDES = 6;...
import java.util.Scanner; import java.util.Random; public class DiceRoll {    public static final int SIDES = 6;    public static void main(String[] args) {        // TODO Auto-generated method stub        System.out.println("Enter the number of times a 6 sided die should be rolled ");        Scanner keyboard = new Scanner(System.in);        Random r = new Random();               int times = keyboard.nextInt();        boolean valid = false;        while(!valid) {           ...
1) Consider the following Java program, which one of the following best describes "setFlavor"? public class...
1) Consider the following Java program, which one of the following best describes "setFlavor"? public class Food {     static int count;     private String flavor = "sweet";     Food() { count++; }     void setFlavor(String s) { flavor = s; }     String getFlavor() { return flavor; }     static public void main(String[] args) {         Food pepper = new Food();         System.out.println(pepper.getFlavor());     } } a. a class variable b. a constructor c. a local object variable d....
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT