Question

In: Computer Science

Comeputer science .This is java problem. PROBLEM 1 Queue (Links to an external site.) is an...

Comeputer science .This is java problem.

PROBLEM 1

Queue (Links to an external site.) is an abstract data type (ADT) consisting of a sequence of entities with a first-in-first-out (FIFO) (Links to an external site.) property. Queue has the following operations, in alignment with the Java Queue interface (Links to an external site.) in the Oracle's SDK:

  • add(x): inserts the specified x element to the back of queue without violating capacity limitation.
  • remove(): removes the head (front) of queue, returning the removed element.
  • peek(): retrieves and displays but does not remove (i.e., read-only) the head of queue.
  • isEmpty: returns whether the queue is empty or not as boolean.

Implement the above FOUR (4) operations in your Solution class as follows. You are responsible for implementing the area shown in red below. Note that you are given TWO (2) local Stacks to help you manage the above listed queue operations.

STARTER CODE

import java.util.Stack;
public class HomeworkAssignment5_1 {    
    public static void main(String[] args) {
       // just like any problems, whatever you need here, etc.
    }
}
class Solution {

   // YOUR STACK TO USE FOR THIS PROBLEM
   private Stack<Integer> pushStack = new Stack<Integer>();
   private Stack<Integer> popStack = new Stack<Integer>();

   /* =====================================
   /* !!! DO NOT MODIFY ABOVE THIS LINE!!!
   /* ====================================

   // YOUR STYLING DOCUMENTATION HERE
   public void add(int x) { 
      // YOUR CODE HERE
   }
   // YOUR STYLING DOCUMENTATION HERE
   public int remove() { 
      // YOUR CODE HERE
   }
   // YOUR STYLING DOCUMENTATION HERE
   public int peek() { 
      // YOUR CODE HERE
   }
   // YOUR STYLING DOCUMENTATION HERE
   public boolean isEmpty() { 
     // YOUR CODE HERE
   }
}

EXAMPLES

// TEST CASE #1
Solution sol = new Solution();
sol.add(8);
sol.add(1);  
sol.peek();     // 8 (if you use System.out.println(), for example)
sol.remove();   // 8
sol.isEmpty();  // false
sol.remove();   // 1
sol.isEmpty();  // true
sol.add(2);     
sol.add(3); 
sol.peek();     // 2

// TEST CASE #2
// etc.

CONSTRAINTS / ASSUMPTIONS

  • You must use Java Stack (Links to an external site.) (java.utils.Stack), both local pushStack and popStack instances, to implement the solution queue for this problem; failure to do so receives 0 logic points.
  • You must use only the standard Stack methods, pop(), push(), peek(), and empty(), for this problem.
  • This problem tests your understanding of how queue works in Java by implementing it from scratch using the Stack ADT you learned.
  • All operations called on the queue are valid. In other words, both remove() and peek() will NOT be called on an empty queue. This means you won't have to create any Exceptions to handle errors.
  • Your solution will be tested against 9-10 test cases; -1 for each failed test.

Solutions

Expert Solution

import java.util.*;
import java.lang.*;
import java.io.*;

class Solution {

// YOUR STACK TO USE FOR THIS PROBLEM
private Stack<Integer> pushStack = new Stack<Integer>();
private Stack<Integer> popStack = new Stack<Integer>();

/* =====================================
/* !!! DO NOT MODIFY ABOVE THIS LINE!!!
/* ==================================== */

// This is going to add an elemnt at top of the stack
public void add(int x) {
while (!pushStack.isEmpty())
{
popStack.push(pushStack.pop());
}
  
pushStack.push(x);
  
while (!popStack.isEmpty())
{
pushStack.push(popStack.pop());
}
}
// This is going to remove an element from the top of the stack
public int remove() {

// if first stack is empty
if (pushStack.isEmpty())
{
System.out.println("Q is Empty");
return -1;
}
  
// Return top of pushStack
int x = pushStack.peek();
pushStack.pop();
return x;
}
// This shows the top most element from the stack wothout popping it
public int peek() {
Integer peek_element = (Integer) pushStack.lastElement();
return peek_element;
}
// isEmpty is not directly a part of the Stack but rather implementation from
// Vector. Check if the element exist inside the Stack/Vector or not.
public boolean isEmpty() {
return pushStack.isEmpty();
}
}


public class HomeworkAssignment5_1
{
   public static void main (String[] args) throws java.lang.Exception
   {
       // your code goes here
       Solution sol = new Solution();
sol.add(8);
sol.add(1);
System.out.println(sol.peek());
System.out.println(sol.remove());   
System.out.println(sol.isEmpty());
System.out.println(sol.remove());
System.out.println(sol.isEmpty());
sol.add(2);   
sol.add(3);
System.out.println(sol.peek());
   }
}


Related Solutions

1. go to www.sec.gov (Links to an external site.)Links to an external site. 2. Hover your...
1. go to www.sec.gov (Links to an external site.)Links to an external site. 2. Hover your cursor over "Filings" in the menu bar across the top 3. click on "company filings search" that pops up when you hover 4. in the FAST SEARCH box on the far right type in "AEO" for the ticker and click "search" for American Eagle. For Buckle type in "BKE". 5. in the "filings" column on the far left look for the most recent "10-K"...
Go to the Bureau of Economic Analysis (Links to an external site.)Links to an external site....
Go to the Bureau of Economic Analysis (Links to an external site.)Links to an external site. (BEA) website and look at quarterly data from the last few years of the National Accounts. Can you make a decision of what part of the business cycle the U.S. economy is currently in? Why? What factors lead you to this conclusion? You may want to do additional research of sources to reach a conclusion. If so, please identify the sources that added to...
Visit the Bureau of Labor Statistics (Links to an external site.)Links to an external site. website...
Visit the Bureau of Labor Statistics (Links to an external site.)Links to an external site. website and explore the Consumer Price Index section. Read the "Current CPI Economic News Releases" and summarize the latest release. Did the CPI increase or decrease? What caused the index to increase or decrease? Explain. Visit the inflation calculator (Links to an external site.)Links to an external site. section and easily find out how the buying power of the dollar has changed over the years....
Watch the Shifts in Aggregate Demand video (Links to an external site.)Links to an external site....
Watch the Shifts in Aggregate Demand video (Links to an external site.)Links to an external site. (embedded in the reading) and summarize the factors that cause the AD curve to shift. Did the video help you understand the model? Pick one of the four scenarios listed below the video and explain in your own words which way the AD curve will shift. At least 200 words
INSTRUCTIONS Watch the Starbucks (Links to an external site.)Links to an external site. video and answer...
INSTRUCTIONS Watch the Starbucks (Links to an external site.)Links to an external site. video and answer the question below. How has leadership impacted Starbucks' current growth and how will their leaders need to evolve in order to maintain the company's success given the threats to the specialty coffee industry? in 300 word
Again, please use www.federalreserve.gov (Links to an external site.)Links to an external site. and find what...
Again, please use www.federalreserve.gov (Links to an external site.)Links to an external site. and find what the current Fed policy is for interest rates. Do you think this policy is appropriate for the economy? Why or why not?
Centers for Disease Control and Prevention. (2017). Stroke(Links to an external site.)Links to an external site....
Centers for Disease Control and Prevention. (2017). Stroke(Links to an external site.)Links to an external site. (Links to an external site.)Links to an external site.. Retrieved from http://www.cdc.gov/stroke/index.htm Initial Discussion Post: Compose a response to the following questions: As an RN in an emergency room what signs and symptoms would you associate with someone having a stroke? Would these symptoms be different in different cultures and genders? Identify one culture or gender and discuss their risk factors and presentation of...
Edmunson Electrical Distribution (www.edmunson-electrical.co.uk (Links to an external site.)Links to an external site.) is a leading...
Edmunson Electrical Distribution (www.edmunson-electrical.co.uk (Links to an external site.)Links to an external site.) is a leading distributor of electrical equipment and components with over 230 branches in the United Kingdom. The company is a wholesaler of electrical products acting as an intermediary between manufacturers and customers. Accounts are classified according to turnover and margins achieved. The ‘bread and butter’ of the business is the electrical contractor, who provides high turnover but low margins. The more significant the purchases, the higher...
Read an article on the purpose of GAAP (Links to an external site.)Links to an external...
Read an article on the purpose of GAAP (Links to an external site.)Links to an external site. . Write a 1-2 page paper that addresses the following questions: How does GAAP standardize accounting records across companies? Why are private businesses not required to follow GAAP? Which issues may have occurred before rules for accounting documentation were standardized? Who maintains GAAP rules? Why is this separate from the responsibilities of government? What is the difference between preparing reports without GAAP? What...
AMC Entertainment Holdings, Inc. (NYSE: AMC (Links to an external site.)Links to an external site.), also...
AMC Entertainment Holdings, Inc. (NYSE: AMC (Links to an external site.)Links to an external site.), also known as AMC Theatres, recently announced a $19.95 per month subscription service, AMC Stubs A-List (Links to an external site.)Links to an external site., to compete with MoviePass. With the A-List program, subscribers can see up to three movies per week with few restrictions. The minimum commitment is three months, after which time the subscriber can cancel at any time. Just as a point...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT