Win a copy of Rust Web Development this week in the Other Languages forum!
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
• Tim Cooke
• Campbell Ritchie
• Ron McLeod
• Liutauras Vilda
• Jeanne Boyarsky
Sheriffs:
• Junilu Lacar
• Rob Spoor
• Paul Clapham
Saloon Keepers:
• Tim Holloway
• Tim Moores
• Jesse Silverman
• Stephan van Hulst
• Carey Brown
Bartenders:
• Al Hobbs
• Piet Souris
• Frits Walraven

# methods and return type

Ranch Hand
Posts: 56
• Number of slices to send:
Optional 'thank-you' note:
need help in identifying the right method and return type in my program.thanks for ever listening hears.

code:

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;
System.out.println(g.Equation(a,b,c));
}
}

Ranch Hand
Posts: 1274
• Number of slices to send:
Optional 'thank-you' note:
Hi all,

you cannot return multiple values. In your case you should return an array of doubles.

Yours,
Bu.

Java Cowboy
Posts: 16084
88
• Number of slices to send:
Optional 'thank-you' note:
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)?

oghenekaro EFEKODO
Ranch Hand
Posts: 56
• Number of slices to send:
Optional 'thank-you' note:
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 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;
System.out.println(g.Equation(a,b,c));
}
}

Ranch Hand
Posts: 55
• Number of slices to send:
Optional 'thank-you' note:
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
Posts: 56
• Number of slices to send:
Optional 'thank-you' note:
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.

author and iconoclast
Posts: 24203
44
• Number of slices to send:
Optional 'thank-you' note:
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 .

oghenekaro EFEKODO
Ranch Hand
Posts: 56
• Number of slices to send:
Optional 'thank-you' note:

so what do i do next.thanks for your ever listening hears

Ernest Friedman-Hill
author and iconoclast
Posts: 24203
44
• Number of slices to send:
Optional 'thank-you' note:
Classpath issues, I imagine. Just start your program with