Results 1 to 19 of 19
  1. #1
    Debonairj is offline Member
    Join Date
    Jul 2010
    Posts
    19
    Rep Power
    0

    Default 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:

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,518
    Rep Power
    25

    Default

    Sure. What is the issue?
    Post code in code tags if needed to help show your problem.

  3. #3
    Debonairj is offline Member
    Join Date
    Jul 2010
    Posts
    19
    Rep Power
    0

    Default 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);
    
    		
    	}
    }






    Java 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);
    	}
    	
    }
    Moderator Edit: Code tags added
    Last edited by Fubarable; 07-26-2010 at 03:28 AM. Reason: Code tags added

  4. #4
    Debonairj is offline Member
    Join Date
    Jul 2010
    Posts
    19
    Rep Power
    0

    Default

    Sorry norm, I'm new to this, so didn't know how to post in code tags

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,518
    Rep Power
    25

    Default

    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.

  6. #6
    Debonairj is offline Member
    Join Date
    Jul 2010
    Posts
    19
    Rep Power
    0

    Default 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.

  7. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,518
    Rep Power
    25

    Default

    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.

  8. #8
    Debonairj is offline Member
    Join Date
    Jul 2010
    Posts
    19
    Rep Power
    0

    Default

    I'm receiving mutilple console.out.println errors...

    Complex.java:49: not a statement
    Console.out.println (" + real + "" + oper + "" + imag + "i"");
    ^

  9. #9
    Debonairj is offline Member
    Join Date
    Jul 2010
    Posts
    19
    Rep Power
    0

    Default

    Also on the project2 file, I'm receiving this...

    Project2.java:3: package complexnumbers does not exist
    import complexnumbers.*

  10. #10
    Debonairj is offline Member
    Join Date
    Jul 2010
    Posts
    19
    Rep Power
    0

    Default

    Also, multiple:

    Project2.java:49: incompatible types
    found : Complex
    required: double
    return temp.add(num1, num2);

  11. #11
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,518
    Rep Power
    25

    Default

    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.

  12. #12
    Debonairj is offline Member
    Join Date
    Jul 2010
    Posts
    19
    Rep Power
    0

    Default

    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...

  13. #13
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,518
    Rep Power
    25

    Default

    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.

  14. #14
    Debonairj is offline Member
    Join Date
    Jul 2010
    Posts
    19
    Rep Power
    0

    Default

    Ok, looking in jgrasp help now

  15. #15
    Debonairj is offline Member
    Join Date
    Jul 2010
    Posts
    19
    Rep Power
    0

    Default

    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.

  16. #16
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,518
    Rep Power
    25

    Default

    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.

  17. #17
    Debonairj is offline Member
    Join Date
    Jul 2010
    Posts
    19
    Rep Power
    0

    Default 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..

  18. #18
    Debonairj is offline Member
    Join Date
    Jul 2010
    Posts
    19
    Rep Power
    0

    Default

    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.

  19. #19
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,518
    Rep Power
    25

    Default

    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

  1. Replies: 0
    Last Post: 07-19-2010, 10:25 PM
  2. Ascii To URL Assistance Needed
    By Sean_J in forum New To Java
    Replies: 2
    Last Post: 03-10-2010, 04:35 AM
  3. Help needed with 'shop' program
    By jkhamler in forum New To Java
    Replies: 1
    Last Post: 12-06-2009, 05:35 PM
  4. Replies: 7
    Last Post: 11-23-2009, 08:42 AM
  5. Need help ASAP with Payroll Program Part 2
    By arrech326 in forum New To Java
    Replies: 10
    Last Post: 11-17-2009, 10:17 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •