The Artima Developer Community
Sponsored Link

Java Answers Forum
stacker, can you find the faults?

2 replies on 1 page. Most recent reply: May 23, 2003 7:30 AM by Adam Duffy

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 2 replies on 1 page
bob

Posts: 1
Nickname: aka787
Registered: May, 2003

stacker, can you find the faults? Posted: May 23, 2003 4:51 AM
Reply to this message Reply
Advertisement
SO YOU THINK YOU CAN JAVA: IV SET THIS SIMPLE TEST, SHOULD TAKE 5 MINS, SO WHAT ARE YOU WAITING FOR? WHAT IS THE CORRECT AND COMPLETE CODE?

AKA787

// Stacker.java:
//
// A partial implementation of a simple stack mechanism --
// are you good enough to complete it? Whats wrong with the rest of the code?
//
// Aka787
//

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

class Stack
{
// Stack data structure
private int size;
private int[] stack;
private int index;
public boolean empty();
public Object peek();
public Object pop();
public Object push(Object item);
public int search(Object o);

// Constructor
Stack(int size)
{
this.size = size;
stack = new int[size];
}

// Returns the number of elements in the stack
int count()
{
return index;
}

// Returns true if stack empty, otherwise false
boolean isEmpty()
{
return index == 0;
}

// Returns true if stack full, otherwise false
boolean isFull()
{
return index == size;
}

// Class invariant [Incomplete]
boolean StackClassInvariant()
{
// Stack index always within valid range
}

// Pushes the value n on top of the stack [Incomplete]
void push(int n)
{
// stack not full

// *** code goes here ***

// number of elements within stack is increased by 1
}

// Removes and returns the top element from the stack [Incomplete]
int pop()
{
// stack not empty

// *** code goes here ***

// number of elements within stack is decreased by 1
}

// Resets the stack index [Incomplete]
void reset()
{
// always applicable

// *** code goes here ***

// number of elements within stack is 0
}

// Returns the value of the top element on the stack [Incomplete]
int top()
{
// stack not empty
while ( index>0 )
{
screen.println("top element on the stack "+Stack.peek().toString());
}

}

}

class Stacker
{
static BufferedReader keyboard =
new BufferedReader (new InputStreamReader (System.in));
static PrintWriter screen =
new PrintWriter (System.out, true);

public static void main(String[] args ) throws IOException
{
final int SIZE = 5;
int cmd;
boolean quit = false;
Stack MyStack = new Stack(SIZE);

while (!quit){
screen.print("Command (Push=1, Pop=2, Top=3, Empty=4, Full=5, Reset=6,
Quit=7): ");
screen.flush();
cmd = Integer.parseInt(keyboard.readLine().trim());
switch (cmd)
{ case 1: screen.print("Data please: ");screen.flush();

MyStack.push(Integer.parseInt(keyboard.readLine().trim()));
break;
case 2: screen.println("Poped = " + MyStack.pop());
break;
case 3: screen.println("Top = " + MyStack.top());
break;
case 4: screen.println("Empty = " + MyStack.isEmpty());
break;
case 5: screen.println("Full = " + MyStack.isFull());
break;
case 6: MyStack.reset();
screen.println("Reset complete");
break;
case 7: quit=true;
break;
default: screen.println("\n*** Invalid command ***\n");
}
}
screen.println("\nStacker terminated\n");

}
}


Adam Duffy

Posts: 168
Nickname: adamduffy
Registered: Feb, 2003

Re: stacker, can you find the faults? Posted: May 23, 2003 7:28 AM
Reply to this message Reply
My suggestion would be not to re-invent the wheel and use the java.util.Stack[/java] class. (And that took all of 5 seconds...)

Adam

Adam Duffy

Posts: 168
Nickname: adamduffy
Registered: Feb, 2003

Re: stacker, can you find the faults? Posted: May 23, 2003 7:30 AM
Reply to this message Reply
Oops. Let's try that again.

My suggestion would be not to re-invent the wheel and use the java.util.Stack class. (And that took all of 5 seconds...)

Adam

Flat View: This topic has 2 replies on 1 page
Topic: how do i resize applet Previous Topic   Next Topic Topic: Error with java SimpleServlet - Urgent

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use