Results 1 to 5 of 5
  1. #1
    ramathews is offline Member
    Join Date
    Feb 2010
    Posts
    6
    Rep Power
    0

    Default [SOLVED] Trouble working with/calling Objects! Any Help?

    Hi,
    I finished the code for an assignment and am having some trouble making the code compile. It is giving me all kinds of cannot find symbol errors. In what may very well be related, when I pop the top value of my stack, it tells me that the package theStack does not exist.

    Also, in my switch statement, any ideas on how I can make the second case run for any of those three operators without copying the same code 3 times?

    Thanks in advance for your time and help. If you have any suggestions on style or anything like that, I am very open to tips!

    Andy

    My code:
    Java Code:
    package Project2;
    
    import Project2.*;
    import java.util.*;
    import java.io.*;
    
    public class Project2 {
      public static void main (String[] args){
         
        String charConv, more = "null";
        Stack theStack = new Stack();
        Scanner keyboard = new Scanner(System.in);
            
        do{
      //allow user to enter and evaluate any number of expressions
      System.out.println("Enter a postfix expression of complex numbers (Ex: (2 + 3i)(1 + 10i)+):");
      StringTokenizer input = new StringTokenizer(keyboard.nextLine());  
        
      while (input.hasMoreTokens())
        charConv = input.nextToken();
        char nextChar = charConv.charAt(0);
        switch (nextChar){
        case '(':
          StringTokenizer expression = new StringTokenizer(input.nextToken(")"));
          Complex newComplex = new fromString(expression);
          theStack.push(newComplex); //push the value onto the stack
          break;
          case ('+'||'-'||'*'):
            //pop two operands from the stack
            Complex num1 = new theStack.pop();
            Complex num2 = new theStack.pop();
            //evaluate
            Complex result = new evaluate(num1, num2, nextChar);
            theStack.push(result);
            break;
        }
      //pop the final result off the stack
        Complex finalResult = new theStack.pop();
        System.out.println("The result of your input is:" + finalResult.toString());
        System.out.println("Evaluate another expression? (Y = Yes):");
        more = keyboard.next();
        } while (more.equalsIgnoreCase("y"));
        }
    
      static public Complex evaluate(Complex num1, Complex num2, char operator){
        //evaluate operation
        Complex result = new Complex();
        if (operator == '+'){
          result = add(num1, num2);
        }
        else if (operator == '-'){
          result = subtract(num1, num2);
        }
        else {
          Complex prod = new multiply(num1, num2);
        }
        return result;
      }
    }
    My worker bee class:
    Java Code:
    package Project2;
    
    import Project2.*;
    import java.util.*;
    import java.io.*;
    
    public class Complex{
      private double real, imag;
      private String oper;
        
      public Complex(double real, double imag, String oper){
        this.real = real;
        this.imag = imag;
        this.oper = oper;
      }
          
      public static Complex fromString(StringTokenizer tokenizer){
        double real, imag;
        String oper;
        real = Double.parseDouble(tokenizer.nextToken());
        oper = tokenizer.nextToken();
        imag = Double.parseDouble(tokenizer.nextToken());
        return new Complex(real, imag, oper);
      }
      
      public String toString(){
        return "(" + real + " " + oper + " " + imag + "i" + ")";
      }
      
      public Complex add(Complex num1, Complex num2){
        num1.real = num1.real + num2.real;
        num1.imag = num1.imag + num2.imag;
        return num1; 
      }
      
      public Complex subtract(Complex num1, Complex num2){
        num1.real = num1.real - num2.real;
        num1.imag = num1.imag - num2.imag;
        return num1;
      }
      
      public Complex multiply(Complex num1, Complex num2){
        int x1 = (int)num1.real;
        int y1 = (int)num1.imag;
        int x2 = (int)num2.real;
        int y2 = (int)num2.imag;
        int intTemp, iTemp = 0;
        
        intTemp = x1 * x2;
        x1 = x1 * y2;
        iTemp = y1 * x2;
        y1 = y1 * y2;
        y1 = y1 * (-1);
        
        x2 = intTemp + y1;
        y2 = x1 + iTemp;
        
        num1.real = x2;
        num1.imag = y2;
        
        return num1;
      }
    }
    Last edited by ramathews; 03-24-2010 at 02:51 PM. Reason: Solved

  2. #2
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    Does your Complex class compile on it's own first?
    If not then post the errors you get on it.

    If it does then post the compilation errors you get on your Project2 class.

  3. #3
    ramathews is offline Member
    Join Date
    Feb 2010
    Posts
    6
    Rep Power
    0

    Default Posting Errors

    The Complex class compiles no problem, which surprised me actually.

    Here are the errors for the Project 2 class:
    File: D:\My Documents\School\CMSC 230\Project2\Project2.java [line: 31]
    Error: D:\My Documents\School\CMSC 230\Project2\Project2.java:31: cannot find symbol
    symbol : class fromString
    location: class Project2.Project2
    File: D:\My Documents\School\CMSC 230\Project2\Project2.java [line: 34]
    Error: D:\My Documents\School\CMSC 230\Project2\Project2.java:34: operator || cannot be applied to char,char
    File: D:\My Documents\School\CMSC 230\Project2\Project2.java [line: 36]
    Error: D:\My Documents\School\CMSC 230\Project2\Project2.java:36: package theStack does not exist
    File: D:\My Documents\School\CMSC 230\Project2\Project2.java [line: 37]
    Error: D:\My Documents\School\CMSC 230\Project2\Project2.java:37: package theStack does not exist
    File: D:\My Documents\School\CMSC 230\Project2\Project2.java [line: 39]
    Error: D:\My Documents\School\CMSC 230\Project2\Project2.java:39: cannot find symbol
    symbol : class evaluate
    location: class Project2.Project2
    File: D:\My Documents\School\CMSC 230\Project2\Project2.java [line: 44]
    Error: D:\My Documents\School\CMSC 230\Project2\Project2.java:44: package theStack does not exist
    File: D:\My Documents\School\CMSC 230\Project2\Project2.java [line: 53]
    Error: D:\My Documents\School\CMSC 230\Project2\Project2.java:53: cannot find symbol
    symbol : constructor Complex()
    location: class Project2.Complex
    File: D:\My Documents\School\CMSC 230\Project2\Project2.java [line: 55]
    Error: D:\My Documents\School\CMSC 230\Project2\Project2.java:55: cannot find symbol
    symbol : method add(Project2.Complex,Project2.Complex)
    location: class Project2.Project2
    File: D:\My Documents\School\CMSC 230\Project2\Project2.java [line: 58]
    Error: D:\My Documents\School\CMSC 230\Project2\Project2.java:58: cannot find symbol
    symbol : method subtract(Project2.Complex,Project2.Complex)
    location: class Project2.Project2
    File: D:\My Documents\School\CMSC 230\Project2\Project2.java [line: 61]
    Error: D:\My Documents\School\CMSC 230\Project2\Project2.java:61: cannot find symbol
    symbol : class multiply
    location: class Project2.Project2

  4. #4
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    First change your class name so that it is not the same as your package name (You haven't run out of appropriate names yet have you?).
    Then change line 31 where you did the
    Java Code:
    new fromString(expression)
    thing. If you want to call a static method you do ClassName.methodName e.g Complex.fromString not new fromString(expression). You use new when creating a new object by calling a constructor not a normal java method.

    After that you look at the next line number reported(34), read the error message and try to understand what the compiler is trying to tell you. Post again if you still have problems but always make an effort to try and solve the problem on your own from the hints that the compiler gives you.

  5. #5
    ramathews is offline Member
    Join Date
    Feb 2010
    Posts
    6
    Rep Power
    0

    Default Like a Champ!

    Thank you very much for your help. I figured out the way with the cases. I keep all of my textbooks and since they all spend an inordinate amount of time on one thing or another, I went to the one spent a ridiculous amount of time on switch statements. Your help in the calling methods cleared up an awful lot for me. Thanks again!
    Andy

Similar Threads

  1. Here comes trouble... :-)
    By sargehendricks in forum Introductions
    Replies: 1
    Last Post: 04-23-2009, 03:18 PM
  2. Replies: 1
    Last Post: 01-22-2009, 04:25 PM
  3. Working with Vector objects + textfile
    By SGRocker in forum New To Java
    Replies: 5
    Last Post: 09-16-2008, 10:55 PM
  4. Trouble will calling a method
    By jonsamwell in forum New To Java
    Replies: 9
    Last Post: 08-22-2008, 10:16 PM
  5. Replies: 8
    Last Post: 05-28-2008, 07:00 AM

Posting Permissions

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