Computers and Technology

Complete the implementation of the ArrayStack class. Specifically, complete the implementations of the isEmpty, size, and toString methods. See Base_A05Q1.java for a starting place and a description of these methods./** * Write a description of the program here. * * @author Lewis et al., (your name) * @version (program version) */import java. util. Arrays;public class Base_A05Q1{ /** * Program entry point for stack testing. * @param args Argument list. */ public static void main(String[] args) { ArrayStack stack = new ArrayStack(); System. out. println("STACK TESTING"); stack. push(3); stack. push(7); stack. push(4); System. out. println(stack. peek()); stack. pop(); stack. push(9); stack. push(8); System. out. println(stack. peek()); System. out. println(stack. pop()); System. out. println(stack. peek()); System. out. println("The size of the stack is: " + stack. size()); System. out. println("The stack contains:\n" + stack. toString()); } /** * An array implementation of a stack in which the bottom of the * stack is fixed at index 0. * * @author Java Foundations * @version 4.0 */ public static class ArrayStack implements StackADT { private final static int DEFAULT_CAPACITY = 100; private int top; private T[] stack; /** * Creates an empty stack using the default capacity. */ public ArrayStack() { this(DEFAULT_CAPACITY); } /** * Creates an empty stack using the specified capacity. * @param initialCapacity the initial size of the array */ @SuppressWarnings("unchecked") //see p505. public ArrayStack(int initialCapacity) { top = 0; stack = (T[])(new Object[initialCapacity]); } /** * Adds the specified element to the top of this stack, expanding * the capacity of the array if necessary. * @param element generic element to be pushed onto stack */ public void push(T element) { if (size() == stack. length) expandCapacity(); stack[top] = element; top++; } /** * Creates a new array to store the contents of this stack with * twice the capacity of the old one. */ private void expandCapacity() { stack = Arrays. copyOf(stack, stack. length * 2); } /** * Removes the element at the top of this stack and returns a * reference to it. * @return element removed from top of stack * @throws EmptyCollectionException if stack is empty */ public T pop() throws EmptyCollectionException { if (isEmpty()) throw new EmptyCollectionException("stack"); top--; T result = stack[top]; stack[top] = null; return result; } /** * Returns a reference to the element at the top of this stack. * The element is not removed from the stack. * @return element on top of stack * @throws 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. * @return true if this stack is empty */ public boolean isEmpty() { //TODO: Implement me. return false; } /** * Returns the number of elements in this stack. * @return the number of elements in the stack */ public int size() { //TODO: Implement me. return 0; } /** * Returns a string representation of this stack. The string has the * form of each element printed on its own line, with the top most * element displayed first, and the bottom most element displayed last. * If the list is empty, returns the word "empty". * @return a string representation of the stack */ public String toString() { //TODO: Implement me. return ""; } }}

answer
Answers: 3

Other questions on the subject: Computers and Technology

image
Computers and Technology, 22.06.2019 19:20, mayaparness
Write a program that prompts the user to input a string. the program then uses the function substr to remove all the vowels from the string. for example, if str = "there", then after removing all the vowels, str = "thr". after removing all the vowels, output the string. your program must contain a function to remove all the vowels and a function to determine whether a character is a vowel.
Answers: 2
image
Computers and Technology, 23.06.2019 22:00, rocksquad9125
Take a critical look at three gui applications you have used—for example, a spreadsheet, a word-processing program, and a game. describe how well each conforms to the gui design guidelines listed in this chapter.
Answers: 3
image
Computers and Technology, 24.06.2019 17:40, xinkyx616
Pseudocode pld #6, pg. 117 start// declarations// number numbertoguess// number myguess; numbertoguess = 92// while myguess ! = numbertoguess// output " guess an integer number between 1 and 100"// input myguess// if (myguess == numbertoguess)// output "you guessed the correct number"// else// output "the number you guessed was incorrect. try again! "// end if// end while// output " for playing the guessing game. have a great day! "// stop
Answers: 3
image
Computers and Technology, 24.06.2019 20:30, LaughingAlanna
Does the query hawaiian photographers fully meets results?
Answers: 1
Do you know the correct answer?
Complete the implementation of the ArrayStack class. Specifically, complete the implementations of t...

Questions in other subjects:

Konu
English, 08.01.2022 14:00