Results 1 to 19 of 19
- 07-26-2010, 01:41 AM #1
Member
- Join Date
- Jul 2010
- Posts
- 19
- Rep Power
- 0
Assistance needed ASAP: Postpix program
:confused:I have a project complex numbers (real & imaginary) that I have been working on which includes having two classes. The complex class has a toString to convert completx number to string, Add, sub, multiply methods that return results of two complex numbers closed in parens and fromString that accepts a StringTokenizer.
I'm having issues with the toString, and multiplication method.... Can some one help please..........?:confused:
- 07-26-2010, 01:51 AM #2
Sure. What is the issue?
Post code in code tags if needed to help show your problem.
- 07-26-2010, 01:59 AM #3
Member
- Join Date
- Jul 2010
- Posts
- 19
- Rep Power
- 0
Having trouble and not sure why it's not working...
Well, I do now why, it's because I'm a working about and after 10 years I'm trying to complete my degree and the very project I'm about to fail tonight..
Java Code:public class Project2 { public static void main (String[]args) { // reads input from the user System.out.println("Please input a postfix expression"); Scanner sc= new Scanner(System.in); String tempToken = new String(); StringTokenizer input = new StringTokenizer (sc.toString()); Complex result = new Complex(); Complex temp1= new Complex(); Stack<Complex> st1 = new Stack<Complex>(); Complex temp =new Complex(); Complex temp2 = new Complex(); //while loop to discect the input while(input.hasMoreTokens()) { sc= new Scanner (input.nextToken()); switch (sc.next()) { case "+": case "-": case "*": temp1= st1.pop(); temp2= st1.pop(); st1.push(new Complex(evaluate(temp1,temp2, sc.toString()))); break; default: /*complex number*/ st1.push(new Complex(sc.nextDouble())); // break; } } } public static double evaluate (Complex num1, Complex num2, String opcode ) { Complex temp = new Complex(); if(opcode =="+") return temp.add(num1, num2); else if (opcode == "-") return temp.subtract(num1, num2); else return temp.multiply(num1, num2); } }
Moderator Edit: Code tags addedJava Code:import complexnumbers.*; import java.util.*; import java.io.*; public class Complex { private double real; private double imag; // default constructor public Complex() { real= 0; imag= 0; } // overload constructor public Complex(double real) { this.real = real; this.imag = 0; } // overload constructor public Complex(double imag) { this.real = 0; this.imag = imag; } // overload constructor public Complex(double real , double imag) { this.real = real; this.imag = imag; } // accepts a StringTokenizer and returns a complex number public Complex fromString (StringTokenizer in) { double data= (double) in.nextToken(); Complex temp= new Complex(Double.parseDouble(in.nextToken()); } public String toString() { Console.out.println (" + real + "" + oper + "" + imag + "i""); } public Complex add(Complex num1, Complex num2) { double r; double i; r = num1.real + num2.real; i = num1.image + num2.imag; return new Complex(r,i); } public Complex subtract ( Complex num1, Complex num2) { double r; double i; r = num1.real - num2.real; i = num1.image - num2.imag; return new Complex(r,i); } public Complex multiply ( Complex num1, Complex num2) { double r; double i; r = (num1.real * num2.real) + (-(num1.imag * num2.imag)); i = (num1.real * num2.imag) + (num1.imag * num2.real); return new Complex(r,i); } }Last edited by Fubarable; 07-26-2010 at 03:28 AM. Reason: Code tags added
- 07-26-2010, 02:00 AM #4
Member
- Join Date
- Jul 2010
- Posts
- 19
- Rep Power
- 0
Sorry norm, I'm new to this, so didn't know how to post in code tags
- 07-26-2010, 02:26 AM #5
Code tags are placed before and after posted code to preserve formatting: [code ]
and [/code ] without the spaces.
I don't see any comments or description of what the program is supposed to do.
I'd rather not have to scroll up and back to read the comments and the code. Can they be all in one place?
What problems are you having? Be sure to post all error messages here.
- 07-26-2010, 02:44 AM #6
Member
- Join Date
- Jul 2010
- Posts
- 19
- Rep Power
- 0
Postpix program: I'm getting mutilple errors
:(OK, 1. The main method, which performs the expression evaluation. The algorithm for evaluating a postfix expression requires a stack of Complex numbers. The user should be allowed to enter and evaluate any number of expressions. A class method called evaluate, which accepts two complex numbers and an operator and returns the result of performing the operation on those two numbers.
2. The second class should be a class named Complex. Each complex number should contain a real and imaginary part. Each should be of type double. This class should contain the following methods.
1. A constructor.
2. A class method named fromString that accepts a StringTokenizer positioned at a complex number and returns that complex number.
3. A toString method that converts a complex number to a string.
4. A add method that adds two complex numbers and returns the result.
5. A subtract method that subtracts two complex numbers and returns the result.
Java Code:public class Project2 { public static void main (String[]args) { // reads input from the user System.out.println("Please input a postfix expression"); Scanner sc= new Scanner(System.in); String tempToken = new String(); StringTokenizer input = new StringTokenizer (sc.toString()); Complex result = new Complex(); Complex temp1= new Complex(); Stack<Complex> st1 = new Stack<Complex>(); Complex temp =new Complex(); Complex temp2 = new Complex(); //while loop to discect the input while(input.hasMoreTokens()) { sc= new Scanner (input.nextToken()); switch (sc.next()) { case "+": case "-": case "*": temp1= st1.pop(); temp2= st1.pop(); st1.push(new Complex(evaluate(temp1,temp2, sc.toString()))); break; default: /*complex number*/ st1.push(new Complex(sc.nextDouble())); // break; } } } public static double evaluate (Complex num1, Complex num2, String opcode ) { Complex temp = new Complex(); if(opcode =="+") return temp.add(num1, num2); else if (opcode == "-") return temp.subtract(num1, num2); else return temp.multiply(num1, num2); } } import complexnumbers.*; import java.util.*; import java.io.*; public class Complex { private double real; private double imag; // default constructor public Complex() { real= 0; imag= 0; } // overload constructor public Complex(double real) { this.real = real; this.imag = 0; } // overload constructor public Complex(double imag) { this.real = 0; this.imag = imag; } // overload constructor public Complex(double real , double imag) { this.real = real; this.imag = imag; } // accepts a StringTokenizer and returns a complex number public Complex fromString (StringTokenizer in) { double data= (double) in.nextToken(); Complex temp= new Complex(Double.parseDouble(in.nextToken()); } public String toString() { Console.out.println (" + real + "" + oper + "" + imag + "i""); } public Complex add(Complex num1, Complex num2) { double r; double i; r = num1.real + num2.real; i = num1.image + num2.imag; return new Complex(r,i); } public Complex subtract ( Complex num1, Complex num2) { double r; double i; r = num1.real - num2.real; i = num1.image - num2.imag; return new Complex(r,i); } public Complex multiply ( Complex num1, Complex num2) { double r; double i; r = (num1.real * num2.real) + (-(num1.imag * num2.imag)); i = (num1.real * num2.imag) + (num1.imag * num2.real); return new Complex(r,i); } }Last edited by Debonairj; 07-26-2010 at 02:47 AM.
- 07-26-2010, 02:48 AM #7
Ok, I see the definition of what the program is supposed to do,
now can you explain what your problem is?
Try to be specific.
- 07-26-2010, 02:51 AM #8
Member
- Join Date
- Jul 2010
- Posts
- 19
- Rep Power
- 0
I'm receiving mutilple console.out.println errors...
Complex.java:49: not a statement
Console.out.println (" + real + "" + oper + "" + imag + "i"");
^
- 07-26-2010, 02:52 AM #9
Member
- Join Date
- Jul 2010
- Posts
- 19
- Rep Power
- 0
Also on the project2 file, I'm receiving this...
Project2.java:3: package complexnumbers does not exist
import complexnumbers.*
- 07-26-2010, 02:53 AM #10
Member
- Join Date
- Jul 2010
- Posts
- 19
- Rep Power
- 0
Also, multiple:
Project2.java:49: incompatible types
found : Complex
required: double
return temp.add(num1, num2);
- 07-26-2010, 03:09 AM #11
Console.out.println
Where is the Console class defined? I'm not familiar with it. I use:
System.out.println
package complexnumbers does not exist
Where is the package: complexnumbers defined?
If its in a jar file, you need to add the jar file to the classpath so the javac command can search in it to find that package.
How are you compiling the program? Command prompt command line or IDE or?
Where is the class Complex defined?
Probably in the complexnumbers package. See above about the jar file.
- 07-26-2010, 03:15 AM #12
Member
- Join Date
- Jul 2010
- Posts
- 19
- Rep Power
- 0
I was using jgrasp and Eclipse and neither seems to work.. I'm sure I'm missing a lot about package, as I'm new to this after 8 years of being out of college...So pardon me if I seem very lost about things...
- 07-26-2010, 03:18 AM #13
Have you found the jar file with the package you need?
I don't use your IDE and have no idea how to use it.
You'll have to look in the help about how to add a jar file (library?) to the IDE for the javac compiler to use.
- 07-26-2010, 03:23 AM #14
Member
- Join Date
- Jul 2010
- Posts
- 19
- Rep Power
- 0
Ok, looking in jgrasp help now
- 07-26-2010, 03:45 AM #15
Member
- Join Date
- Jul 2010
- Posts
- 19
- Rep Power
- 0
I have found a jar.exe under c:\programfiles\JAVA\jdk1.6.0_12\bin.. The about in jgrasp is not much help... Not sure when you say point to the jar file, where exactly do I make this change.
- 07-26-2010, 01:30 PM #16
Sorry, by jar file I meant a file with the extension of .jar
jar.exe is an exe file. The extension says what type of file it is.
Classes are defined in libraries that are contained in jar files. You are missing a class definition for the complexnumbers. The compiler (javac.exe) uses the classpath to find the libraries to search for classes. You need to find the library with the missing classes and put it on the classpath.
- 07-27-2010, 05:06 AM #17
Member
- Join Date
- Jul 2010
- Posts
- 19
- Rep Power
- 0
Didn't find a .jar file on the machine
I ran a search on the machine and did not find a a .jar file anywhere.... Not sure what happen to it..
- 07-27-2010, 05:11 AM #18
Member
- Join Date
- Jul 2010
- Posts
- 19
- Rep Power
- 0
Let me correct myself... I found multiple .jar files and found one that said jgrasp.jar.zip. I unzipped and it had a bunch of .class files listed. But i'm not sure if this is the .jar file you were referring too.
- 07-27-2010, 01:37 PM #19
Why are you using the "import complexnumbers.*;" statement in your program?
Who told you to use it? Ask that person where that package is defined.
Sorry, I fixed on the missing package. Is there is a package defining complexnumbers or is there only your code?
Lets go back to compiling the Complex.java file. Forget about the missing jar file.
Where did you get that file from. It has several compile errors in it.
First remove the import complexnumbers.* statement.Last edited by Norm; 07-27-2010 at 01:47 PM.
Similar Threads
-
Java Programmers needed ASAP Tucson, AZ Swing, Maven, HTML, XML, SQL
By pbracey in forum Jobs OfferedReplies: 0Last Post: 07-19-2010, 10:25 PM -
Ascii To URL Assistance Needed
By Sean_J in forum New To JavaReplies: 2Last Post: 03-10-2010, 04:35 AM -
Help needed with 'shop' program
By jkhamler in forum New To JavaReplies: 1Last Post: 12-06-2009, 05:35 PM -
Need to end program by typing quit and not -1!!! I have pasted my code for assistance
By sarchie109 in forum New To JavaReplies: 7Last Post: 11-23-2009, 08:42 AM -
Need help ASAP with Payroll Program Part 2
By arrech326 in forum New To JavaReplies: 10Last Post: 11-17-2009, 10:17 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks