Please help! Problem with run time error.
Author |
Message |
tigerhawk33
|
Posted: Fri Oct 12, 2007 4:54 pm Post subject: Please help! Problem with run time error. |
|
|
Hello everyone. A very good friend of mine recommended this site to me. I have a problem with a program that I have to get working, but I'm at a loss now and I really need help. I've checked all the possible searches here at this website, but my problem seems difficult.
Ok, here is the program that I'm charged with:
code: |
public class TestError
{
public static void main(String[]args)
{
System.out.println("The max between 3.0 & 4 is " + max(3,4));
System.out.println("The max between 3.0 & 5.4 is " + max(3.0,5.4));
System.out.println("The max between 3.0,5.4, and 10.14 is " + max(3.0,5.4,10.14);
System.out.println("The result of 4.0 divided by 3.0 is " +divide(3,0));
}
static int max(int num1, int num2)
{
if (num1>num2) return num1;
else return num2;
}
static double max(double num1, double num2)
{
if (num1>num2) return num1;
else return num2;
}
static double max(double num1, double num2, double num3);
{
return max(max(num1, num2), num3);
}
static double divide(double num1, double num2)
{
double result;
result = num1/num2;
return result;
}
}//end class
|
Now, I understand the problem, because the printout will show infinity due to the value of 3 and 0 being used in the beginning of the program. Thus a run-time error. But I'm told that to correct the run time error, when the denominator is zero, I have to output an error message indicating the denominator is 0 and to bypass the calculation.
So I guess I have to leave in the calculation, but maybe I should use an if/else statement here? I don't think I'm allowed to delete the [System.out.println("The result of 4.0 divided by 3.0 is " + divide(3,0));] line, because the way I read my assignment is that I can somehow replace this statement with a new error message statement.
Please help me my new friends. (_ _)
ps. I am told the God 'wtd' is the one to talk to about Java... but anyone's help would be greatly appreciated please. Thank you!
edit: Oh, and I'm not allowed to remove the 3 and 0 because I have to leave them in. I'm somehow supposed to work around this and print out "Error detected". Thanks guys. |
|
|
|
|
 |
Sponsor Sponsor

|
|
 |
rdrake

|
Posted: Fri Oct 12, 2007 5:41 pm Post subject: Re: Please help! Problem with run time error. |
|
|
Java: | if(num2 == 0) {
System. out. println("Cannot divide by zero.");
return 0;
} | Er... do a check before attempting to divide like above? |
|
|
|
|
 |
tigerhawk33
|
Posted: Fri Oct 12, 2007 5:50 pm Post subject: Re: Please help! Problem with run time error. |
|
|
Oh thank you very much!!! It seems to have worked. It still prints out the "The result of 4 divided by 3 is 0.0" line, but it's better than nothing.
My teacher said there were many ways of discovering how to manipulate Java, but I guess I'm having some difficulty in understanding the program.
Thank you my friend~! (^__^)/
Tigerhawk33  |
|
|
|
|
 |
richcash
|
Posted: Fri Oct 12, 2007 7:27 pm Post subject: Re: Please help! Problem with run time error. |
|
|
You can always use the Object type.
Java: | static Object divide (double num1, double num2 ) {
Object result;
if (num2 == 0) {
result = "undefined, attempting to divide by zero.";
}
else {
result = num1 / num2;
}
return result;
} |
But if you use that you'll have to research the Object type.  |
|
|
|
|
 |
tigerhawk33
|
Posted: Fri Oct 12, 2007 7:44 pm Post subject: Re: Please help! Problem with run time error. |
|
|
I tried it but it didn't work. But thank you sooooooooooo much for the input. You know, the more I practice and study Java, the more fun it begins to learn it. Thanks man!
 |
|
|
|
|
 |
Euphoracle

|
Posted: Fri Oct 12, 2007 7:54 pm Post subject: Re: Please help! Problem with run time error. |
|
|
richcash @ Fri Oct 12, 2007 7:27 pm wrote: But if you use that you'll have to research the Object type. 
The object is the basis of (most if not) all classes in java. Anything that stems from it, you can cast to it and it will retain it's values. |
|
|
|
|
 |
tigerhawk33
|
Posted: Fri Oct 12, 2007 8:13 pm Post subject: Re: Please help! Problem with run time error. |
|
|
Thanks bro~! Yeah, the Object stuff that I'm reading now makes sense. Everything seems to stem from it. Thanks man, you pointed me in the right direction.
Even though I'm a newb, it's still alot of fun learning about Java. Right now, I'm looking what my teacher calls the "Bible" of Java... I googled a page called Java 1.5 API. I'm just reading it. It's a lot to take in right now.
Again, thanks to everyone who replied or viewed it. I'm really sorry to have bothered anyone with these problems. Just its difficult to get things rolling, but I'm starting to see some patterns and familiar looking things.
I'm still at the college (man it's late...). It's late so I think I'll pack it up for tonight. I'll try again Saturday afternoon.
Ciao and goodnight bros.~!
 |
|
|
|
|
 |
|
|