Results 1 to 3 of 3
- 11-29-2008, 10:03 PM #1
Member
- Join Date
- Nov 2008
- Location
- Boston
- Posts
- 2
- Rep Power
- 0
Static Method and Return Statements
Having basic problem with a program that is supposed to call static methods from main as part of homework assignment.
1 compile error: >javac Harmonic.java
Harmonic.java:24: missing return statement
on line: return Hlarge(N);
Seems to be having trouble with my if else or how i'm handling the returns of these static methods.
Thanks in advance for any help. Perhaps an easy fix for an intermediate developer.
--------------------------------------
public class Harmonic {
public static double Hsmall(int N) {
// Base case: H(1) = 1.
if(N <= 1) return 1;
// Every other case: H(n) = 1/n+H(n-1).
return Hsmall(N-1)+1.0/N;
}
public static double Hlarge(int N) {
return Math.exp(N); //library for Euler's constant
}
public static double H(int N) {
if (N < 100)
return Hsmall(N);
else if (N >= 100)
return Hlarge(N); // <-- compile error here
}
public static void main (String[] args){
//int N = Double.parseDouble(args[0]); //evaluate number
int N = Integer.parseInt(args[0]);
System.out.println(H(N));
}// main
}//classLast edited by berelson; 11-29-2008 at 11:26 PM.
-
Your problem may be coming from the if / else if blocks (next time, please indicate with a code comment which line is causing the error). In this situation, the compiler doesn't know if there are more possibilities that you haven't accounted for,... if the if block is false and the else if is false, then what? Even if your else if covers all responsibilities, the compiler is not smart enough to figure this out. In that situation, you shouldn't be using else if, but should instead be using only "else".
- 11-29-2008, 11:17 PM #3
Member
- Join Date
- Nov 2008
- Location
- Boston
- Posts
- 2
- Rep Power
- 0
Similar Threads
-
cannot call private method from static method
By jon80 in forum New To JavaReplies: 3Last Post: 05-07-2008, 08:37 AM -
Non-Static method in static context error
By wizmang in forum New To JavaReplies: 4Last Post: 04-24-2008, 08:51 AM -
return a null method
By valoyivd in forum New To JavaReplies: 2Last Post: 04-21-2008, 11:19 PM -
Error: Non-static method append(char) cannot be referenced from a static context
By paul in forum Advanced JavaReplies: 1Last Post: 08-07-2007, 05:05 AM -
Return value of method
By cachi in forum New To JavaReplies: 1Last Post: 08-01-2007, 08:23 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks