I was wondering if anyone had some general clean up advice on my code. I believe that more helper functions would be my case but I could be wrong. 30 lines maximum per method is my rule. I can't seem to figure out how to clean this up more, though.
Sample Input:
(1/3) (1/5) - (40/1) * #
(2/3) B * #
Sample Output
Expression 1 is: (1/3)(1/5)-(40/1)*
The value is: (16/3)
Intermediate results: (2/15)(16/3)
Expression 2 is: (2/3)B
Invalid Expression
Intermediate results:
import java.util.Scanner;
public class RpnEvaluator
{
private final int MAX_TOKEN = 100;
private Scanner stdin = new Scanner(System.in);
private Queue myQueue = new Queue(MAX_TOKEN);
private Stack myStack = new Stack(MAX_TOKEN);
public void run() throws java.io.IOException
{
int count = 1;
Fraction myInterMid;
while(stdin.hasNext())
{
runOnce(count++);
System.out.print("Intermediate results: ");
while(!myQueue.isEmpty())
{
myInterMid = (Fraction)myQueue.remove();
System.out.print(myInterMid.toString());
}
System.out.println();
clear(myStack, myQueue);
}
System.out.println("Normal Termination of Program 3.");
}
private boolean isOperator(String input)
{
String[] oprtr = {"+", "-", "*"};
for(String choice: oprtr)
if(choice.equals(input))
return true;
return false;
}
private boolean isOperand(String input)
{
if(input.charAt(0) == '(')
return true;
return false;
}
private Fraction runOperation(String choice, Fraction op2, Fraction op1)
{
Fraction newFract = new Fraction();
switch (choice)
{
case "*":
newFract = new Fraction(op1.times(op2));
break;
case "+":
newFract = new Fraction(op1.plus(op2));
break;
case "-":
newFract = new Fraction(op1.minus(op2));
break;
}
return newFract;
}
private void runOnce(int count)
{
Fraction op1 = null;
Fraction op2 = null;
clear(myStack, myQueue);
System.out.print("Expression " + count++ + " is: ");
doTypeCheck(op1, op2);
}
private void clear(Stack myStack, Queue myQueue)
{
myStack.clear();
myQueue.clear();
}
private void runTheOperator(Fraction op2, Fraction op1,
String readIn)
{
op1 = (Fraction)myStack.pop();
Fraction interMed = runOperation(readIn, op2, op1);
myStack.push(interMed);
myQueue.add(interMed);
}
private void doTypeCheck(Fraction op1, Fraction op2)
{
Fraction answer = null;
String readIn = "";
boolean valid = true;
readIn = stdin.next();
while(!readIn.equals("#") && valid == true)
{
if(!isOperator(readIn) && isOperand(readIn))
{
processOperand(readIn);
readIn = stdin.next();
}
else if(isOperator(readIn))
{
System.out.print(readIn);
if(myStack.isEmpty())
valid = false;
else
op2 = (Fraction)myStack.pop();
if(myStack.isEmpty())
{
valid = false;
throwLine(readIn);
}
else
{
runTheOperator(op2, op1, readIn);
readIn = stdin.next();
}
}
else
{
System.out.print(readIn);
valid = false;
throwLine(readIn);
}
}
System.out.println();
if(myStack.isEmpty())
valid = false;
else
answer = (Fraction)myStack.pop();
if(!myStack.isEmpty())
valid = false;
checkMessageValid(valid, answer);
}
private void checkMessageValid(boolean valid, Fraction answer)
{
if(valid == false)
System.out.println("Invalid Expression");
else
System.out.println("The value is: " + answer.toString());
}
private void throwLine(String line)
{
while(!line.equals("#"))
{
line = stdin.next();
}
}
private void processOperand(String readIn)
{
Fraction stringFract = null;
Fraction myFract = null;
stringFract = new Fraction(readIn);
System.out.print(stringFract.toString());
myFract = new Fraction(readIn);
myStack.push(myFract);
}
}