Question

Create a Java application that allows user to build a Priority Queue of Circle Elements (i.e.,...

Create a Java application that allows user to build a Priority Queue of Circle Elements (i.e., priority based on the radius of the circle). The application must be menu controlled similar to Project 4 and provide the following. Allow insertion of a "Circle" object/structure in the Priority Queue data structures. Allow display of all elements from Priority Queue data structure by Invoking a method/function "DisplayPriorityQueue" (uses "DeQueue" method). Allow for deletion of the Queue

This is what I have so far. I am running into issues using  PriorityQueue<>

Circle Class that calculates. I want the PQ to use the final calculation to prioritize the lowest radius entered.

class Circle
{
    private double radius;
    Circle()
    {
        radius = 0; // initialize to 0
    }


    void setRadius(double radius) {
        this.radius = radius;
    }

    private double calculateArea()
    {
        return Math.PI * (radius * radius); // calculation to determine area of the circle
    }

    public String toString()
    {
        return String.format("Radius : %.2f , Area %.2f\n", radius, calculateArea()); // formatted to two decimal
    }
}

Main Class

import java.util.*;

class Main
{
    //create a scanner class
    private static final Scanner console=new Scanner(System.in);
    private static Stack<Circle>circleStack = null;
    private static Queue<Circle>circleQueue = null;
    public static void main(String[] args)
    {
        //create Stack of Circle
        circleStack = new Stack<>();
        //create a Queue of Circle
        circleQueue = new LinkedList<>();
        int selection;
        //call ui method
        selection = ui();
        while(selection != -1)
        {
            switch(selection)
            {
                case 1:
                    createCircleStack();break;
                case 2:
                    createCircleQueue();break;
                case 3:
                    displayStackElement();break;
                case 4:
                    displayQueueElement();break;
                case 5:
                    deleteStackElement();break;
                case 6:
                    deleteQueueElement();break;
            }
            selection = ui();
        }
        System.out.println("Quitting");
    }

    //Method to remove circle object from stack
    private static void deleteStackElement()
    {
        System.out.println("Deleting element from stack");
        System.out.println(circleStack.pop());
    }
    //Method to remove circle object from Queue
    private static void deleteQueueElement()
    {
        System.out.println("Deleting element from Queue");
        System.out.println(circleQueue.remove());
    }
    //Method to display stack element
    private static void displayStackElement()
    {
        System.out.println("Element in stack :" + circleStack.peek());
    }
    //Method to display queue element
    private static void displayQueueElement()
    {
        System.out.println("Element in queue :" + circleQueue.peek());
    }
    //Method to create stack element
    private static void createCircleStack()
    {
        double radius;

        System.out.println("Enter radius of any circle :");
        radius = Double.parseDouble(console.nextLine());

        Circle circle = new Circle();

        circle.setRadius(radius);
        circleStack.push(circle);
    }
    //Method to create circle on queue
    private static void createCircleQueue()
    {
        double radius;

        System.out.println("Enter radius of Circle :");
        radius = Double.parseDouble(console.nextLine()); //parse int

        Circle circle = new Circle();

        circle.setRadius(radius);

        //Adds circle object to queue
        circleQueue.add(circle);

    }




    //UI method that prompts user for the various required selections
    private static int ui()
    {
        System.out.println("1. Add circle to stack");
        System.out.println("2. Add a circle to queue");
        System.out.println("3. Display stack");
        System.out.println("4. Display queue");
        System.out.println("5. Pop circle fom the stack");
        System.out.println("6. Deque circle");

        System.out.println("Enter your selection[1-6] or enter -1 to exit: "); // exit program

        /*
        parseInt to convert strings to int
         */

        return Integer.parseInt(console.nextLine());
    }
}
0 0
Add a comment Improve this question Transcribed image text
Answer #1

import java.util.Comparator;
import java.util.PriorityQueue;
import java.util.Scanner;

class Circle {
        private double radius;

        Circle() {
                radius = 0; // initialize to 0
        }

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

        public double getRadius() {
                return radius;
        }

        void setRadius(double radius) {
                this.radius = radius;
        }

        private double calculateArea() {
                return Math.PI * (radius * radius); // calculation to determine area of the circle
        }

        public String toString() {
                return String.format("Radius : %.2f , Area %.2f\n", radius, calculateArea()); // formatted to two decimal
        }
}

public class CirclePQ {

        // create a scanner class
        private static final Scanner console = new Scanner(System.in);
        private static PriorityQueue<Circle> circleQueue = null;

        // UI method that prompts user for the various required selections
        private static int ui() {
                System.out.println("1. Add circle to queue");
                System.out.println("2. Display queue");
                System.out.println("3. Deque circle");

                System.out.println("Enter your selection[1-3] or enter -1 to exit: "); // exit program

                return Integer.parseInt(console.nextLine());
        }

        // Method to create circle on queue
        private static void createCircleQueue() {
                System.out.println("Enter radius of Circle :");
                double radius = Double.parseDouble(console.nextLine()); // parse int

                Circle circle = new Circle(radius);

                // Adds circle object to queue
                circleQueue.add(circle);
        }

        // Method to display queue element
        private static void displayQueueElement() {
                for(Object o: circleQueue.toArray()) {
                        System.out.println(o);
                }
        }

        // Method to remove circle object from Queue
        private static void deleteQueueElement() {
                System.out.println("Deleting element from Queue");
                System.out.println(circleQueue.remove());
        }

        public static void main(String[] args) {
                
                // Note that, a priority queue is an ordered collection, hence we need to pass
                // a comparator, so that we can organize the elements inside the queue
                circleQueue = new PriorityQueue<>(new Comparator<Circle>() {

                        @Override
                        public int compare(Circle o1, Circle o2) {
                                if (o1.getRadius() > o2.getRadius()) {
                                        return 1;
                                }
                                if (o1.getRadius() < o2.getRadius()) {
                                        return -1;
                                }
                                return 0;
                        }
                });

                int selection;
                // call ui method
                selection = ui();
                while (selection != -1) {
                        switch (selection) {
                        case 1:
                                createCircleQueue();
                                break;
                        case 2:
                                displayQueueElement();
                                break;
                        case 3:
                                deleteQueueElement();
                                break;
                        }
                        System.out.println();
                        selection = ui();
                        System.out.println();
                }
                System.out.println("Quitting");
        }

}
**************************************************

Thanks for your question. We try our best to help you with detailed answers, But in any case, if you need any modification or have a query/issue with respect to above answer, Please ask that in the comment section. We will surely try to address your query ASAP and resolve the issue.

Please consider providing a thumbs up to this question if it helps you. by Doing that, You will help other students, who are facing similar issue.

Add a comment
Know the answer?
Add Answer to:
Create a Java application that allows user to build a Priority Queue of Circle Elements (i.e.,...
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. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Create a class named Circle with fields named radius, diameter, and area. Include a constructor that...

    Create a class named Circle with fields named radius, diameter, and area. Include a constructor that sets the radius to 1 and calculates the other two values. Also include methods named setRadius() and getRadius(). The setRadius() method not only sets the radius, but it also calculates the other two values. (The diameter of a circle is twice the radius, and the area of a circle is pi multiplied by the square of the radius. Use the Math class PI constant...

  • Java:Netbeans-Use the LinkedList class to simulate a queue, and use the add method to simulate the...

    Java:Netbeans-Use the LinkedList class to simulate a queue, and use the add method to simulate the enqueue, and the remove method to simulate the dequeue method for a Queue. Remember to use FIFO. Need help with 0. Add New Microchips pushMicroChip(), popMicroChip() and . To simulate this, you will create a menu option 0, which will generate 100 microchip long objects, and place them into a stack of microchips. Those microchip objects will be generated by using the System.nanotime() method.  ...

  • JAVA --Design a class named StackOfStrings that contains: a. A private array elements to store strings...

    JAVA --Design a class named StackOfStrings that contains: a. A private array elements to store strings in the stack b. A private data field size to store the number of strings in the stack c. A constructor to construct an empty stack with a default capacity of 4 d. A constructor to construct an empty stack with a specified capacity e. A method empty() that returns true if the stack is empty f. A method push(String value) that stores value...

  • Let's fix the displayMenu() method! First, edit the method to do the following: We want to...

    Let's fix the displayMenu() method! First, edit the method to do the following: We want to also allow the user to quit. Include a "Quit" option in the print statements! Have it get the user input from within the method itself and return an int based on the user's choice. (Make sure to also edit the method header and how it is called back in the main() method!) What is the method's return type now?                  ...

  • what are the debuggers in Java? import java.text.NumberFormat; /** * * This class represents a circle...

    what are the debuggers in Java? import java.text.NumberFormat; /** * * This class represents a circle that would be used as part * of a larger geometry application */ public class Circle {    private double radius;    private NumberFormat numberFormat; /** *    Constructor for the Circle. * @param radius for the circle */    public Circle(double r) {        radius = r;    } /** *    This method uses the radius of the circle to compute...

  • Write a java class definition for a circle object. The object should be capable of setting...

    Write a java class definition for a circle object. The object should be capable of setting radius, and computing its area and circumference. Use this to create two Circle objects with radius 10 and 40.5, respectively. Print their areas and circumference. Here is the Java class file (Circle.java). Compile it. public class Circle{ //Instance Variables private double PI = 3.1459; private double radius; //Methods public Circle ( ) { }    //get method (Accessor Methods ) public double getRadius (...

  • Java -Create an interface and implement it In the murach.db package, create an interface named IProductDB....

    Java -Create an interface and implement it In the murach.db package, create an interface named IProductDB. This interface should specify this abstract method: public abstract Product get(String productCode); Modify the ProductDB class so it implements the IProductDB interface. Write the code for the new ‘get’ method. Then remove the getProductByCode method. In the Main class, modify the code so it works with the new ProductDB class. This code should create an instance of the IProductDB interface like this: IProductDB db...

  • Write a Java console application that prompts the user to enter the radius of a circle,...

    Write a Java console application that prompts the user to enter the radius of a circle, then prints its radius, diameter, circumference, and area. Write a JavaFX GUI application to do the same calculation, and draw the circle. The Console Output Enter the radius of the circle: 1.2 The radius is 1.2 The diameter is 2.4 The circumference is 7.5398223686155035 The area is 4.523893421169302 Write and document your program per class coding conventions. Add an instance variable double radius. Generate...

  • Here's the problem that I have to solve: Write a Java program that uses a Stack...

    Here's the problem that I have to solve: Write a Java program that uses a Stack data structure to evaluate postfix expressions. Your program takes a postfix expression as an input,for example:3 42.3+ 5.25* ,from the user and calculates/display the result of the expression. What I'm having trouble is getting it to reading and calculating it as postfix Here's my code: import java.util.Scanner; public class Assignment2 {     public static void main(String[] args)     {         Scanner scan = new...

  • Create a class Circle with one instance variable of type double called radius. Then define an...

    Create a class Circle with one instance variable of type double called radius. Then define an appropriate constructor that takes an initial value for the radius, get and set methods for the radius, and methods getArea and getPerimeter. Create a class RightTriangle with three instance variables of type double called base, height, and hypotenuse. Then define an appropriate constructor that takes initial values for the base and height and calculates the hypotenuse, a single set method which takes new values...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT