KaptainCavy's Profile User Rating: -----

Reputation: 0 Apprentice
Group:
New Members
Active Posts:
13 (0.02 per day)
Joined:
25-June 12
Profile Views:
266
Last Active:
User is offline Sep 18 2012 09:51 AM
Currently:
Offline

Previous Fields

Country:
US
OS Preference:
Windows
Favorite Browser:
Chrome
Favorite Processor:
Intel
Favorite Gaming Platform:
PC
Your Car:
BMW
Dream Kudos:
0
Icon   KaptainCavy has not set their status

Posts I've Made

  1. In Topic: Stack as an array

    Posted 10 Sep 2012

    I am getting the following errors after making the changes sepp2k recommended:

    P2Cavallaro.java:43: error: a generic class may not extend java.lang.Throwable
    public class StackException extends RuntimeException {
    ^
    P2Cavallaro.java:54: error: non-static variable this cannot be referenced from a static context
    Stack<Character> openDelimiterStack = new Stack<Character>();
    ^
    P2Cavallaro.java:56: error: non-static variable expression cannot be referenced from a static context
    int characterCount = expression.length();
    ^
    P2Cavallaro.java:63: error: non-static variable expression cannot be referenced from a static context
    nextCharacter = expression.charAt(index);
    ^
    P2Cavallaro.java:74: error: incompatible types
    char openDelimiter = openDelimiterStack.pop();
    ^
    required: char
    found: void

    P2Cavallaro.java:84: error: cannot return a value from method whose result type is void
    return isBalanced;


    // program 
    public final class P2Cavallaro
    {
    
    	// stack and stack methods 	
    	final String expression = "({[]})";
    	
    	public class Stack<T>{
          private int top;
          private int[] storage;
     
          Stack(int capacity) {
                if (capacity <= 0)
                      throw new IllegalArgumentException(
                                 "Stack's capacity must be positive");
                storage = new int[capacity];
                top = -1;
          }
     
          void push(int value) {
                if (top == storage.length)
                      throw new StackException("Stack's underlying storage is overflow");
                top++;
                storage[top] = value;
          }
     
          int peek() {
                if (top == -1)
                      throw new StackException("Stack is empty");
                return storage[top];
          }
     
          void pop() {
                if (top == -1)
                      throw new StackException("Stack is empty");
                top--;
          }
     
          boolean isEmpty() {
                return (top == -1);
          }
     
          public class StackException extends RuntimeException {
                public StackException(String message) {
                      super(message);
                }
          }
    	}
    	
    	// main
    	public static void main(String[] args)
       {
    		
    	   Stack<Character> openDelimiterStack = new Stack<Character>();
             
          int characterCount = expression.length();
          boolean isBalanced = true;
          int index = 0;
          char nextCharacter = ' ';
             
          for (; isBalanced && (index < characterCount); index++)
          {
             nextCharacter = expression.charAt(index);
             switch (nextCharacter)
             {
                case '(': case '[': case '{':
                   openDelimiterStack.push(nextCharacter);
                   break;
                case ')': case ']': case '}':
                   if (openDelimiterStack.isEmpty())
                      isBalanced = false;
                   else
                   {
                      char openDelimiter = openDelimiterStack.pop();
                      isBalanced = isPaired(openDelimiter, nextCharacter);
                   }
                   break;
                default: break;
             } 
          }
             
          if (!openDelimiterStack.isEmpty())
             isBalanced = false;
          return isBalanced;
       }
             
       // Returns true if the given characters, open and close, form a pair
       // of parentheses, brackets, or braces.
       private static boolean isPaired(char open, char close)
       {
          return (open == '(' && close == ')') ||
                 (open == '[' && close == ']') ||
                 (open == '{' && close == '}');
       } 
    }
    
    

My Information

Member Title:
New D.I.C Head
Age:
Age Unknown
Birthday:
Birthday Unknown
Gender:

Contact Information

E-mail:
Private

Friends

KaptainCavy hasn't added any friends yet.

Comments

KaptainCavy has no profile comments yet. Why not say hello?