wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes methods and return type Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "methods and return type" Watch "methods and return type" New topic
Author

methods and return type

oghenekaro EFEKODO
Ranch Hand

Joined: Oct 10, 2005
Posts: 56
need help in identifying the right method and return type in my program.thanks for ever listening hears.

code:

public class QuadraticEquation{
public static double Equation(int a,int b,int c){
double D,x1,x1;
//int a,b,c;
D=(Math.pow(b,2)-4*a*c);
if(D=0){
x1=(-b-Math.sqrt(D))/(2*a);
x2=(-b+Math.sqrt(D))/(2*a);
return(x1,x2);
}
}
public static void main(String arg[]){
int a=1;
int b=-3;
int c=2;
QuadraticEquation g=new QuadraticEquation();
System.out.println(g.Equation(a,b,c));
}
}
Burkhard Hassel
Ranch Hand

Joined: Aug 25, 2006
Posts: 1274
Hi all,

you cannot return multiple values. In your case you should return an array of doubles.
Your method could be like:




Yours,
Bu.


all events occur in real time
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 13869
    
  10

There are some more bugs in your code. For example:

if(D=0){

This is not going to do what you want. It assigns 0 to D. To compare values, use == instead of =.

Quadratic equations do not always have two solutions. What are you going to return if the equation does not have any (real) solutions (when D < 0)?


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
oghenekaro EFEKODO
Ranch Hand

Joined: Oct 10, 2005
Posts: 56
i corrected my mistake and add the array method,compiled with no error,but the problem now is that the program just keep runnin with no result.please what do i do next to correct this problem.
the cod again is

public class QuadraticEquation{
public static double [] Equation(int a,int b,int c){
double [] array=new double[2];
double D,x1,x2;
//int a,b,c;
D=(Math.pow(b,2)-4*a*c);
for(int i=0;1<array.length;i++){
if(D==0){
x1=(-b-Math.sqrt(D))/(2*a);
x2=(-b+Math.sqrt(D))/(2*a);
}
}
return array;
}
public static void main(String arg[]){
int a=1;
int b=-3;
int c=2;
QuadraticEquation g=new QuadraticEquation();
System.out.println(g.Equation(a,b,c));
}
}
fred Joly
Ranch Hand

Joined: Jan 19, 2006
Posts: 55
for(int i=0;1<array.length;i++)

have a closer look at this line. Should not be to
difficult to spot the mistake
oghenekaro EFEKODO
Ranch Hand

Joined: Oct 10, 2005
Posts: 56
please couldn't locate the problem in

for(int i=0;i<=array.length;i++)


i tried but my best wasn't good enough.please somebody help me,am so shy for not finding what the problem really is.thanks again for your ever listening hears.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24166
    
  30

An array "array" has array.length elements in it. The first one is array[0]; the last one is array[array.length - 1]. If you try to access array[array.length], that's off the end of the array and you'll get an ArrayIndexOutOfBoundsException .


[Jess in Action][AskingGoodQuestions]
oghenekaro EFEKODO
Ranch Hand

Joined: Oct 10, 2005
Posts: 56
i added array[array.length-1] and gave an Exception in thread "main" java.lang.NoClassDefFoundError:QuadraticEquation.

so what do i do next.thanks for your ever listening hears
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24166
    
  30

Classpath issues, I imagine. Just start your program with

java -cp . QuadraticEquation

That's "java space dash cp space dot space QuadraticEquation".

I'm afraid, based on this last question, that this really needs to be moved to Java in General (Beginner) -- so off it goes.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: methods and return type
 
Similar Threads
Quadratic perplextion
Understanding superclass and subclass objects
Intersection points not being determined accurately- java awt
equation problem
Calculating the distance between two points problem