Results 1 to 7 of 7
 02182012, 08:59 PM #1Member
 Join Date
 Feb 2012
 Location
 Philadelphia
 Posts
 10
 Rep Power
 0
Newbie QuestionExponents & 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) } }
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; 02182012 at 09:19 PM.
 02182012, 09:21 PM #2
Re: Newbie QuestionExponents & 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.
 02182012, 10:05 PM #3Member
 Join Date
 Feb 2012
 Location
 Philadelphia
 Posts
 10
 Rep Power
 0
Re: Newbie QuestionExponents & 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?

Re: Newbie QuestionExponents & Missing Return Statement
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)
 02192012, 01:01 AM #5Member
 Join Date
 Feb 2012
 Location
 Philadelphia
 Posts
 10
 Rep Power
 0
Re: Newbie QuestionExponents & Missing Return Statement
Thanks very much! Checking it out now.

 02212012, 08:27 PM #7Member
 Join Date
 Feb 2012
 Location
 Philadelphia
 Posts
 10
 Rep Power
 0
Re: Newbie QuestionExponents & 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

Missing return statement
By Aenohe in forum New To JavaReplies: 8Last Post: 03072012, 02:03 PM 
Missing return statement... but there is one
By Eleeist in forum New To JavaReplies: 4Last Post: 02052012, 08:24 PM 
Missing Return statement =[
By avirunes in forum New To JavaReplies: 6Last Post: 02122011, 11:34 AM 
Missing return statement
By gkoef in forum New To JavaReplies: 8Last Post: 01012011, 03:52 AM 
missing return statement
By bayan in forum New To JavaReplies: 6Last Post: 04262010, 03:15 PM
Bookmarks