1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
Tema [17]
4 years ago
11

Write a function that implements another stack function, peek. Peek returns the value of the first element on the stack without

removing the element from the stack. Peek should also do underflow error checking. (Why is overflow error checking unnecessary
Computers and Technology
1 answer:
devlian [24]4 years ago
3 0

Answer:

See explaination

Explanation:

StackExample.java

public class StackExample<T> {

private final static int DEFAULT_CAPACITY = 100;

private int top;

private T[] stack = (T[])(new Object[DEFAULT_CAPACITY]);

/**

* Returns a reference to the element at the top of this stack.

* The element is not removed from the stack.

* atreturn element on top of stack

* atthrows EmptyCollectionException if stack is empty

*/

public T peek() throws EmptyCollectionException

{

if (isEmpty())

throw new EmptyCollectionException("stack");

return stack[top-1];

}

/**

* Returns true if this stack is empty and false otherwise.

* atreturn true if this stack is empty

*/

public boolean isEmpty()

{

return top < 0;

}

}

//please replace "at" with the at symbol

Note:

peek() method will always pick the first element from stack. While calling peek() method when stack is empty then it will throw stack underflow error. Since peek() method will always look for first element ffrom stack there is no chance for overflow of stack. So overflow error checking is not required. In above program we handled underflow error in peek() method by checking whether stack is an empty or not.

You might be interested in
How has the internet improved business communication worldwide
emmasim [6.3K]

Answer:

One of the biggest changes for businesses is the communication technology enabled by the internet. The way people communicate with one another, share files, make purchases, and collaborate has completely shifted since the internet became widespread.

Explanation:

4 0
2 years ago
Yall rachits and yall eat butt xD
Trava [24]
Please use this for questions.
6 0
3 years ago
Read 2 more answers
Is a procedure to copy one or more files from backup media to the original disk or a replacement disk when data or programs have
Stels [109]
Restore is a procedure to copy one or more files from backup media to the original disk or a replacement disk when data or programs have been erased or destroyed.
4 0
4 years ago
Picture related to cyber safety
Fantom [35]

Answer:

?????

Explanation:

4 0
3 years ago
Read 2 more answers
Create and apply a CSS class named YellowBackground that selects a yellow background. Apply the class to your HTML file’s body t
masha68 [24]

Here is an HTML example with the CSS class defined inline:

<!doctype html>

<html>

 <head>

   <style>

  .YellowBackground {

 background-color : yellow;

  }

</style>

   

   </head>

 

   <body class="YellowBackground">

       <h1>A yellow background</h1>

   </body>

</html>

6 0
3 years ago
Other questions:
  • What is internet protocol? Language computers use to communicate over the internet Small files that websites put on your PC to s
    8·2 answers
  • Jorge has $300 for work he performed. He expects to spend the money in the next few weeks to buy a new bike. Which type of accou
    8·1 answer
  • If you know the unit prices of two different brands of an item you are better able to
    8·1 answer
  • Help me please (program c++)
    11·1 answer
  • What three steps Name a group??
    14·1 answer
  • An is auditor reviewing a network log discovers that an employee ran elevated commands on his/her pc by invoking the task schedu
    10·1 answer
  • One page of content in a PowerPoint presentation is referred to as a
    12·1 answer
  • Help me I'm so confused by this question
    7·1 answer
  • Select the correct answer.
    12·2 answers
  • What statement best describes entrepreneurship?
    7·2 answers
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!