Results 1 to 8 of 8
  1. #1
    Z-slasher is offline Member
    Join Date
    Apr 2011
    Posts
    7
    Rep Power
    0

    Default need help with a java assignment.

    For my java assignment I am supposed to modify a code to add other functions to it. I need to add sin, cos, tan, and x^y buttons on the calculator and make them work.

    The problem is I get this error. (I know I probably got ^ wrong):
    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 23
    at Calculator.<init>(Calculator.java:124)
    at Calculator.main(Calculator.java:586)

    original code:
    #1798262 - Pastie

    where I am at so far:
    #1799334 - Pastie

    As I'm still a beginner java programmer I probably got other things wrong in the code, so any help that I can get I will appreciate.

  2. #2
    Dark's Avatar
    Dark is offline Senior Member
    Join Date
    Apr 2011
    Location
    Camp Lejuene, North Carolina
    Posts
    643
    Rep Power
    4

    Default

    You're getting an index out of range error. You're trying to call a array slot that doesn't exist.

    Your program is hundreds of lines long so I'm not going to go through it all to find where, but you should investigate your arrays. I have a feeling its with your jbnButtons array.
    • Use [code][/code] tags when posting code. That way people don't want to stab their eyes out when trying to help you.
    • +Rep people for helpful posts.

  3. #3
    Z-slasher is offline Member
    Join Date
    Apr 2011
    Posts
    7
    Rep Power
    0

    Default

    Sweet. fixed that! it was on line 97. thanks for showing that one.

    now I see sin, cos, tan, and x^y isn't functioning the way they should.

    any where I should put that on the code?
    Last edited by Z-slasher; 04-16-2011 at 11:48 PM.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by Z-slasher View Post
    now I see sin, cos, tan, and x^y isn't functioning the way they should.

    any where I should put that on the code?

    Please re-read this comment:
    Quote Originally Posted by Dark View Post
    Your program is hundreds of lines long so I'm not going to go through it all to find where, ....

    Consider creating and posting an SSCCE to help us be better able to help you. You can find out what this entails by checking my link on this subject below.

  5. #5
    Z-slasher is offline Member
    Join Date
    Apr 2011
    Posts
    7
    Rep Power
    0

    Default

    my apologies. let me break it down to the problem. There are 2 areas I'm not so sure about I'll start with the first switch statement on the program. I'll start with case 20 to 23 because that is the ones to look at.

    (Find this one on line 240-368)

    for (int i=0; i<jbnButtons.length; i++)
    {
    if(e.getSource() == jbnButtons[i])
    {
    switch(i)
    {



    case 20: // sin
    processOperator("sin");
    break;

    case 21: // cos
    processOperator("cos");
    break;

    case 22: // sin
    processOperator("tan");
    break;

    case 23: // x^y
    processOperator("^");
    break;



    and I modified the if statement.

    (this is found on line 538 to 568 in the pastie)

    double processLastOperator() throws DivideByZeroException {
    double result = 0;
    double numberInDisplay = getNumberInDisplay();

    if (lastOperator.equals("/"))
    {
    if (numberInDisplay == 0)
    throw (new DivideByZeroException());

    result = lastNumber / numberInDisplay;
    }

    if (lastOperator.equals("*"))
    result = lastNumber * numberInDisplay;

    if (lastOperator.equals("-"))
    result = lastNumber - numberInDisplay;

    if (lastOperator.equals("+"))
    result = lastNumber + numberInDisplay;

    if (lastOperator.equals("sin"))
    result = Math.sin(numberInDisplay);

    if (lastOperator.equals("cos"))
    result = Math.cos(numberInDisplay);

    if (lastOperator.equals("tan"))
    result = Math.tan(numberInDisplay);

    return result;

    These are the codes where I find the +,-,* and divide operators function. What could I be doing wrong for sin, cos, tan, and x^y?

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

  7. #7
    Z-slasher is offline Member
    Join Date
    Apr 2011
    Posts
    7
    Rep Power
    0

    Default

    It is radians. I actually figured this one out by looking at sqrt since that uses the math logic. I'll paste the what I did in C indentation so it is easy to look at.

    Changing the switch statements to this gave me the correct number in radians.

    here is the code for case 20 to 22:

    {
    result = Math.sin(getNumberInDisplay());
    displayResult(result);
    }

    so I did that for cos, and tan as well.

    now to figure out x^y. how do I do this one using the Math.pow logic?

    edit:
    {
    result = Math.pow(getNumberInDisplay(), getNumberInDisplay());
    displayResult(result);
    }

    This gives x^x without the = sign input. I need x^y.
    Last edited by Z-slasher; 04-17-2011 at 02:02 AM.

  8. #8
    Z-slasher is offline Member
    Join Date
    Apr 2011
    Posts
    7
    Rep Power
    0

    Default

    I figured it out. :) thanks for the help guys. I guess this is pretty simple even for someone who isn't really a java programmer.
    Last edited by Z-slasher; 04-18-2011 at 12:07 AM.

Similar Threads

  1. Need Help with Java Assignment
    By smurf67 in forum New To Java
    Replies: 4
    Last Post: 03-26-2011, 11:25 AM
  2. Help me please (Java assignment)
    By cris_carriaga in forum Java Applets
    Replies: 4
    Last Post: 10-06-2010, 05:11 PM
  3. Java assignment
    By xtianah77 in forum New To Java
    Replies: 1
    Last Post: 02-18-2008, 12:54 AM
  4. java assignment, need help bad.
    By carlos123 in forum New To Java
    Replies: 1
    Last Post: 11-06-2007, 05:53 PM
  5. Help with my assignment java
    By toby in forum New To Java
    Replies: 1
    Last Post: 08-07-2007, 06:59 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
  •