Answered Essay: Documentation: 1. Explain the purpose of the program as detail as possible. 2. Develop a solution for t

Documentation: 20% 1 . Explain the purpose of the program as detail as possible 3%. 2, Develop a solution for the problem and mention algorithm used-12% 3. List data structures to be used in the solution-2%. 4. Give a description of how to use the program and expected input/output-3% Programming: 80% For each method, give the pre and post conditions and invariants, if any-5% 2. Program execution according to the requirements 50% 3. Naming of program as required 5% 4. Useful comments and readability: 20% Description of program: Write a program to implement a queue using stack. The file name should be MyQueue.java. ·

Documentation: 1. Explain the purpose of the program as detail as possible. 2. Develop a solution for the problem and mention algorithm used 3. List data structures to be used in the solution. 4. Give a description of how to use the program and expected input/output Programming: For each method, give the pre and post conditions and invariants, if any 2. Program execution according to the requirements 3. Naming of program as required 4. Useful comments and readability: Description of program: Write a program to implement a queue using stack. The file name should be MyQueue.java.

Expert Answer

 

Documentation

1. Purpose

Implement the queue Data sructure using stack data sructure

which supports the basics operation like queuee , dequeue cheking the

size of queue , finding the first element od queue without removing.

2. Solution and algorithm

Algortihm are very identical to stack supports like pushing and popping

for solution please refer the code, used two stacks for achieving the goal

both stack are internal class in java library

3 . Data structure

1. Qeueu

2. Stack

4. How to run program

Program is menu driven where you will get prompted to input or can say you need to

select the option showing on the screen . Simply Run the MyQueue class in which main methos is implemented.

Programming:

All requirement in MyQueue class itself

Please find below the required code

save the below code as MyQueue.java

import java.util.NoSuchElementException;

import java.util.Scanner;

import java.util.Stack;

/**

* This is generic Queue implementation using two stacks

* that can be used with any data type

* it supports queue operations like

* enqueue dequeue

* and have some utility methods

* isEmpty, peek

* @author YourName

*

* @param <T>

*/

public class MyQueue<T> {

//two stack used for

//one for pushing and one for pop the element

private Stack<T> s1;

private Stack<T> s2;

  

//constructor MyQueue that initialize both stacks

public MyQueue() {

s1 = new Stack<T>();

s2 = new Stack<T>();

}

/**

* this method is used for checking the queue

* if no element return true

* @return if no element return true else false

*/

public boolean isEmpty() {

return s1.isEmpty() && s2.isEmpty();

}

/**

* used this method to check size of queue

* @return summed of both stacks sizes s

*/

public int size() {

return s1.size() + s2.size();   

}

/**

* this method for demonstating the

* first element in queue

* @return first element in queue

*/

public T peek() {

if (isEmpty()) throw new NoSuchElementException(“Queue underflow”);

if (s2.isEmpty()) moveTemp();

return s2.peek();

}

/**

* this method is used to move element from

* s1 to s2 for dequeue

*/

private void moveTemp() {

while (!s1.isEmpty())

s2.push(s1.pop());

}

/**

* this method for used to enqueue element

* in the queue

* @param item

*/

public void enqueue(T item) {

s1.push(item);

}

/**

* this method is used to remove element from queue

* @return dequeued element or first element in queue

*/

public T dequeue() {

if (isEmpty()) throw new NoSuchElementException(“Queue underflow”);

if (s2.isEmpty()) moveTemp();

return s2.pop();

}

/**

* main method for demonstrating the MyQueue

* class

* @param args

*/

public static void main(String[] args) {

//created queue for string data type

MyQueue<String> q = new MyQueue<String>();

int userChoice;

Scanner sc = new Scanner(System.in);

//menu driven program for MyQueue

while(true)

{

System.out.println(“1.Enqueue the elements”);

System.out.println(“2.Dequeue the elements”);

System.out.println(“3.Check the MyQueue Size”);

System.out.println(“4.Check if MyQueue is empty”);

System.out.println(“5.See the first element in MyQueue”);

System.out.println(“6.Exit”);

System.out.print(“Enter the choice : “);

userChoice=sc.nextInt();

switch(userChoice){

case 1:System.out.print(“Enter the element to enqueue : “);

String temp = sc.next();

q.enqueue(temp);

break;

case 2:System.out.println(“Dequed Element : “+q.dequeue());

break;

case 3:System.out.println(“Size of MyQueue : “+q.size());

break;

case 4:System.out.println(“IS MyQueue Empty ? :”+q.isEmpty());

break;

case 5:System.out.println(“First element in queue : “+q.peek());

break;

case 6:System.exit(0);

default : System.out.println(“Invalid entry , please try again !”);break;

}

}

}

}//end of class

//OUTPUT

p

Buy Essay
Calculate your paper price
Pages (550 words)
Approximate price: -

Help Me Write My Essay - Reasons:

Best Online Essay Writing Service

We strive to give our customers the best online essay writing experience. We Make sure essays are submitted on time and all the instructions are followed.

Our Writers are Experienced and Professional

Our essay writing service is founded on professional writers who are on stand by to help you any time.

Free Revision Fo all Essays

Sometimes you may require our writers to add on a point to make your essay as customised as possible, we will give you unlimited times to do this. And we will do it for free.

Timely Essay(s)

We understand the frustrations that comes with late essays and our writers are extra careful to not violate this term. Our support team is always engauging our writers to help you have your essay ahead of time.

Customised Essays &100% Confidential

Our Online writing Service has zero torelance for plagiarised papers. We have plagiarism checking tool that generate plagiarism reports just to make sure you are satisfied.

24/7 Customer Support

Our agents are ready to help you around the clock. Please feel free to reach out and enquire about anything.

Try it now!

Calculate the price of your order

Total price:
$0.00

How it works?

Follow these simple steps to get your paper done

Place your order

Fill in the order form and provide all details of your assignment.

Proceed with the payment

Choose the payment system that suits you most.

Receive the final file

Once your paper is ready, we will email it to you.

HOW OUR ONLINE ESSAY WRITING SERVICE WORKS

Let us write that nagging essay.

STEP 1

Submit Your Essay/Homework Instructions

By clicking on the "PLACE ORDER" button, tell us your requires. Be precise for an accurate customised essay. You may also upload any reading materials where applicable.

STEP 2

Pick A & Writer

Our ordering form will provide you with a list of writers and their feedbacks. At step 2, its time select a writer. Our online agents are on stand by to help you just in case.

STEP 3

Editing (OUR PART)

At this stage, our editor will go through your essay and make sure your writer did meet all the instructions.

STEP 4

Receive your Paper

After Editing, your paper will be sent to you via email.