I'm writing a method for integer power which is something like this 2^4 read in program like 2*2*2*2 without using the math class method. I tried to use several different ways to write, but I think can't figure out where I did wrong. I know I'm close to what it should look like, but something is wrong. Another question I have is I use the if state to specify if the power equal to 0, display 1, but when I enter 0 it display 0. What is the problem?

here is the code for the method

public class Power
{
	public static int integerPower(int ba, int pow)
	{
		int base = ba;
		int power = pow;
		int x= 1;
		
		if (base==1 || power==0 )
			x=1;
		else
		{	
			for(int counter=0; counter < power; counter++ )
			{
				x *=base;

			}
		}
		return x;
	}
}

Here is the application I use to call the method. I think I did it correctly.

import java.util.Scanner;

public class PowerCalculate
{
	public static void main(String[] args)
	{
		Scanner input = new Scanner( System.in );
		
		System.out.print("Please enter the base: ");
		int pow = input.nextInt();
		System.out.print("Please enter the power: ");
		int ba = input.nextInt();
		
		System.out.println(Power.integerPower(ba, pow));
	}
}

Thank you so much

Recommended Answers

All 8 Replies

if (base==1 || power==0 )

what's do you expects if base ==0 ???

you can start with

import java.util.Scanner;

public class Power {

    public static int integerPower(int ba, int pow) {
        int base = ba;
        int power = pow;
        int x = 1;
        if (base > 0) {
            if (power > 0) {
                for (int counter = 0; counter < power; counter++) {
                    x *= base;
                }
                return x;
            } else {
                return 1;
                // some stuff 
            }
        } else {
            return 1;
            // some stuff 
        }
    }

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.print("Please enter the base: ");
        int pow = input.nextInt();
        System.out.print("Please enter the power: ");
        int ba = input.nextInt();
        System.out.println(Power.integerPower(ba, pow));
    }

    private Power() {
    }
}

You have to think what actually determines the value of '1' when the power is 0? In theory, if I remember correctly anything power to 0 is equal to 1 except 0. Therefore, when you go about looking for '1', you need to test for base==0 first, and then power==0.

As mKorbel pointed out in his code, you may need to check for 'power>0'. That's crucial because computing negative power is different (use divide instead of multiple). Not sure that you need to cover this as well.

So...
1)First check is the 'base'. If base is 0, return 0 regardless. Note that you do NOT need the check whether base is positive or negative. It does not matter. (-3^2 is 9 while -3^3 must be -81)
2)Then check if 'power' is 0. If it is 0, return 1 regardless.
3)Compute the power value using absolute value of power (positive) as the loop number. Your way of computation is fine.
3.1 If the power is positive, return x
3.2 if the power is negative, return 1/x instead

NOTE: If you need to deal with negative power value, the return type cannot be 'int' due to the 1/x which is double. I think you could simply change the return type to double for any cases. That's what Math.pow() does.

The power method still wrong except the 0 is fixed and thank you.
The requirement ask me to put it in the different class. That's why I create another class for the main method. Anyway, I run the PowerCalculate class, i type in 2 for the base and 3 for the power. It displays 9 which is way off. Where does my power method do wrong? By the why do you write private Power()? What is that for?

To Taywin
no, I do not need to deal with negative number. But thanks for the future reference and explain further more why mKorbel did it that way. But my power method still wrong. As I say I type 2 for base 3 for the power, but I what i got out is 9.

here is the code:

public class Power
{
	public static int integerPower(int ba, int pow)
	{
		int base = ba;
		int power = pow;
		int x= 1;
		
		if (base > 0 )
		{
			if (power > 0)
			{
				for(int counter=0; counter < power; counter++ )
				{
					x *=base;
				}
				return x;
			}
			else
			{
				return 1;
			}
		}
		else
		{
			return 1;
		}
	}
}

Thank you guys so much

1)Are you sure that anything from 0 and below must return 1?
2)You could assign x=base inside the if(power>0) and start the loop counter from 1 instead of 0.
3)It is odd that you get a 9 from 2^3... I don't see how you could get it from your code? How did you call it?

Well, I kind of make this assignment too complicate. The requirement actually say nonzero and positive integer.

here is the code:

import java.util.Scanner;

public class PowerCalculate
{
	public static void main(String[] args)
	{
		Scanner input = new Scanner( System.in );
		
		System.out.print("Please enter the base: ");
		int pow = input.nextInt();
		System.out.print("Please enter the power: ");
		int ba = input.nextInt();
		
		System.out.println(Power.integerPower(ba, pow));
	}
}

The power method stay the same. About part two, you mean that I could do this if(x=base; power > 0 )?

Try dis


public static int mathPower(int base, int power){

int ba = base;
int pow= power;

int result = 1;

if( pow =0)
return result;

else{
for(int j=1: j <= pow: j++)

result = (ba *= pow);
}
return result;
}

@Dxpat4real
His code works similarly to yours except his does not need to reassign to 'result' over and over again in the for-loop.

@nickliutw
I now understand why you got 9. You actually put in base = 3 and power = 2. So 3^2 is equal to 9! Look closely at how you call your function and user input. You swap them. :)

I can't believe that I make this stupid mistake. Thank you so much. No wonder I couldn't find where I do wrong. I was keep checking my method. Thank you so much again^^

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.