From ggc
|
001 packagepackage is used to name the directory or folder a class is in intermediate;
002
003 importimport means to make the classes and/or packages available in this program java.io.ByteArrayInputStream;
004 importimport means to make the classes and/or packages available in this program java.util.Scanner;
005 importimport means to make the classes and/or packages available in this program java.util.Stack;
006 importimport means to make the classes and/or packages available in this program java.util.StringTokenizer;
007 importimport means to make the classes and/or packages available in this program wiki.*;
008
009
010 publicpublic is used to indicate unrestricted access (any other class can have access) classclass is a group of fields and methods used for making objects StackCalculator
011 {open braces start code blocks and must be matched with a close brace
012
013 /*publicpublic is used to indicate unrestricted access (any other class can have access) staticstatic means that an instance is not required for access (class level access) doubledouble is the type for numbers that can contain decimal fractions getExpressionValue(String expression)
014 {open braces start code blocks and must be matched with a close brace
015 ifif executes the next statement only if the condition in parenthesis evaluates to true(expression.contains("+"))
016 {open braces start code blocks and must be matched with a close brace
017 ByteArrayInputStream bis=this assignment operator makes the left side equal to the right sidenewnew is used to create objects by calling the constructor ByteArrayInputStream(expression.getBytes());
018 Scanner scanner=this assignment operator makes the left side equal to the right sidenewnew is used to create objects by calling the constructor Scanner(bis);
019 doubledouble is the type for numbers that can contain decimal fractions x=this assignment operator makes the left side equal to the right sidescanner.nextDouble();
020 }close braces end code blocks and must match an earlier open brace
021 ifif executes the next statement only if the condition in parenthesis evaluates to true(expression.contains("-"))
022 {open braces start code blocks and must be matched with a close brace
023
024 }close braces end code blocks and must match an earlier open brace
025 ifif executes the next statement only if the condition in parenthesis evaluates to true(expression.contains("*"))
026 {open braces start code blocks and must be matched with a close brace
027
028 }close braces end code blocks and must match an earlier open brace
029 ifif executes the next statement only if the condition in parenthesis evaluates to true(expression.contains("/"))
030 {open braces start code blocks and must be matched with a close brace
031
032 }close braces end code blocks and must match an earlier open brace
033 }close braces end code blocks and must match an earlier open brace*/
034
035 publicpublic is used to indicate unrestricted access (any other class can have access) staticstatic means that an instance is not required for access (class level access) String getInnerMostExpression(String expression)
036 {open braces start code blocks and must be matched with a close brace
037 intint is the type for whole numbers and it is short for integer firstRight=this assignment operator makes the left side equal to the right sideexpression.indexOf(")");
038 ifif executes the next statement only if the condition in parenthesis evaluates to true(firstRight<0) returnreturn means to provide the result of the method and/or cease execution of the method immediately expression;
039 intint is the type for whole numbers and it is short for integer lastLeft=this assignment operator makes the left side equal to the right sideexpression.substring(0, firstRight).lastIndexOf("(");
040 String contents=this assignment operator makes the left side equal to the right sideexpression.substring(lastLeft+adds two numbers together or concatenates Strings together1, firstRight);
041 returnreturn means to provide the result of the method and/or cease execution of the method immediately contents;
042 }close braces end code blocks and must match an earlier open brace
043
044 privateprivate is used to restrict access to the current class only staticstatic means that an instance is not required for access (class level access) String getStackText(Stack stack)
045 {open braces start code blocks and must be matched with a close brace
046 String text=this assignment operator makes the left side equal to the right side"B ";
047 forfor is a looping structure for repeatedly executing a block of code(intint is the type for whole numbers and it is short for integer i=this assignment operator makes the left side equal to the right side0; i<stack.size(); i++this is the increment operator, which increases the variable by 1)
048 {open braces start code blocks and must be matched with a close brace
049 text+=this increases the variable on the left by the value on the rightstack.get(i)+adds two numbers together or concatenates Strings together" ";
050 }close braces end code blocks and must match an earlier open brace
051 returnreturn means to provide the result of the method and/or cease execution of the method immediately text;
052 }close braces end code blocks and must match an earlier open brace
053
054 /**
055 * @paramthis is the Javadoc tag for documenting the purpose of parameters args
056 */
057 publicpublic is used to indicate unrestricted access (any other class can have access) staticstatic means that an instance is not required for access (class level access) voidvoid means the method does not return a value mainThe main method is the place where applications begin executing.(String[brackets are typically used to declare, initialize and index (indicate which element of) arrays]brackets are typically used to declare, initialize and index (indicate which element of) arrays args)
058 {open braces start code blocks and must be matched with a close brace
059 Wiki.out.println("Enter expression:");
060 Scanner scanner=this assignment operator makes the left side equal to the right sidenewnew is used to create objects by calling the constructor Scanner(Wiki.in);
061 scanner.useDelimiter("\n");
062 String expression=this assignment operator makes the left side equal to the right sidescanner.next();
063
064 Stack<Double> numbers=this assignment operator makes the left side equal to the right sidenewnew is used to create objects by calling the constructor Stack<Double>();
065 Stack<Character> operators=this assignment operator makes the left side equal to the right sidenewnew is used to create objects by calling the constructor Stack<Character>();
066 StringTokenizer tokenizer=this assignment operator makes the left side equal to the right sidenewnew is used to create objects by calling the constructor StringTokenizer(expression, "*/+-", truetrue is the boolean value that is the opposite of false);
067 whilewhile is a looping structure for executing code repeatedly(tokenizer.hasMoreElements())
068 {open braces start code blocks and must be matched with a close brace
069 String element=this assignment operator makes the left side equal to the right sidetokenizer.nextToken();
070 element=this assignment operator makes the left side equal to the right sideelement.trim();
071 Wiki.out.println("Element: "+adds two numbers together or concatenates Strings togetherelement);
072 trytry is for executing a code block that may experience exceptions (errors)
073 {open braces start code blocks and must be matched with a close brace
074 doubledouble is the type for numbers that can contain decimal fractions x=this assignment operator makes the left side equal to the right sideDouble.parseDouble(element);
075 numbers.push(x);
076 }close braces end code blocks and must match an earlier open brace
077 catchcatch means to handle an exception that may occur(NumberFormatException nfe)
078 {open braces start code blocks and must be matched with a close brace
079 charchar is the type for a single letter or symbol and it is short for character operator=this assignment operator makes the left side equal to the right sideelement.charAt(0);
080 ifif executes the next statement only if the condition in parenthesis evaluates to true(operators.size()>0)
081 {open braces start code blocks and must be matched with a close brace
082
083 charchar is the type for a single letter or symbol and it is short for character onTop=this assignment operator makes the left side equal to the right sideoperators.peek();
084 //boolean higherPrecedence=precedenceNondecreasing(operator, onTop);
085 ifif executes the next statement only if the condition in parenthesis evaluates to true(operator==this is the comparison operator which evaluates to true if both sides are the same'-' ||this is boolean or, meaning if either or both are true then the result is true operator==this is the comparison operator which evaluates to true if both sides are the same'+adds two numbers together or concatenates Strings together')
086 {open braces start code blocks and must be matched with a close brace
087 operators.pop();
088 doubledouble is the type for numbers that can contain decimal fractions first=this assignment operator makes the left side equal to the right sidenumbers.pop();
089 doubledouble is the type for numbers that can contain decimal fractions second=this assignment operator makes the left side equal to the right sidenumbers.pop();
090 ifif executes the next statement only if the condition in parenthesis evaluates to true(onTop==this is the comparison operator which evaluates to true if both sides are the same'+adds two numbers together or concatenates Strings together') numbers.push(first+adds two numbers together or concatenates Strings togethersecond);
091 ifif executes the next statement only if the condition in parenthesis evaluates to true(onTop==this is the comparison operator which evaluates to true if both sides are the same'-') numbers.push(second-first);
092 ifif executes the next statement only if the condition in parenthesis evaluates to true(onTop==this is the comparison operator which evaluates to true if both sides are the same'*') numbers.push(first*second);
093 ifif executes the next statement only if the condition in parenthesis evaluates to true(onTop==this is the comparison operator which evaluates to true if both sides are the same'/') numbers.push(second/first);
094 }close braces end code blocks and must match an earlier open brace
095
096 }close braces end code blocks and must match an earlier open brace
097 operators.push(operator);
098 }close braces end code blocks and must match an earlier open brace
099 Wiki.out.println("Operators "+adds two numbers together or concatenates Strings togethergetStackText(operators));
100 Wiki.out.println("Numbers "+adds two numbers together or concatenates Strings togethergetStackText(numbers));
101 }close braces end code blocks and must match an earlier open brace
102 whilewhile is a looping structure for executing code repeatedly(operators.size()>0)
103 {open braces start code blocks and must be matched with a close brace
104 charchar is the type for a single letter or symbol and it is short for character onTop=this assignment operator makes the left side equal to the right sideoperators.pop();
105 doubledouble is the type for numbers that can contain decimal fractions first=this assignment operator makes the left side equal to the right sidenumbers.pop();
106 doubledouble is the type for numbers that can contain decimal fractions second=this assignment operator makes the left side equal to the right sidenumbers.pop();
107 ifif executes the next statement only if the condition in parenthesis evaluates to true(onTop==this is the comparison operator which evaluates to true if both sides are the same'+adds two numbers together or concatenates Strings together') numbers.push(first+adds two numbers together or concatenates Strings togethersecond);
108 ifif executes the next statement only if the condition in parenthesis evaluates to true(onTop==this is the comparison operator which evaluates to true if both sides are the same'-') numbers.push(second-first);
109 ifif executes the next statement only if the condition in parenthesis evaluates to true(onTop==this is the comparison operator which evaluates to true if both sides are the same'*') numbers.push(first*second);
110 ifif executes the next statement only if the condition in parenthesis evaluates to true(onTop==this is the comparison operator which evaluates to true if both sides are the same'/') numbers.push(second/first);
111 Wiki.out.println("Operators "+adds two numbers together or concatenates Strings togethergetStackText(operators));
112 Wiki.out.println("Numbers "+adds two numbers together or concatenates Strings togethergetStackText(numbers));
113 }close braces end code blocks and must match an earlier open brace
114 //String innerMost=getInnerMostExpression(expression);
115 doubledouble is the type for numbers that can contain decimal fractions result=this assignment operator makes the left side equal to the right sidenumbers.pop();
116
117 Wiki.out.println("Result is "+adds two numbers together or concatenates Strings togetherresult);
118 }close braces end code blocks and must match an earlier open brace
119
120 privateprivate is used to restrict access to the current class only staticstatic means that an instance is not required for access (class level access) booleanboolean is a type that is either true or false precedenceNondecreasing(charchar is the type for a single letter or symbol and it is short for character operator, charchar is the type for a single letter or symbol and it is short for character onTop)
121 {open braces start code blocks and must be matched with a close brace
122 ifif executes the next statement only if the condition in parenthesis evaluates to true(onTop==this is the comparison operator which evaluates to true if both sides are the same'*' ||this is boolean or, meaning if either or both are true then the result is true onTop==this is the comparison operator which evaluates to true if both sides are the same'/' ||this is boolean or, meaning if either or both are true then the result is true operator==this is the comparison operator which evaluates to true if both sides are the same'+adds two numbers together or concatenates Strings together' ||this is boolean or, meaning if either or both are true then the result is true operator==this is the comparison operator which evaluates to true if both sides are the same'-')
123 returnreturn means to provide the result of the method and/or cease execution of the method immediately truetrue is the boolean value that is the opposite of false;
124 returnreturn means to provide the result of the method and/or cease execution of the method immediately falsefalse is a value for the boolean type and means not true;
125 }close braces end code blocks and must match an earlier open brace
126
127 }close braces end code blocks and must match an earlier open brace
|
Download/View intermediate/StackCalculator.java