• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

help with project please

 
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am trying to write a command line calculator
that when typed should look like something like this: C:\\java cs212.calculator.Calculator prefix + x 8 8 x 6 6
the first argument should be what notation the problem is written in. The other arguments(which can vary in number) will be the problem itself.
Here's what i need to do:
1. Take arguments from command line
2. Convert to a string
3. Tokenize on the string
4. Feed into a queue, stack, linked list
5. use the datastructure
6. print out answer and change notaion to suffix prefix and postfix
ex)
c:>java cs212.calculator.Calculator prefix + x 8 8 x 6 6
Prefix + x 8 8 x 6 6
Infix 8 x 8 + 6 x 6
Postfix 8 8 x 6 6 x +
100
I've done most of it except i have one really hard problem that i have no idea how to go about.
How do I convert prefix or postfix to infix especially since the problem varies in length. Oh. I decided to use a stack for a prefix notation argument because its first in last out so all i have to do is pop it contents and it should be in postfix notation.
here's what i have so far:
this is the class that takes the argument and decides where to send it:

package cs212.calculator;

public class Calculator {
String s;
PrefixStack ps;
InfixQueue iq;
public Calculator(String s) {
this.s = s;
if (s.startsWith("prefix"))
ps = new PrefixStack(s);
else if (s.startsWith("infix"))
iq = new InfixQueue(s);
else if (s.startsWith("postfix"))
System.out.println("postfix " + s);
else
System.out.println("Error: ModeOfExecutionNotFound");
}

public static void main(String[] args) {
if (args.length < 2){
System.out.println("Usage: ParametersNotMet program will terminate");
System.exit(0);
} else {
String s = args[0] + " ";
for (int i = 1;i < args.length; i++){
s += (args[i] + " ");
}
Calculator calculator1 = new Calculator(s);
}
}
}
Here is my file that takes care of a prefix:
package cs212.calculator;
import java.util.*;
public class PrefixStack {
/*variables*/
private String s;
private StringTokenizer st;
private Stack stack;
private Stack stack2;
/*constructor*/
public PrefixStack(String s) {
this.s = s;
/*intialize StringTokenizer and tokenize on String s*/
st = new StringTokenizer(s," ");
/*initialize Java DataStructure Stack, "First in Last out" */
stack = new Stack();
stack2 = new Stack();
/*push tokenz on to Stack*/
while(st.hasMoreTokens()){
stack.push(st.nextToken());
}
/**********************from here **************************/
System.out.println(s);
System.out.println(convertToSufix(s, stack));
System.out.println(convertToInfix(s, stack));
}
public String convertToSufix(String s, Stack stack){
String s2 = "Sufix ";
while(!stack.empty()){
if(stack.peek().equals("prefix")){
break;
}
s2 += (stack.pop() + " ");
}
return s2;
}
public String convertToInfix(String s, Stack stack){
String s2 = "Infix ";
return s2;
}
}
as you can see my in my convertToInfix method i have nothing going on. any help would be greatly apprecitated.
Thanks in advance.
 
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
kevin,
Please note that you can preserve code formatting by surrounding it with the [code] and [/code] UBB Tags.
Formatted code is often much easier to read and understand.
[ August 18, 2002: Message edited by: Dirk Schreckmann ]
 
Sheriff
Posts: 7001
6
Eclipse IDE Python C++ Debian Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't want to sound sarcastic here, but is cs212 a second year CS course? Have you done an algorithms course yet (maybe that's what this is)?
What you are asking for is not really a Java problem. Your code, although a little "rough round the edges", shows a reasonable understanding of Java and how to use it. What you are looking for should be described in any reasonable undergraduate algorithms book. Or you can go to Knuth if you are up to it. I don't want to say too much, because being able to find algorithms for this sort of thing in a library will serve you very well later.
 
kevin comario
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks frank. I will do that. Here is what I've done so far. I decided to seperate the operators and variables into to sepreate stacks so for instance if the argument i took in looked like this:
+ x 8 8 x 6 6
stackOne would contain + X X
and stackTwo would contain 8 8 6 6
Doing this has helped me a little. The problem i am having is with is i dont know how to pop them in a correct order that will change the prefix statement to a infix statement.
it should look like this (8 x 8) + (6 x 6).

Heres what my code looks like right now

Any help is appreciated.
 
kevin comario
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oh. I forgot to mention I have no experience with algorithms. That is the class I am supposed to take next semester, But thanks for the advice frank i'm actually of to the library right now.
 
reply
    Bookmark Topic Watch Topic
  • New Topic