# Thread: Newbie Question-Exponents & Missing Return Statement

1. Member Join Date
Feb 2012
Location
Posts
10
Rep Power
0

## 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.  Reply With Quote

2. ## 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.  Reply With Quote

3. Member Join Date
Feb 2012
Location
Posts
10
Rep Power
0

## 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?  Reply With Quote

4. ## Re: Newbie Question-Exponents & Missing Return Statement Originally Posted by dgoff267 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)  Reply With Quote

5. Member Join Date
Feb 2012
Location
Posts
10
Rep Power
0

## Re: Newbie Question-Exponents & Missing Return Statement

Thanks very much! Checking it out now.  Reply With Quote

6. ## Re: Newbie Question-Exponents & Missing Return Statement Originally Posted by dgoff267 Thanks very much! Checking it out now.
You're welcome! Please let us know one way or the other how things work out.  Reply With Quote

7. Member Join Date
Feb 2012
Location
Posts
10
Rep Power
0

## 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);
}
}  Reply With Quote

exponent, missing, return, statement 