Q:

(Generic PriorityQueue using Comparator) Revise MyPriorityQueue in Listing 24.9, using a generic parameter for comparing objects. Define a new constructor with a Comparator as its argument as follows:

0

(Generic PriorityQueue using Comparator) Revise MyPriorityQueue in Listing 24.9, using a generic parameter for comparing objects. Define a new constructor with a Comparator as its argument as follows:

PriorityQueue(Comparator<? super E> comparator)

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

public class Circle
		extends GeometricObject {
	private double radius;

	public Circle() {
	}

	public Circle(double radius) {
		this.radius = radius;
	}

	public Circle(double radius, 
		String color, boolean filled) {
		this.radius = radius;
		setColor(color);
		setFilled(filled);
	}

	/** Return radius */
	public double getRadius() {
		return radius;
	}

	/** Set a new radius */
	public void setRadius(double radius) {
		this.radius = radius;
	}

	@Override /** Return area */
	public double getArea() {
		return radius * radius * Math.PI;
	}

	/** Return diameter */
	public double getDiameter() {
		return 2 * radius;
	}

	@Override /** Return perimeter */
	public double getPerimeter() {
		return 2 * radius * Math.PI;
	}

	@Override /** Override the toString method in the Object class */
	public String toString() {
		return super.toString() + ", Circle, Created: " 
			+ getDateCreated() + ", Radius: " + radius;
	}
}

Exercise_24_06.java

/*********************************************************************************
* (Generic PriorityQueue using Comparator) Revise MyPriorityQueue in Listing     *
* 24.9, using a generic parameter for comparing objects. Define a new            *
* constructor with a Comparator as its argument as follows:                      *
*                                                                                *
* PriorityQueue(Comparator<? super E> comparator)                                *
*********************************************************************************/
import java.util.Comparator;

public class Exercise_24_06 {
	public static void main(String[] args) {
		// Create 4 GeometricObjects
		GeometricObject object1 = new Circle(5);
		GeometricObject object2 = new Rectangle(4, 5);
		GeometricObject object3 = new Circle(6.5);
		GeometricObject object4 = new Rectangle(2.4, 5);

		// Create a priority queue
		MyPriorityQueue<GeometricObject> priorityQueue
			= new MyPriorityQueue<>(new GeometricObjectComparator());

		// Enqueue objects
		priorityQueue.enqueue(object1);
		priorityQueue.enqueue(object2);
		priorityQueue.enqueue(object3);
		priorityQueue.enqueue(object4);

		// Dequeue and display the area of each object
		System.out.printf("%.2f ", priorityQueue.dequeue().getArea());
		System.out.printf("%.2f ", priorityQueue.dequeue().getArea());
		System.out.printf("%.2f ", priorityQueue.dequeue().getArea());
		System.out.printf("%.2f ", priorityQueue.dequeue().getArea());
		System.out.println();
	}
}

GeometricObject.java

public abstract class GeometricObject {
	private String color = "white";
	private boolean filled;
	private java.util.Date dateCreated;

	/** Construct a default geometric object */
	protected GeometricObject() {
		dateCreated = new java.util.Date();
	}

	/** Construct a geometric object with color and filled value */
	protected GeometricObject(String color, boolean filled) {
		dateCreated = new java.util.Date();
		this.color = color;
		this.filled = filled;
	}

	/** Return color */
	public String getColor() {
		return color;
	}

	/** Set a new color */
	public void setColor(String color) {
		this.color = color;
	}

	/** Return filled. Since filled is boolean,
	 *  the get method is named isFilled */
	public boolean isFilled() {
		return filled;
	}

	/** Set a new filled */
	public void setFilled(boolean filled) {
		this.filled = filled;
	}

	/** Get dateCreated */
	public java.util.Date getDateCreated() {
		return dateCreated;
	}

	@Override
	public String toString() {
		return "created on " + dateCreated + "\ncolor: " + color +
			" and filled: " + filled;
	}

	/** Abstract method getArea */
	public abstract double getArea();

	/** Abstract method getPerimeter */
	public abstract double getPerimeter();
}

GeometricObjectComparator.java 

import java.util.Comparator;

public class GeometricObjectComparator
		implements Comparator<GeometricObject>, java.io.Serializable {
	public int compare(GeometricObject o1, GeometricObject o2) {
		double area1 = o1.getArea();
		double area2 = o2.getArea();

		if (area1 < area2)
			return -1;
		else if (area1 == area2)
			return 0;
		else
			return 1;
	}
}

Heap.java

import java.util.Comparator;
import java.util.ArrayList;

public class Heap <E>{
	private Comparator<? super E> comparator;
	private ArrayList<E> list = new ArrayList<>();


	/** Create a default heap */
	public Heap() {
	}

	/** Create a heap from an array of objcets using comparator */
	public Heap(Comparator<? super E> comparator) {
		this.comparator = comparator;
	}

	/** Create a heap from an array of objects using comparator */
	public Heap(E[] objects, Comparator<? super E> comparator) {
		this.comparator = comparator;
		for (int i = 0; i < objects.length; i++) {
			add(objects[i]);
		}
	}

	/** Add a new object into the heap */
	public void add(E newObject) {
		list.add(newObject); // Append to the heap
		int currentIndex = list.size() - 1; // The index of the last node

		while (currentIndex > 0) {
			int parentIndex = (currentIndex - 1) / 2;
			// Swap if the current object is greater than its parent
			
			if (comparator.compare(list.get(currentIndex), 
				list.get(parentIndex)) > 0) {
				E temp = list.get(currentIndex);
				list.set(currentIndex, list.get(parentIndex));
				list.set(parentIndex, temp);
			}
			else
				break; // The tree is a heap now

			currentIndex = parentIndex;
		}
	}

	/** Remove the root from the heap */
	public E remove() {
		if (list.size() == 0) return null;

		E removedObject = list.get(0);
		list.set(0, list.get(list.size() - 1));
		list.remove(list.size() - 1);

		int currentIndex = 0;
		while (currentIndex < list.size()) {
			int leftChildIndex = 2 * currentIndex + 1;
			int rightChildIndex = 2 * currentIndex + 2;

			// Find the maximum between two children
			if (leftChildIndex >= list.size()) break; // The tree is a heap
			int maxIndex = leftChildIndex;
			if (rightChildIndex < list.size()) {
				if (comparator.compare(list.get(maxIndex),
					list.get(rightChildIndex)) < 0) {
					maxIndex = rightChildIndex;
				}
			}

			// Swap if the current node is less than the maximum
			if (comparator.compare(list.get(currentIndex),
				list.get(maxIndex)) < 0) {
				E temp = list.get(maxIndex);
				list.set(maxIndex, list.get(currentIndex));
				list.set(currentIndex, temp);
				currentIndex = maxIndex;
			}
			else
				break; // The tree is a heap
		}

		return removedObject;
	}

	/** Get the number of nodes in the tree */
	public int getSize() {
		return list.size();
	}
}

MyPriorityQueue.java

import java.util.Comparator;

public class MyPriorityQueue<E>{
	private Comparator<? super E> comparator;
	private Heap<E> heap;

	MyPriorityQueue(Comparator<? super E> comparator) {
		this.comparator = comparator; 
		this.heap = new Heap<>(comparator);
	}

	public void enqueue(E newObject) {
		heap.add(newObject);
	}

	public E dequeue() {
		return heap.remove();
	}

	public int getSize() {
		return heap.getSize();
	}
}

Rectangle.java

public class Rectangle
		extends GeometricObject {
	private double width;
	private double height;

	public Rectangle() {
	}

	public Rectangle(
		double width, double height) {
		this.width = width;
		this.height = height;
	}

	public Rectangle(
		double width, double height, String color, boolean filled) {
		this.width = width;
		this.height = height;
		setColor(color);
		setFilled(filled);
	}

	/** Return width */
	public double getWidth() {
		return width;
	}

	/** Set a new width */
	public void setWidth(double width) {
		this. width = width;
	}

	/** Return height */
	public double getheight() {
		return height;
	}

	/** Set a new height */
	public void setheight(double height) {
		this.height = height;
	}

	@Override /** Return area */
	public double getArea() {
		return width * height;
	}

	@Override /** Return perimeter */
	public double getPerimeter() {
		return 2 * (width * height);
	}

	@Override /** Override the toString method in the Object class */
	public String toString() {
		return super.toString() + " Rectangle, Created: " 
			+ getDateCreated() + ", Width: " + width + 
			", Height: " + height;
	}
}

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now