Results 1 to 8 of 8
- 04-16-2011, 10:00 PM #1
Member
- 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.
- 04-16-2011, 10: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.
- 04-16-2011, 10:41 PM #3
Member
- 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 Z-slasher; 04-16-2011 at 10:48 PM.
-
- 04-17-2011, 12:09 AM #5
Member
- 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 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?
-
For the trig calculations: Are your numbers in radians or degrees?
- 04-17-2011, 12:34 AM #7
Member
- 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 Z-slasher; 04-17-2011 at 01:02 AM.
- 04-17-2011, 11:05 PM #8
Member
- 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: 03-26-2011, 10:25 AM -
Help me please (Java assignment)
By cris_carriaga in forum Java AppletsReplies: 4Last Post: 10-06-2010, 04:11 PM -
Java assignment
By xtianah77 in forum New To JavaReplies: 1Last Post: 02-17-2008, 11:54 PM -
java assignment, need help bad.
By carlos123 in forum New To JavaReplies: 1Last Post: 11-06-2007, 04:53 PM -
Help with my assignment java
By toby in forum New To JavaReplies: 1Last Post: 08-07-2007, 05:59 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks