Results 1 to 8 of 8
 04162011, 11:00 PM #1Member
 Join Date
 Apr 2011
 Posts
 7
 Rep Power
 0
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.
 04162011, 11:34 PM #2
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.
 04162011, 11:41 PM #3Member
 Join Date
 Apr 2011
 Posts
 7
 Rep Power
 0
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 Zslasher; 04162011 at 11:48 PM.

 04172011, 01:09 AM #5Member
 Join Date
 Apr 2011
 Posts
 7
 Rep Power
 0
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 240368)
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?

For the trig calculations: Are your numbers in radians or degrees?
 04172011, 01:34 AM #7Member
 Join Date
 Apr 2011
 Posts
 7
 Rep Power
 0
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 Zslasher; 04172011 at 02:02 AM.
 04182011, 12:05 AM #8Member
 Join Date
 Apr 2011
 Posts
 7
 Rep Power
 0
Similar Threads

Need Help with Java Assignment
By smurf67 in forum New To JavaReplies: 4Last Post: 03262011, 11:25 AM 
Help me please (Java assignment)
By cris_carriaga in forum Java AppletsReplies: 4Last Post: 10062010, 05:11 PM 
Java assignment
By xtianah77 in forum New To JavaReplies: 1Last Post: 02182008, 12:54 AM 
java assignment, need help bad.
By carlos123 in forum New To JavaReplies: 1Last Post: 11062007, 05:53 PM 
Help with my assignment java
By toby in forum New To JavaReplies: 1Last Post: 08072007, 06:59 AM
Bookmarks