Question

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) {
LinkedDropOutStack<Integer> stack = new LinkedDropOutStack<Integer>(4);
  
System.out.println("DROP-OUT STACK TESTING");
  
stack.push(1);
stack.push(2);
stack.push(3);
stack.push(4);   
stack.push(5);   
  
System.out.println("The size of the stack is: " + stack.size());
if(!stack.isEmpty())
System.out.println("The stack contains:\n" + stack.toString());
  
stack.pop();
stack.push(7);
stack.push(8);
  
System.out.println("The size of the stack is: " + stack.size());
if(!stack.isEmpty())
System.out.println("The stack contains:\n" + stack.toString());
}
  
public static class LinkedDropOutStack<T> implements DropoutStackADT<T> {   
//TODO: Implement me.
private final int DEFAULT_CAPACITY = 8;

// refers to the item in the array being indexed

private int cIndex;
private int count;//counts the number in the stack
private T[] stack;

public void DropOutStackADT() {
cIndex = 0;
count = 0;
stack = (T[]) (new Object[DEFAULT_CAPACITY]);
}


public void DropOutStackADT(int initialCapacity) {
cIndex = 0;
count = 0;
stack = (T[]) (new Object[initialCapacity]);
}


public T peek() throws EmptyCollectionException {
if (isEmpty())
throw new EmptyCollectionException("Drop-out Stack");
return stack[(cIndex + stack.length - 1)%stack.length];
}


public T pop() throws EmptyCollectionException {
if (isEmpty())
throw new EmptyCollectionException("Drop-out Stack");
cIndex = (cIndex + stack.length - 1)%stack.length;
T result = stack[cIndex];
stack[cIndex] = null;
count--;
return result;
}
  

public void push(T element)throws EmptyCollectionException {
cIndex = cIndex % stack.length;
stack[cIndex] = element;
cIndex++;
if (count != stack.length)
count++;
}


public int size() {
return count;
}
  

public boolean isEmpty() {
return size() == 0;
}
  

public String toString() {
String str = "";
int temp = cIndex;
int length = size();
while(length != 0) {
str += stack[(temp + stack.length - 1)%stack.length] + " ";
temp--;
length--;
}

return str;
}
}
}

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

//project.java

public class project {

public static void main(String[] args) {
LinkedDropOutStack<Integer> stack = new LinkedDropOutStack<Integer>(4);
System.out.println("DROP-OUT STACK TESTING");
  
stack.push(1);
stack.push(2);
stack.push(3);
stack.push(4);   
stack.push(5);   
  
System.out.println("The size of the stack is: " + stack.size());
if(!stack.isEmpty())
System.out.println("The stack contains:\n" + stack.toString());
  
stack.pop();
stack.push(7);
stack.push(8);
  
System.out.println("The size of the stack is: " + stack.size());
if(!stack.isEmpty())
System.out.println("The stack contains:\n" + stack.toString());
}
  
public static class LinkedDropOutStack<T> implements StackADT<T> {  
   private int top = 0;
private T[] stack;
  
   @SuppressWarnings("unchecked")
           public LinkedDropOutStack(int initialCapacity) {
stack = (T[])(new Object[initialCapacity]);
   }

   public void push(T element) {
if (size() == stack.length) {
for (int i = 0; i < stack.length - 1; i++) {
   stack[i] = stack[i + 1];
}
       stack[stack.length - 1] = element;
} else {
   stack[top] = element;
   top++;
}
}
  
   public T pop() throws EmptyCollectionException {
if (isEmpty())
throw new EmptyCollectionException("stack");
  
top--;
T result = stack[top];
stack[top] = null;
  
return result;
}

   public T peek() throws EmptyCollectionException {
if (isEmpty())
throw new EmptyCollectionException("stack");
  
return stack[top-1];
}

   public boolean isEmpty() {
return (top == 0);
}

   public int size() {
   return top;
}
  
   public String toString() {
   String result = "";
               for (int i = top-1; i >= 0; i--)
       result += stack[i] + "\n";
return result;
}
}
}

=======================================================================

//StackADT.java

public interface StackADT<T>
{
/**
* Adds the specified element to the top of this stack.
* @param element element to be pushed onto the stack
*/
public void push(T element);
  
/**
* Removes and returns the top element from this stack.
* @return the element removed from the stack
*/
public T pop();

/**
* Returns without removing the top element of this stack.
* @return the element on top of the stack
*/
public T peek();
  
/**
* Returns true if this stack contains no elements.
* @return true if the stack is empty
*/
public boolean isEmpty();

/**
* Returns the number of elements in this stack.
* @return the number of elements in the stack
*/
public int size();

/**
* Returns a string representation of this stack.
* @return a string representation of the stack
*/
public String toString();
}

===================================================================

//EmptyCollectionException.java

public class EmptyCollectionException extends RuntimeException
{
/**
* Sets up this exception with an appropriate message.
* @param collection the name of the collection
*/
public EmptyCollectionException(String collection)
{
super("The " + collection + " is empty.");
}
}

===================================================================

sample output:

Add a comment
Know the answer?
Add Answer to:
What is wrong with my code, when I pass in 4 It will not run, without...
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
  • 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 addition to the base files, three additional files are attached: EmptyCollectionException.java, LinearNode.java, and StackADT.java. These...

    In addition to the base files, three additional files are attached: EmptyCollectionException.java, LinearNode.java, and StackADT.java. These files will need to be added to your Java project. They provide data structure functionality that you will build over. It is suggested that you test if these files have been properly added to your project by confirming that Base_A05Q1.java compiles correctly. Complete the implementation of the ArrayStack class. Specifically, complete the implementations of the isEmpty, size, and toString methods. See Base_A05Q1.java for a...

  • I need to implement a stack array but the top of the stack has to be...

    I need to implement a stack array but the top of the stack has to be Initialize as the index of the last location in the array.    //Array implementation of stacks.    import java.util.Arrays;       public class ArrayStack implements Stack {        //Declare a class constant called DEFAULT_STACK_SIZE with the value 10.           private static final int DEFAULT_STACK_SIZE = 10;           /* Declare two instance variables:            1. An integer called...

  • *JAVA* Can somebody take a look at my current challenge? I need to throw a different...

    *JAVA* Can somebody take a look at my current challenge? I need to throw a different exception when a)(b is entered. It should throw "Correct number of parenthesis but incorrect syntax" The code is as follows. Stack class: public class ArrayStack<E> {    private int top, size;    private E arrS[];    private static final int MAX_STACK_SIZE = 10;    public ArrayStack() {        this.arrS = (E[]) new Object[MAX_STACK_SIZE];        this.top = size;        this.size = 0;...

  • There is a data structure called a drop-out stack that behaves like a stack in every...

    There is a data structure called a drop-out stack that behaves like a stack in every respect except that if the stack size is n, then when the n+1element is pushed, the bottom element is lost. Implement a drop-out stack using links, by modifying the LinkedStack code. (size, n, is provided by the constructor. Request: Please create a separate driver class, in a different file, that tests on different types of entries and show result of the tests done on...

  • I need help with this code This is what I need to do: Implement the Stack...

    I need help with this code This is what I need to do: Implement the Stack Class with an ArrayList instead of an array, including the following functions: • empty • push • peek • pop • overrided toString( ) function which returns all of the stack’s contents Things to note: • You no longer need a size. • You no longer need to define a constant DEFAULT_CAPACITY. Since ArrayLists grow dynamically. • Whenever possible, use ArrayList functions instead of...

  • Currently, I'm getting this as my output: "Exception in thread "main" java.lang.NullPointerException    at InfixExpression.execute(InfixExpression.java:98)   ...

    Currently, I'm getting this as my output: "Exception in thread "main" java.lang.NullPointerException    at InfixExpression.execute(InfixExpression.java:98)    at InfixExpression.Evaluate(InfixExpression.java:65)    at InfixExpression.setWholeExpr(InfixExpression.java:24)    at InfixExpression.<init>(InfixExpression.java:17)    at Main.testHW1(Main.java:17)    at Main.main(Main.java:6)" I need to get this as my output: "Testing InfixExpression: When passing null, the String and double = Infix String: , result: 0.0 When passing a valid String, the String and double = Infix String: ( 234.5 * ( 5.6 + 7.0 ) ) / 100.2, result: 29.488023952095805 ..." I...

  • JAVA Lab Create a class called ArrayBasedStack. Declare the following variables: • data: references an array...

    JAVA Lab Create a class called ArrayBasedStack. Declare the following variables: • data: references an array storing elements in the list • topOfStack: an int value representing the location of the stack top in the array • INITIAL_CAPACITY: the default capacity of the stack public class ArrayBasedStack <E> { private E[] data; private int topOfStack; private static final int INITIAL_CAPACITY = 5; } Add a constructor that will initialize the stack with a user-defined initial capacity. The top of the...

  • ------------------------------------------------------------------------------------------------------------ CODE ALREADY HAVE BELOW--------------------------------------------------------------------------------------- public class LinkedQueue<T>

    ------------------------------------------------------------------------------------------------------------ CODE ALREADY HAVE BELOW--------------------------------------------------------------------------------------- public class LinkedQueue<T> implements QueueADT<T> {    private int count;    private LinearNode<T> head;    private LinearNode<T> tail;               public LinkedQueue()    {        count = 0;        head = null;        tail = null;    }    @Override    public void enqueue(T element)    {        LinearNode<T> node = new LinearNode<T> (element);        if(isEmpty())            head = node;        else           ...

  • Im writing a method to evaluate a postfix expression. Using my own stack class. Here is my code but I keep getting a classcastexception where it says java.lang.Character cannot be cast to java.lang,In...

    Im writing a method to evaluate a postfix expression. Using my own stack class. Here is my code but I keep getting a classcastexception where it says java.lang.Character cannot be cast to java.lang,Integer. Im not sure how to fix this. public class Evaluator { public static void evaluatePost(String postFix)    {        LinkedStack stack2 = new LinkedStack();        int val1;        int val2;        int result;        for(int i = 0; i < postFix.length(); i++)        {            char m = postFix.charAt(i);            if(Character.isDigit(m))            {                stack2.push(m);            }            else            {               ...

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
ADVERTISEMENT