Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

This is a Java Programming question: The power function calculates the power of

ID: 3682703 • Letter: T

Question

This is a Java Programming question:

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. Design and write a Dialog box for Input/Output 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.

This assignment uses the following description to implement a Dialog class for the Power Problem. We need to decompose this problem into 2 classes:

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 need:

two instance variables: float a and int n.( 5 points)

a constructor that has two parameters in order: one a float and one an int.(10 points)

"baseIs()" which returns the base "a". It has no parameters.(5 points)

"exponentIs()" which returns the exponent "n". It has no parameters.(5 points)

"power()" which returns a float. It has no parameters. In the "power()" method you need to write: one loop for n>0 and another loop for n<0. Note that power() does not have any parameters but returns a float as follows: float power(){ }. Use the Discussion 'Hints on Assign6" for help with writing the code for Power.java as an example. Remember that you are using 2 loops in the "power()" method that you need to write: one loop for n>0 and another loop for n<0.(30 points)

PowerJDialog.java" is a public class that represents the GUI. 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 using the inputDialogboxes (10 points):

a base "a"

an exponent "n". Remember to limit the value of n to the value 40 maximum.

And out of these 2 values you will be able to create the object "pow" of the "Power" class using the constructor. (15 points).

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. (20 points).

Example:

Power(5.0,2)=(5.0)2= 25.0
Power(5.0,-2)=(5.0)-2 = 0.04

Do not forget to compile the 2 java files. To verify that the PowerJDialog.java works, in TextPad after you compile PowerJDialog.java, Click on Tools, Click on "Run Java Application".

Explanation / Answer

/********** Power.java ********/

public class Power {

private float base;
private long power;

public Power (float a,long n) {
base = a;
power = n;
}

public float baseIs() {
return base;
}

public long exponentIs() {
return power;
}

// loop n<0 and n>0
float power() {
float pow=1;
if (power >= 0) {
for(int i=0;i<power;++i) {
pow=pow*base;
}
}
else {
for(int i=0;i<power*(-1);++i) {

pow=pow*base;
}
pow=1/pow;
}
return pow;
}
}

/********* PowerJDialog.java ***********/

import javax.swing.JOptionPane;

public class PowerJDialog
{
public static void main (String[] args)
{
String input;
float base;
long power1;

//Prompt user for base number
input = JOptionPane.showInputDialog("Please enter the base number: ");
base = Float.parseFloat(input);

//Prompt user for power
input = JOptionPane.showInputDialog("Please enter the exponent, Maximum value is 40: ");
power1 = Long.parseLong(input);

//Display results

Power d = new Power(base, power1);

JOptionPane.showMessageDialog(null, "The power value of " + base + " raised to the power of " + power1 + " is " + d.power() + ".");

//Exits the program
System.exit(0);
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote