Question

In: Computer Science

Write a C++ or Java application to create BOTH Stack & Queue data structures. The application...

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

Solutions

Expert Solution


/*
* 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


Related Solutions

In this lab, using C++, you will create two data structures: a stack and a queue....
In this lab, using C++, you will create two data structures: a stack and a queue. You will use STL containers to demonstrate basic ADTs. Queue For the queue, you will simulate a buffer. Remember it is first-in-first-out. The user will enter a number for the number of rounds to run your simulation. You need one function that randomly generates a number. You will also have a user specified percentage, and the function uses this percentage to randomly put the...
C++ Data Structures: Implement a Stack and a Queue using Linked list In this lab you...
C++ Data Structures: Implement a Stack and a Queue using Linked list In this lab you will implement the functionality of a stack and a queue using a linked list. Your program must use of the declaration of the Stack and Queue class in Stack.h and Queue.h You have to implement the functionalities of queue (enq, deq, displayQueue) in a file called Queue.cpp. All the functions in Queue.cpp should follow the prototypes declared in Queue.h. Your code should make use...
In Java or C++, implement a stack and a queue using a linkedlist data structure.  You may...
In Java or C++, implement a stack and a queue using a linkedlist data structure.  You may not use any standard Java or C++ libraries. Assume your data structure only allows Strings. Implement the following operations for the data structure: Queue: enqueue, dequeue, create, isEmpty (10 points) Stack: push, pop, create, isEmpty (10 points) Here is a link to get started on transferring from Java to C++ http://www.horstmann.com/ccj2/ccjapp3.html (Links to an external site.) Upload a zip file with one implementation for...
Requirements Both the stack and queue were implemented correctly according data structure and java standard practices....
Requirements Both the stack and queue were implemented correctly according data structure and java standard practices. The stack was used to implement a post-fix calculator using push, pop, peek and other stack concepts. The queue was used to implement the palindrome using add, remove, insert and other queue concepts. Both implementations produced the expected output. All submission requirements were followed. Test Harness for the first part: public class StackCalcTest { public static void main(String[] args) { StackCalc stackCalc = new...
Exercise 3: Stack Write a program in Java to manipulate a Stack List: 1. Create Stack...
Exercise 3: Stack Write a program in Java to manipulate a Stack List: 1. Create Stack List 2. Display the list 3. Create the function isEmply 4. Count the number of nodes 5. Insert a new node in the Stack List. 6. Delete the node in the Stack List. 7. Call all methods above in main method with the following data: Test Data : Input the number of nodes : 4 Input data for node 1 : 5 Input data...
Suppose the interface and the class of stack already implemented, Write application program to ( java)...
Suppose the interface and the class of stack already implemented, Write application program to ( java) 1- insert 100 numbers to the stack                         2- Print the even numbers 3- Print the summation of the odd numbers
In C++ In this lab we will be creating a stack class and a queue class,...
In C++ In this lab we will be creating a stack class and a queue class, both with a hybrid method combining linked list and arrays in addition to the Stack methods(push, pop, peek, isEmpty, size, print) and Queue methods (enqueue, deque, peek, isEmpty, size, print). DO NOT USE ANY LIBRARY, implement each method from scratch. Both the Stack and Queue classes should be generic classes. Don't forget to comment your code.
In java thanks! Design a Stack that is composed ONLY of one or two Queue objects...
In java thanks! Design a Stack that is composed ONLY of one or two Queue objects ergo the ONLY instance variables that exist in this stack are queues. Stack class should contain the following methods: Print Pop Push Top Size isEmpty copy [(2)] Design a Queue that is composed ONLY of two Stacks objects ergo the ONLY instance variables that exist in this queue are stacks. Queue class should contain the following methods:   Print Enqueue Dequeue Front Rear Size isEmpty...
***IN C# ONLY, USING WINDOWS FORMS*** --NO JAVA--. Create a GUI application in C# that calculates...
***IN C# ONLY, USING WINDOWS FORMS*** --NO JAVA--. Create a GUI application in C# that calculates and displays the total travel expenses of a business person on a trip. Here is the information that the user must provide: • Number of days on the trip • Amount of airfare, if any • Amount of car rental fees, if any • Number of miles driven, if a private vehicle was used • Amount of parking fees, if any • Amount of...
C language Write a program in C to implement Queue and its operation (like create, insert,...
C language Write a program in C to implement Queue and its operation (like create, insert, delete, search) using array data structure.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT