Search code examples
javajoptionpanedialogexponent

power function exponent JOptionPane java


I am stuck on an assignment I am very beginning in java and the assignment is having to use the JOptionPane to calculate an exponent. I am totally lost and dont even know where to start. Here is part of the assignment instructions

*****The Power function calculates the power of a base "a" raised to an exponent "n". Write a class which that you will call Power class with a method power() that prints the corresponding power value. Remember to use "float" for a and "long" for n and the returned value to be a "float" because the number is a real number.**

Simplified, Given two numbers n and a where a can have decimals and n can be negative. Create a function to calculate the power.

Design and write a Dialog box for Input/Ouput that allows the users to input a base "a" and an exponent "n" and outputs the result of the power of a raised to the exponent of n.

Example:

power(5.0,2)=(5.0)2= 25.0

power(5.0,-2)=(5.0)-2 = (1/25)=0.04.

Remember the values of n can be postive or negative. Your code should be able to deal with both cases. Remember to limit the value of n to the value 40 maximum. The Assigment will require you to create 2 files:

1- Power.java which contain the details of creating the Power class and the method power which should calculate the power of any number a raised to the exponent n. (Use the Discussion 'Hints on Assign6" for help with writing the code for Power.java as an example). Remember that you:

a- We need two instance variables: float a and int n.

b- We need a constrcutor that has two parameters in order: one a float and one an int.

c- "power()" method that you need to write: one loop for n>0 and another loop for n<0. Note that power() does not have any paramters but returns a float as follows:

float power(){ }***

2- PowerJDialog.java which is modeled after the Dialog boxes developed in the book in chapter2 in pages 99-100 in the code-listing 2-32 (NamesDialog.java) for input and output.

Remember that you will prompt the user to enter:

a base "a" an exponent "n. And out of these 2 values you will be able to create the object "pow" of the "Power" class using the constructor. Then you will use the dialog box to print to the user the result of the power of a raised to the exponent of n.

Remember the values of n can be positive or negative. Your code of Power.java should be able to deal with both cases. Remember to limit the value of n to the value 40 maximum.

You have to have 2 files, the first one that sets the rules and the professor has given a hint:

The example below covers the case of n>0. You need to cover both >0 and <0.

class Power{

float power (){

float pow=1;
for(int i=0;i<n;++i)
pow=pow*x;

return pow;
}

}

Solution

  • Assuming that you professor means that there is the possibility that it will be given a negative exponent, you should make an If statement then two different cases. Case two for negatives will do the same except that in the end it will divide one by the result because x^-1 = 1/x. Also, you need to take input from your method where a is a float and n a long

    public float power(float a, long n) {
            if (n > 0) {
                for (int i = 0; i < n; ++i) {
                    a = a * n;
                }
    
                return a;
            } else if (n < 0) {
                for (int i = 0; i < n; ++i) {
                    a = a * n;
                }
                a = 1 / a;
                return a;
            }else{
                return 0f;
            }
        }