Question

how would I complete this code without calling any built-in java collection framework classes like ArrayList,...

how would I complete this code without calling any built-in java collection framework classes like ArrayList, LinkedList, etc?

import java.util.Iterator;

class CallStack<T> implements Iterable<T> {

// You'll want some instance variables here

public CallStack() {

//setup what you need

}

public void push(T item) {

//push an item onto the stack

//you may assume the item is not null

//O(1)

}

public T pop() {

//pop an item off the stack

//if there are no items on the stack, return null

//O(1)

//replace this dummy return statement

return null;

}

public T peek() {

//return the top of the stack (but don't remove it)

//if there are no items on the stack, return null

//O(1)

//replace this dummy return statement

return null;

}

public String toString() {

//Create a string of the stack where each item

//is separated by a space. The top of the stack

//should be shown to the right and the bottom of

//the stack on the left.

//Hint: Reuse the provided code from another class

//instead of writing this yourself!

//O(n)

//replace this dummy return statement

return null;

}

public void clear() {

//remove everything from the stack

//O(1)

}

public int size() {

//return the number of items on the stack

//O(1)

//replace this dummy return statement

return -1;

}

public boolean isEmpty() {

//return whether or not the stack is empty

//O(1)

//replace this dummy return statement

return false;

}

@SuppressWarnings("unchecked")

public Object[] toArray() {

//Return an array representation of the stack.

//The top of the stack should be element 0

//in the array.

//O(n)

//replace this dummy return statement

return null;

}

public Iterator<T> iterator() {

//Return an iterator that traverses from

//the top of the stack to the bottom of

//the stack.

//The iterator's hasNext() and next() methods

//must both be O(1)

//next() should throw a NullPointerException

//if you try to use next when there are no

//more items

//replace this dummy return statement

return null;

}

public static void main(String[] args) {

CallStack<String> s1 = new CallStack<>();

s1.push("a");

s1.push("b");

CallStack<Integer> s2 = new CallStack<>();

s2.push(1);

s2.push(2);

s2.push(3);

if(s1.toString().equals("a b") && s1.toArray()[0].equals("b") && s1.toArray()[1].equals("a") && s1.toArray().length == 2) {

System.out.println("Yay 1");

}

if(s1.peek().equals("b") && s2.peek().equals(3) && s1.size() == 2 && s2.size() == 3) {

System.out.println("Yay 2");

}

if(s1.pop().equals("b") && s2.pop().equals(3) && s1.size() == 1 && s2.size() == 2) {

System.out.println("Yay 3");

}

if(s1.toString().equals("a") && s1.peek().equals("a") && s2.peek().equals(2) && s1.pop().equals("a") && s2.pop().equals(2) && s1.size() == 0 && s2.size() == 1) {

System.out.println("Yay 4");

}

if(s1.toString().equals("") && s1.peek() == null && s2.peek().equals(1) && s1.pop() == null && s2.pop().equals(1) && s1.size() == 0 && s2.size() == 0) {

System.out.println("Yay 5");

}

s2.push(10);

s2.push(20);

s2.push(30);

if(s1.isEmpty() && s1.toArray().length == 0 && !s2.isEmpty()) {

s2.clear();

if(s2.isEmpty()) {

System.out.println("Yay 6");

}

}

CallStack<Integer> s3 = new CallStack<>();

s3.push(3);

s3.push(2);

s3.push(1);

int i = 1;

for(Integer item : s3) {

if(i == item) System.out.println("Yay " + (6+i));

else

System.out.println(item);

i++;

}

}

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1


   Given below is the completed code for the question. Please do rate the answer if it helped. Thank you.

   import java.util.Iterator;

   class CallStack<T> implements Iterable<T> {

       private T[] data;
       private int size;

       public CallStack() {
           data = (T[])new Object[10];
           size = 0;
       }

       public void push(T item) {

           //push an item onto the stack

           //you may assume the item is not null

           data[size] = item;
           size++;

       }

       public T pop() {

           //pop an item off the stack

           //if there are no items on the stack, return null

           //O(1)

           if(isEmpty())
               return null;
           else
               return data[--size];

       }

       public T peek() {

           //return the top of the stack (but don't remove it)

           //if there are no items on the stack, return null

           //O(1)

           if(isEmpty())
               return null;
           else
               return data[size-1];

       }

       public String toString() {

           //Create a string of the stack where each item

           //is separated by a space. The top of the stack

           //should be shown to the right and the bottom of

           //the stack on the left.

           //Hint: Reuse the provided code from another class

           //instead of writing this yourself!

           //O(n)

           //replace this dummy return statement
           String s = "";
           if(size > 0){
               s += data[0];
               for(int i = 1; i < size; i++)
                   s += " " + data[i];
           }
           return s;

       }

       public void clear() {

           //remove everything from the stack

           //O(1)
           size = 0;

       }

       public int size() {

           //return the number of items on the stack

           //O(1)

           return size;

       }

       public boolean isEmpty() {

           //return whether or not the stack is empty

           //O(1)

           return size == 0;
       }

       @SuppressWarnings("unchecked")

       public Object[] toArray() {

           //Return an array representation of the stack.

           //The top of the stack should be element 0

           //in the array.

           //O(n)

           Object[] arr = new Object[size];
           for(int i = size-1, j = 0; i >= 0; i--, j++)
               arr[j] = data[i];
           return arr;

       }

       public Iterator<T> iterator() {

           //Return an iterator that traverses from

           //the top of the stack to the bottom of

           //the stack.

           //The iterator's hasNext() and next() methods

           //must both be O(1)

           //next() should throw a NullPointerException

           //if you try to use next when there are no

           //more items

           return new CallStackIterator();

       }
      
       class CallStackIterator implements Iterator<T>{
           private int index;
           private CallStackIterator() {
               index = size - 1;
           }
           @Override
           public boolean hasNext() {
               return (index >= 0);
           }

           @Override
           public T next() {
               return data[index--];
           }
          
       }

       public static void main(String[] args) {

           CallStack<String> s1 = new CallStack<>();

           s1.push("a");

           s1.push("b");

           CallStack<Integer> s2 = new CallStack<>();

           s2.push(1);

           s2.push(2);

           s2.push(3);

           if(s1.toString().equals("a b") && s1.toArray()[0].equals("b") && s1.toArray()[1].equals("a") && s1.toArray().length == 2) {

               System.out.println("Yay 1");

           }

           if(s1.peek().equals("b") && s2.peek().equals(3) && s1.size() == 2 && s2.size() == 3) {

               System.out.println("Yay 2");

           }

           if(s1.pop().equals("b") && s2.pop().equals(3) && s1.size() == 1 && s2.size() == 2) {

               System.out.println("Yay 3");

           }

           if(s1.toString().equals("a") && s1.peek().equals("a") && s2.peek().equals(2) && s1.pop().equals("a") && s2.pop().equals(2) && s1.size() == 0 && s2.size() == 1) {

               System.out.println("Yay 4");

           }

           if(s1.toString().equals("") && s1.peek() == null && s2.peek().equals(1) && s1.pop() == null && s2.pop().equals(1) && s1.size() == 0 && s2.size() == 0) {

               System.out.println("Yay 5");

           }

           s2.push(10);

           s2.push(20);

           s2.push(30);

           if(s1.isEmpty() && s1.toArray().length == 0 && !s2.isEmpty()) {

               s2.clear();

               if(s2.isEmpty()) {

                   System.out.println("Yay 6");

               }

           }

           CallStack<Integer> s3 = new CallStack<>();

           s3.push(3);

           s3.push(2);

           s3.push(1);

           int i = 1;

           for(Integer item : s3) {

               if(i == item) System.out.println("Yay " + (6+i));

               else

                   System.out.println(item);

               i++;

           }

       }

   }

   output
   =====
   Yay 1
   Yay 2
   Yay 3
   Yay 4
   Yay 5
   Yay 6
   Yay 7
   Yay 8
   Yay 9

Add a comment
Know the answer?
Add Answer to:
how would I complete this code without calling any built-in java collection framework classes like ArrayList,...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • how do I change my code to generic form *********************************************************************** public class UnboundedStackQueue { //question#3 }...

    how do I change my code to generic form *********************************************************************** public class UnboundedStackQueue { //question#3 } class Stack { Node head; int size; Stack() //default constructor { this.head=null; this.size=0; } //Input = data //Output = void (just adds value to list) // method pushes elements on stack // time: O(1) // space: O(1) public void push(int data) { Node node=new Node(data); node.next=head; head=node; size++; } //Input = none //Output = top of stack // method pops value from top of...

  • I was told I need three seperate files for these classes is there anyway to tie...

    I was told I need three seperate files for these classes is there anyway to tie all these programs together into one program after doing that. I'm using netbeans btw. import java.util.ArrayList; import java.util.Scanner; /** * * */ public class MySorts {       public static void main(String[] args) {             Scanner input = new Scanner(System.in);             String sentence;             String again;             do {                   System.out                               .println("Enter a sentence, I will tell you if it is a palindrome: ");...

  • I have added a little Code but I need help with the rest. /** A class...

    I have added a little Code but I need help with the rest. /** A class of stacks whose entries are stored in a chain of nodes. Implement all methods in MyStack class Main Reference : text book or class notes Do not change or add data fields */ package PJ2; public class MyStack<T> implements StackInterface<T> {    // Data fields    private Node<T> topNode; // references the first node in the chain    private int numberOfEntries;       public...

  • Complete StackArray.java code below. Use StackArrayDemo.java to test your implementation of StackArray.java. StackArray.java below. public class...

    Complete StackArray.java code below. Use StackArrayDemo.java to test your implementation of StackArray.java. StackArray.java below. public class StackArray <T> { public static int CAPACITY = 100; private final T[] elements; private int topIndex; // Constructor public StackArray() { // Initialize elements // Initialize topIndex to -1 } public T peek() { // If topIndex is less than zero, return null. // Otherwise, return element from top of the stack. } public T pop() { // If topIndex is less than zero,...

  • What is wrong with my code, when I pass in 4 It will not run, without...

    What is wrong with my code, when I pass in 4 It will not run, without the 4 it will run, but throw and error. I am getting the error   required: no arguments found: int reason: actual and formal argument lists differ in length where T is a type-variable: T extends Object declared in class LinkedDropOutStack public class Help { /** * Program entry point for drop-out stack testing. * @param args Argument list. */ public static void main(String[] args)...

  • java create java program that make stack with LinkedList and stack is implement iterator. When stack’s iterator call next(), it pop its data. here is the example of output //by user 5 1 2 3 4 5 //then...

    java create java program that make stack with LinkedList and stack is implement iterator. When stack’s iterator call next(), it pop its data. here is the example of output //by user 5 1 2 3 4 5 //then output comes like this 5 4 3 2 1 Stack is empty. here is the code that i'm going to use class Stack<T> implements Iterator<T> {    LinkedList<T> list;       public Stack() {        list = new LinkedList<T>();    }       public boolean isEmpty() {        return list.isEmpty();   ...

  • Complete the implementation of the LinkedStack class presented in Chapter 13. Specifically, complete the implementations of...

    Complete the implementation of the LinkedStack class presented in Chapter 13. Specifically, complete the implementations of the peek, isEmpty, size, and toString methods. See Base_A06Q1.java for a starting place and a description of these methods. Here is the base given: /** * Write a description of the program here. * * @author Lewis et al., (your name) * @version (program version) */ import java.util.Iterator; public class Base_A06Q1 { /** * Program entry point for stack testing. * @param args Argument...

  • In Java. What would the methods of this class look like? StackADT.java public interface StackADT<T> {...

    In Java. What would the methods of this class look like? StackADT.java public interface StackADT<T> { /** Adds one element to the top of this stack. * @param element element to be pushed onto stack */ public void push (T element);    /** Removes and returns the top element from this stack. * @return T element removed from the top of the stack */ public T pop(); /** Returns without removing the top element of this stack. * @return T...

  • Can someone help me to figure that error I have put below. JAVA ----jGRASP exec: javac...

    Can someone help me to figure that error I have put below. JAVA ----jGRASP exec: javac -g P4Program.java P4Program.java:94: error: package list does not exist Iterator i = new list.iterator(); ^ 1 error ----jGRASP wedge2: exit code for process is 1. ----jGRASP: operation complete. Note: Below there are two different classes that work together. Each class has it's own fuctions/methods. import java.util.*; import java.io.*; public class P4Program{ public void linkedStackFromFile(){ String content = new String(); int count = 1; File...

  • Java. Must not use Java API java.util.Stack /** A class of stacks whose entries are stored in an ...

    Java. Must not use Java API java.util.Stack /** A class of stacks whose entries are stored in an array. Implement all methods in ArrayStack class using resizable array strategy, i.e. usedoubleArray() Must throw StackException during exception events in methods:    peek(), pop(), ArrayStack(int initialCapacity) Do not change or add data fields Do not add new methods */ import java.util.Arrays; public class Arraystack«Т> implements Stack!nterface«T> private TI stack;// Array of stack entries private int topIndex; /7 Index of top entry private static...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT