(Solved Homework): 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

 

Don't use plagiarized sources. Get Your Custom Essay on
(Solved Homework): Documentation: 1. Explain the purpose of the program as detail as possible. 2. Develop a solution for t…
Get an essay WRITTEN FOR YOU, Plagiarism free, and by an EXPERT!
Order Essay

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

Homework Ocean
Calculate your paper price
Pages (550 words)
Approximate price: -

Why Work with Us

Top Quality and Well-Researched Papers

We always make sure that writers follow all your instructions precisely. You can choose your academic level: high school, college/university or professional, and we will assign a writer who has a respective degree.

Professional and Experienced Academic Writers

We have a team of professional writers with experience in academic and business writing. Many are native speakers and able to perform any task for which you need help.

Free Unlimited Revisions

If you think we missed something, send your order for a free revision. You have 10 days to submit the order for review after you have received the final document. You can do this yourself after logging into your personal account or by contacting our support.

Prompt Delivery and 100% Money-Back-Guarantee

All papers are always delivered on time. In case we need more time to master your paper, we may contact you regarding the deadline extension. In case you cannot provide us with more time, a 100% refund is guaranteed.

Original & Confidential

We use several writing tools checks to ensure that all documents you receive are free from plagiarism. Our editors carefully review all quotations in the text. We also promise maximum confidentiality in all of our services.

24/7 Customer Support

Our support agents are available 24 hours a day 7 days a week and committed to providing you with the best customer experience. Get in touch whenever you need any assistance.

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.

Our Services

No need to work on your paper at night. Sleep tight, we will cover your back. We offer all kinds of writing services.

Essays

Essay Writing Service

No matter what kind of academic paper you need and how urgent you need it, you are welcome to choose your academic level and the type of your paper at an affordable price. We take care of all your paper needs and give a 24/7 customer care support system.

Admissions

Admission Essays & Business Writing Help

An admission essay is an essay or other written statement by a candidate, often a potential student enrolling in a college, university, or graduate school. You can be rest assurred that through our service we will write the best admission essay for you.

Reviews

Editing Support

Our academic writers and editors make the necessary changes to your paper so that it is polished. We also format your document by correctly quoting the sources and creating reference lists in the formats APA, Harvard, MLA, Chicago / Turabian.

Reviews

Revision Support

If you think your paper could be improved, you can request a review. In this case, your paper will be checked by the writer or assigned to an editor. You can use this option as many times as you see fit. This is free because we want you to be completely satisfied with the service offered.

× Contact Live Agents