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?

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.

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.

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?

Re: Newbie Question-Exponents & Missing Return Statement

Quote:

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)

Re: Newbie Question-Exponents & Missing Return Statement

Thanks very much! Checking it out now.

Re: Newbie Question-Exponents & Missing Return Statement

Quote:

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.

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);

}

}