In: Computer Science
Write a C++ or Java application to create BOTH Stack & Queue data structures.
The application also creates a "DisplayStackElement" and "DisplayQueueElement" routine. The application must be menu driven (with an option to terminate the application) and provide the following features.
Allow insertion of a "Circle" object/structure in the Stack data structures.
The Circle contains a "radius" data member.
The Circle also uses functions/methods "setRadius", "getRadius" and calculateArea (returns a double data type).
Allow insertion of a "Circle" object/structure in the Queue data structures.
Allow display of an element from Stack data structure by Invoking a method/function "DisplayStackElement" (uses the "Pop" method)
Allow display of elements from Queue data structure by Invoking a method/function "DisplayQueueElement" (uses "DeQueue" method).
Allow for deletion of the Stack Allow for deletion of the Queue
/*
* The java class display a menu of choices to select.
* The menu contains choice for creating Circle object
* stack ,create a circle object on queue, display element
* from stack, display an circle object from queue.,
* delete the circle object from stack and delete
* the circle object from queue.
* Then print the results on console.
* */
//StackQueue.java
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;
import java.util.Stack;
public class StackQueue
{
//create a scanner class
private static 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 type
circleStack=new
Stack<Circle>();
//create a Queue of Circle
type
circleQueue=new
LinkedList<Circle>();
int choice=0;
//call menu method
choice=menu();
while(choice!=-1)
{
switch(choice)
{
case 1:
createCircleStack();break;
case 2:
createCircleQueue();break;
case 3:
displayStackElement();break;
case 4:
displayQueueElement();break;
case 5:
deleteStackElement();break;
case 6:
deleteQueueElement();break;
}
choice=menu();
}
System.out.println("Program
exit");
}
//Method to remove circle object from stack
public static void deleteStackElement()
{
System.out.println("Deleting
element from stack");
System.out.println(circleStack.pop());
}
//Method to remove circle object from Queue
public static void deleteQueueElement()
{
System.out.println("Deleting
element from Queue");
System.out.println(circleQueue.remove());
}
//Method to display stack element
public static void displayStackElement()
{
System.out.println("Element in
stack :"+circleStack.peek());
}
//Method to display queue element
public static void displayQueueElement()
{
System.out.println("Element in
stack :"+circleQueue.peek());
}
//Method to create stack element
public static void createCircleStack()
{
double radius;
System.out.println("Enter radius
of Circle :");
radius=Double.parseDouble(console.nextLine());
Circle circle=new Circle();
circle.setRadius(radius);
circleStack.push(circle);
}
//Method to create circle on queue
public static void createCircleQueue()
{
double radius;
System.out.println("Enter radius
of Circle :");
radius=Double.parseDouble(console.nextLine());
Circle circle=new Circle();
circle.setRadius(radius);
//Add circle object to queue
circleQueue.add(circle);
}
//Method that prompts user for menu of choice
public static int menu()
{
System.out.println("1. Create a
circle on stack");
System.out.println("2. Create a
circle on Queue");
System.out.println("3. Display
StackElement");
System.out.println("4. Display
QueueElement");
System.out.println("5. Pop Stack
Element");
System.out.println("6. Deque Queue
Element");
System.out.println("Enter your
choice[1-6] or enter -1 to stop: ");
int
choice=Integer.parseInt(console.nextLine());
return choice;
}
}
-------------------------------------------------------------------------------------------------------
//Circle.java
public class Circle
{
private double radius;
public Circle()
{
radius=0;
}
/**
* return the radius
*/
public double getRadius() {
return radius;
}
/**
* param radius the radius to set
*/
public void setRadius(double radius) {
this.radius = radius;
}
public double calculateArea()
{
return Math.PI*radius*radius;
}
public String toString()
{
return String.format("Radius : %f ,
Area %f\n", radius,calculateArea());
}
}
-------------------------------------------------------------------------------------------------------
Sample Output:
1. Create a circle on stack
2. Create a circle on Queue
3. Display StackElement
4. Display QueueElement
5. Pop Stack Element
6. Deque Queue Element
Enter your choice[1-6] or enter -1 to stop:
1
Enter radius of Circle :
10
1. Create a circle on stack
2. Create a circle on Queue
3. Display StackElement
4. Display QueueElement
5. Pop Stack Element
6. Deque Queue Element
Enter your choice[1-6] or enter -1 to stop:
2
Enter radius of Circle :
15
1. Create a circle on stack
2. Create a circle on Queue
3. Display StackElement
4. Display QueueElement
5. Pop Stack Element
6. Deque Queue Element
Enter your choice[1-6] or enter -1 to stop:
3
Element in stack :Radius : 10.000000 , Area 314.159265
1. Create a circle on stack
2. Create a circle on Queue
3. Display StackElement
4. Display QueueElement
5. Pop Stack Element
6. Deque Queue Element
Enter your choice[1-6] or enter -1 to stop:
4
Element in stack :Radius : 15.000000 , Area 706.858347
1. Create a circle on stack
2. Create a circle on Queue
3. Display StackElement
4. Display QueueElement
5. Pop Stack Element
6. Deque Queue Element
Enter your choice[1-6] or enter -1 to stop:
5
Deleting element from stack
Radius : 10.000000 , Area 314.159265
1. Create a circle on stack
2. Create a circle on Queue
3. Display StackElement
4. Display QueueElement
5. Pop Stack Element
6. Deque Queue Element