Thursday, August 21, 2014

PowerBase

Power and Base program. Create Class named powerbase which has a method definition ‘public static double power(double base, double power)’ that will return the value equals the base, which is the number n times by the power. Instantiate your class and use them in your main program.

Sample/Input:
Enter two numbers: 2 3
The result is 8.

import javax.swing.JOptionPane;
public class PowerBase {

    public static double Power(double base, double power){
       
        double total = 1.0;
        for(int i = 0;i<power;i++){
            total = base * total;
        }
        return total;
    }
    public static void main(String[] args) {
       
        PowerBase power = new PowerBase();
       
        double result = 0.0;
       
        result = power.Power(2, 3);

        JOptionPane.showMessageDialog(null, result);
    }
}

No comments:

Post a Comment

advertisement