Results 1 to 3 of 3
  1. #1
    berelson is offline Member
    Join Date
    Nov 2008
    Location
    Boston
    Posts
    2
    Rep Power
    0

    Default 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

    }//class
    Last edited by berelson; 11-29-2008 at 11:26 PM.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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".

  3. #3
    berelson is offline Member
    Join Date
    Nov 2008
    Location
    Boston
    Posts
    2
    Rep Power
    0

    Default

    Thank you so much. That did the trick. I tried everything but your comments were pithy and very helfpul.

    Solution :
    if (N < 100)
    return Hsmall(N);
    else;
    return Hlarge(N);
    Last edited by berelson; 11-29-2008 at 11:58 PM.

Similar Threads

  1. cannot call private method from static method
    By jon80 in forum New To Java
    Replies: 3
    Last Post: 05-07-2008, 08:37 AM
  2. Non-Static method in static context error
    By wizmang in forum New To Java
    Replies: 4
    Last Post: 04-24-2008, 08:51 AM
  3. return a null method
    By valoyivd in forum New To Java
    Replies: 2
    Last Post: 04-21-2008, 11:19 PM
  4. Replies: 1
    Last Post: 08-07-2007, 05:05 AM
  5. Return value of method
    By cachi in forum New To Java
    Replies: 1
    Last Post: 08-01-2007, 08:23 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •