Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 15 of 15

Thread: A little help please~~How should i fix it??

  1. #1
    Junior Member
    Join Date
    Mar 2012
    Location
    Malaysia
    Posts
    9
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default A little help please~~How should i fix it??

    (Thanks~~~...This is first time i found this page and post my problem..so....sorry if i did anything wrong.......This code is changing infix to postfix and then calculate out the result)

    /** The <code>Stack</code> interface specifies the basic operations
    * of a last-in-first-out (LIFO) containers.
    */
    public interface Calculation {

    public void push(Object object);
    /** return the element number in this stack
    */

    public double pop();
    /**Adds the specified element to the top of this stack.
    * @param object the element to be pushed onto this stack.
    */

    public Object peek();
    /**
    * Removes and returns the element at the top of this stack.
    * @return the element at the top of this stack.
    * @throws IllegalStateException if this stack is empty.
    */

    public void resize();
    }
    (Above is my interface...^^^)

    public abstract class ArrayCalculation implements Calculation{
    private Object[] stack;
    private int top;
    private int size;

    public void push(){
    top++;
    stack[top]=top;
    }
    public double pop(){
    Object result = stack[top];
    top--;
    return (Double) result;
    }
    public Object top(){
    return stack[top];
    }
    public boolean isFull(){
    return(top == size);
    }
    public boolean isEmpty(){
    return(top == -1);
    }
    public int size(){
    return(top + 1);
    }

    }

    import java.util.Scanner;

    public class TestArrayCalculation {

    public static void main(String[] args)
    {
    char ch;
    ArrayCalculation s;
    double operan1, operan2, Result;

    System.out.println("The postfix expression is: ");
    Scanner scanner = new Scanner(System.in);
    String e = scanner.nextLine();

    s = new ArrayCalculation(); <<<how should i correct it??
    int i = 0;
    double trueValue;

    while (ch == System.in.read() && ch != '\n') {
    ch = e.charAt(i);
    i++;
    trueValue = ch - 48;
    if (Character.isDigit(ch) ){
    s.push(trueValue);
    }
    else {
    operan2 = s.pop();
    operan1 = s.pop();
    switch (ch) {
    case '+' : s.push(operan1+operan2);
    break;
    case '-' : s.push(operan1-operan2);
    break;
    case '*' : s.push(operan1*operan2);
    break;
    case '/' : s.push(operan1+operan2);
    break;
    }
    }
    }
    trueValue = s.pop();
    System.out.println(" = " + trueValue);
    }
    }


  2. #2
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    939
    Thanks
    6
    Thanked 202 Times in 178 Posts

    Default Re: A little help please~~How should i fix it??

    s = new ArrayCalculation(); <<<how should i correct it??
    What is the compiler message you get on this line? The messages are usually a good place to start - but sometimes they take a little explaining.

  3. #3
    Junior Member
    Join Date
    Mar 2012
    Location
    Malaysia
    Posts
    9
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: A little help please~~How should i fix it??

    [QUOTE=pbrockway2;62745]What is the compiler message you get on this line? The messages are usually a good place to start - but sometimes they take a little explaining.[/QUOTE


    (it appear this error message).....
    " Exception in thread "main" java.lang.Error: Unresolved compilation problem:
    Cannot instantiate the type ArrayCalculation

    at TestArrayCalculation.main(TestArrayCalculation.jav a:15) "

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,552
    Thanks
    45
    Thanked 1,953 Times in 1,927 Posts

    Default Re: A little help please~~How should i fix it??

    Cannot instantiate the type ArrayCalculation
    Is that all of the error message? When I try to compile the code I get an error message with more information that describes the reason the compiler is having problems. Check that you are getting the full text of the error message.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Mar 2012
    Location
    Malaysia
    Posts
    9
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: A little help please~~How should i fix it??

    Ha??i use eclipse software...it show only this error message...
    (sorry for late reply, line very lag at my area for now a day..><...)
    Last edited by Li Yin; April 4th, 2012 at 06:54 AM.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,552
    Thanks
    45
    Thanked 1,953 Times in 1,927 Posts

    Default Re: A little help please~~How should i fix it??

    The javac compiler that I use gives more information. Make sure your IDE is telling you everything that is wrong with the source.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Mar 2012
    Location
    Malaysia
    Posts
    9
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: A little help please~~How should i fix it??

    Quote Originally Posted by Norm View Post
    The javac compiler that I use gives more information. Make sure your IDE is telling you everything that is wrong with the source.


    Thanks ! Can you suggest me any better java's software ?

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,552
    Thanks
    45
    Thanked 1,953 Times in 1,927 Posts

    Default Re: A little help please~~How should i fix it??

    I use the javac command included with the JDK
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Junior Member
    Join Date
    Mar 2012
    Location
    Malaysia
    Posts
    9
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: A little help please~~How should i fix it??

    Quote Originally Posted by Norm View Post
    I use the javac command included with the JDK

    kk...........................thank you very much

  10. #10
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    939
    Thanks
    6
    Thanked 202 Times in 178 Posts

    Default Re: A little help please~~How should i fix it??

    Cannot instantiate the type ArrayCalculation

    at TestArrayCalculation.main(TestArrayCalculation.jav a:15)
    This error seems to arise when you are attempting to run the program, rather than compiling it. Eclipse does give compiler error messages (and underlines things in the code), but I would agree with Norm: use javac to begin with. Apart from anything else you'll get good, extensive, compiler messages that you can cut, copy and post here. The more we have to go on, the better the discussion can be.

    "Cannot instantiate the type ArrayCalculation" means that on line 15 you said "new ArrayCalculation()", but the compiler is complaining because ArrayCalculation is an interface and you can only use "new" with a class of some sort. Check your textbook, notes, etc for examples of how an instance of an interface gets created.

  11. #11
    Junior Member
    Join Date
    Mar 2012
    Location
    Malaysia
    Posts
    9
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: A little help please~~How should i fix it??

    OO!!!!!!! ArrayCalculation is point to an interface and "new" use only in calss!!!!@@ Now then i know and i thought every time when i create only whatever then i should put a "new" there.....==....

    Thanks for your comment.......
    Seem like i need to close this matter and re-coding again~~~

  12. #12
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    939
    Thanks
    6
    Thanked 202 Times in 178 Posts

    Default Re: A little help please~~How should i fix it??

    I haven't looked very closely, but I think all you need to do is replace the interface with the appropriate class. (Again you should have notes giving examples).

    If there are still problems, post the compiler's message.

  13. #13
    Junior Member
    Join Date
    Mar 2012
    Location
    Malaysia
    Posts
    9
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: A little help please~~How should i fix it??

    My notes show only this and i dont have textbook either.......My lecturer just explain according to the notes but dint say how.....T.T

  14. #14
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    939
    Thanks
    6
    Thanked 202 Times in 178 Posts

    Default Re: A little help please~~How should i fix it??

    Sorry - I have just read more closely! (I thought I had remembered from the other day...)

    You have declared ArrayCalculation as a class OK, but you made it "abstract". It should not be abstract because you can't use "new" with abstract classes either. Of course you will have to make sure ArrayCalculation implements all of the methods of the Calculation interface including peek() and resize().

  15. #15
    Junior Member
    Join Date
    Mar 2012
    Location
    Malaysia
    Posts
    9
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: A little help please~~How should i fix it??

    OO....ic.....

    i just settled and get to run it....haha....thanks for your help....^^