Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

you need to use the code below , to add this >>> Write a method that takes a stack of integers intStack and an integer

you need to use the code below , to add this >>> Write a method that takes a stack of integers intStack and an integer value num. The method returns true if num is in the stack and false otherwise. The method must also return the elements of intStack back to intStack in their original order. You may only use a queue and some primitive variables. Your method should not use another stack or an array. Test your method.

-------------------------------------------------------------------------------------------------------------------------

import java.util.*; public class testReverseQueue{ static Scanner console = new Scanner(System.in); public static void main(String[] args) { ArrayQueue myQueue = new ArrayQueue(100); int num; while(true){ System.out.print("Enter an integer value (999 to stop): "); num = console.nextInt(); if(num==999) break; myQueue.enqueue(num); } System.out.println("Content of myQueue befor reversing: "); System.out.println(myQueue.toString()); reverseQueue(myQueue); System.out.println("queue content after reversing:"); System.out.println(myQueue.toString()); } // End of main public static void reverseQueue(ArrayQueue Q){ int q_size; q_size=Q.size(); ArrayStack tempStack = new ArrayStack(q_size); for(int i=0; i= 0; j--) { str=str+" "+ data[j]; } return str; } }// end of ArrayStack class class ArrayQueue { // instance variables /** Default array capacity. */ public static final int CAPACITY = 1000; private int[] data; private int front = 0; // index of front private int qSize = 0; // queue size // constructors public ArrayQueue() { data = new int[CAPACITY]; } public ArrayQueue(int capacity) { data = new int[capacity]; } public int size() { return qSize; } public boolean isEmpty() { return (qSize == 0); } public void enqueue(int e) { int avail = (front + qSize) % data.length; data[avail] = e; qSize++; } public int first() { return data[front]; } public int dequeue() { int answer = data[front]; front = (front + 1) % data.length; qSize--; return answer; } public String toString() { StringBuilder sb = new StringBuilder("("); int k = front; for (int j=0; j < qSize; j++) { if (j > 0) sb.append(", "); sb.append(data[k]); k = (k + 1) % data.length; } sb.append(")"); return sb.toString(); } }// End of ArrayQueue class public class Student{ private String name; private int grade; private double cgpa; public Student (){ name=""; grade=0; cgpa=0.0; } public void setStudent(String n, int g, double c){ name = n; grade = g; cgpa = c; } public String getName(){ return name; } public int getGrade(){ return grade; } public double getCGPA(){ return cgpa; } }// End of class Student

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Systems Introduction To Databases And Data Warehouses

Authors: Nenad Jukic, Susan Vrbsky, Svetlozar Nestorov

1st Edition

1943153191, 978-1943153190

More Books

Students also viewed these Databases questions

Question

what is a peer Group? Importance?

Answered: 1 week ago

Question

5. What are the other economic side effects of accidents?

Answered: 1 week ago