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...
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...
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....
Java: ModifyStudentList.javato use anArrayListinstead of an array In StudentList.java, create two new public methods: The addStudent...
Java: ModifyStudentList.javato use anArrayListinstead of an array In StudentList.java, create two new public methods: The addStudent method should have one parameter of type Student and should add the given Student to the StudentList. The removeStudent method should have one parameter of type String. The String is the email of the student to be removed from the StudentList. In Assign5Test.java do the following: Create a new StudentList containing 3 students. Print the info of all the Students in the StudentList using...
in jGRASP INVENTORY CLASS You need to create an Inventory class containing the private data fields,...
in jGRASP INVENTORY CLASS You need to create an Inventory class containing the private data fields, as well as the methods for the Inventory class (object). Be sure your Inventory class defines the private data fields, at least one constructor, accessor and mutator methods, method overloading (to handle the data coming into the Inventory class as either a String and/or int/float), as well as all of the methods (methods to calculate) to manipulate the Inventory class (object). The data fields...
<<<<<<<< I need only the UML diagram for ALL classes.Java???????????? public class House {    private...
<<<<<<<< I need only the UML diagram for ALL classes.Java???????????? public class House {    private int houseNumber;    private int bedrooms;    private int sqFeet;    private int year;    private int cost;    public House(int houseNumber,int bedrooms,int sqFeet, int year, int cost)    {        this.houseNumber = houseNumber;        this.bedrooms = bedrooms;        this.sqFeet = sqFeet;        this.year = year;        this.cost = cost;    }    public int getHouseNumber()    {        return houseNumber;    }   ...
Hello. I have an assignment that is completed minus one thing, I can't get the resize...
Hello. I have an assignment that is completed minus one thing, I can't get the resize method in Circle class to actually resize the circle in my TestCircle claass. Can someone look and fix it? I would appreciate it! If you dont mind leaving acomment either in the answer or just // in the code on what I'm doing wrong to cause it to not work. That's all I've got. Just a simple revision and edit to make the resize...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT