Results 1 to 7 of 7
  1. #1
    dgoff267 is offline Member
    Join Date
    Feb 2012
    Location
    Philadelphia
    Posts
    10
    Rep Power
    0

    Default Newbie Question-Exponents & Missing Return Statement

    Hey,

    I'm just learning Java, and I've been assigned to create a program that multiplies bases and exponents based on what the user inputs. I'm not sure if I've formatted the code correctly to provide the right output, and I keep getting a "missing return statement" on line 33. Can someone please take a look and tell me what you think?
    Java Code:
    import java.util.Scanner;
    
    public class MyMath
    {
     public static double myPow(double base, int exponent)
     {
      double base;
      int exponent;
      double total;
      Scanner kb = new Scanner(System.in);
    
      base = 0;
      exponent = 0;
      System.out.println("What is the base?");
      base = kb.nextDouble();
      System.out.println("What is the exponent?");
      exponent = kb.nextInt();
    
       if (base == 0.0 && exponent == 0)
       {
        System.out.println("Error: Raising 0.0 to the 0 power is undefined.");
        System.exit(0);
       }
       if (base != 0.0 && exponent == 0)
       {
        System.out.println("1.0");
        System.exit(0);
       }
       if(base != 0.0 && exponent >  0);
       {
        for (base = 0; base >= 0; base++)
       {
        total = Math.pow(base, exponent);
        System.out.println("The total is " +total)
     }
    }
    -Create a public class named MyMath.

    -Write public static double function named myPow(double base, int exponent)

    -pow should return the following:

    -If base == 0.0 and exponent = 0, print an error message saying that
    raising 0.0 to the 0 power is nod defined and halt the program using
    System.exit

    -Otherwise, if base is not 0.0 and exponent is 0, return the value 1.0

    -Othersiwe, if base is not 0.0 and exponent is > 0, multiply base by
    itself exonent times (use a for loop) and return the product.

    -Otherwise (base is not 0.0 and exonent < 0, replace base with 1/base and
    then multiply it by itself exponent times.

    -Thus, if base is 3.0 and exponent is 4, your function would calculate
    3.0 * 3.0 * 3.0 * 3.0 and return 81.0

    -On the other hand, if base is 2.0 and exponent is 3, you would caluclate
    0.5 * 0.5 * 0.5 and return 0.125

    -Write a main program to test this function.
    Last edited by Norm; 02-18-2012 at 08:19 PM.

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,271
    Rep Power
    25

    Default Re: Newbie Question-Exponents & Missing Return Statement

    Your definition of the method says it will return a double value:
    public static double myPow(

    All exits from the program must return a double value.
    Where do you use the return statement to return a double?
    Minimally there should be one at the end of the method.

  3. #3
    dgoff267 is offline Member
    Join Date
    Feb 2012
    Location
    Philadelphia
    Posts
    10
    Rep Power
    0

    Default Re: Newbie Question-Exponents & Missing Return Statement

    Do you mean that in "total = Math.pow(base, exponent);", I need to make sure that it's returning a double value? If so, how can I do that?

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

    Default Re: Newbie Question-Exponents & Missing Return Statement

    Quote Originally Posted by dgoff267 View Post
    Do you mean that in "total = Math.pow(base, exponent);", I need to make sure that it's returning a double value? If so, how can I do that?
    No, he means your myPow method itself. Please read this tutorial for more on returning values from a method: Returning a Value from a Method (The Java™ Tutorials > Learning the Java Language > Classes and Objects)

  5. #5
    dgoff267 is offline Member
    Join Date
    Feb 2012
    Location
    Philadelphia
    Posts
    10
    Rep Power
    0

    Default Re: Newbie Question-Exponents & Missing Return Statement

    Thanks very much! Checking it out now.

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

    Default Re: Newbie Question-Exponents & Missing Return Statement

    Quote Originally Posted by dgoff267 View Post
    Thanks very much! Checking it out now.
    You're welcome! Please let us know one way or the other how things work out.

  7. #7
    dgoff267 is offline Member
    Join Date
    Feb 2012
    Location
    Philadelphia
    Posts
    10
    Rep Power
    0

    Default Re: Newbie Question-Exponents & Missing Return Statement

    Thanks for your help guys! I'm not sure if it was the best way to format it, but here's the code I ended up with(and it worked).

    import java.util.Scanner;

    public class Lab3 {
    public static void main (String args[]){
    Scanner kb = new Scanner(System.in);
    double base;
    int exponent;
    double total = 0;

    base = 0;
    exponent = 0;
    System.out.println("What is the base?");
    base = kb.nextDouble();
    System.out.println("What is the exponent?");
    exponent = kb.nextInt();

    if (base == 0.0 && exponent == 0){
    System.out.println("Error: Raising 0.0 to the 0 power is undefined.");
    System.exit(0);
    }
    if (base != 0.0 && exponent == 0){
    System.out.println("1.0");
    System.exit(0);
    }
    else if (base != 0.0 && exponent > 0)
    {
    total = Math.pow(base, exponent);
    }
    System.out.println("The total is "+ total);
    }
    }

Similar Threads

  1. Missing return statement
    By Aenohe in forum New To Java
    Replies: 8
    Last Post: 03-07-2012, 01:03 PM
  2. Missing return statement... but there is one
    By Eleeist in forum New To Java
    Replies: 4
    Last Post: 02-05-2012, 07:24 PM
  3. Missing Return statement =[
    By avirunes in forum New To Java
    Replies: 6
    Last Post: 02-12-2011, 10:34 AM
  4. Missing return statement
    By gkoef in forum New To Java
    Replies: 8
    Last Post: 01-01-2011, 02:52 AM
  5. missing return statement
    By bayan in forum New To Java
    Replies: 6
    Last Post: 04-26-2010, 03:15 PM

Tags for this Thread

Posting Permissions

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