This assignment uses the following description to implement a Dialog class for t
ID: 3682891 • Letter: T
Question
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
Explanation / Answer
//power.java
public class Power
{
Float Power (float a, long n)
{
float pow = 1;
for (int i=0;i<n;++i)
pow = pow * a;
for (int i=0;i>n;++i)
pow = pow / a;
return pow;
}
}
//powerdialog.java
import javax.swing.JOptionPane;
public class PowerJDialog
{
public static void main(String[] args)
{
String inputString;
float a; //Base number
long n; //Exponent
int pow; //Power
final double MAX_EXPONENT = 40;
//user to enter base number
inputString = JOptionPane.showInputDialog("What is the base number?");
//Convert the input into an int
a = Integer.parseInt(inputString);
//enter user to exponent
inputString = JOptionPane.showInputDialog("What is the exponent?");
//Convert the input into an int
n = Integer.parseInt(inputString);
while (n > MAX_EXPONENT)
{
//Display error message if exponent is greater than 40
JOptionPane.showMessageDialog(null, "The power value cannot be greater than 40. Please input another value.");
//Request another exponent
inputString = JOptionPane.showInputDialog("What is the exponent?");
n = Integer.parseInt(inputString);
}
Power p = new Power(a,n);
//Display results
JOptionPane.showMessageDialog(null, "The power value is " + Math.pow(a,n) + ".");
System.exit(0);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.