Write and test the following method that implements the power function:
static double pow(double x, int n)
This method returns the value of x raised to the power n. For example pow(2.0, -3) would return
2-3 = 0.125
For each value of pow(x,n) that you print, also print the value of Math.pow(x,n) to check your results.
Here is a sample run:
0.125
0.25
0.5
1.0
2.0
4.0
8.0
16.0
32.0
64.0
Press any key to continue.....
So far,I have done the coding up to this far..I can't seem to get it right. - import javax.swing.JOptionPane;
-
-
class PowerOf {
-
public static void main(String[] args){
-
-
String strX;
-
double x;
-
String strN;
-
int n;
-
int counter=1;
-
double value;
-
-
strX = JOptionPane.showInputDialog(null, "Enter the x value: ");
-
x = Double.parseDouble(strX);
-
-
strN = JOptionPane.showInputDialog(null, "Enter the power(n) value: ");
-
n = Integer.parseInt(strN);
-
-
for (counter=1; counter<=10; counter++)//to print the value
-
{
-
pow(x,n);
-
System.out.println(pow(x,n));
-
counter++;
-
}
-
-
while (counter!=10)//for checking the answer
-
{
-
counter++;
-
-
-
value=Math.pow(x,n);
-
System.out.println(value);
-
n++;
-
}
-
}
-
-
public static double pow(double x, int n){
-
-
double value=1;
-
-
for (int counter=1,counter<n, counter++)
-
{
-
value=1/value*2;
-
-
}
-
-
value=value*2;
-
-
n++;
-
return value;
-
}
-
-
}
-
-
Hope u can spare the time to look through. Would appreciate it. Thanks
1 5026
Write and test the following method that implements the power function:
static double pow(double x, int n)
This method returns the value of x raised to the power n. For example pow(2.0, -3) would return
2-3 = 0.125
For each value of pow(x,n) that you print, also print the value of Math.pow(x,n) to check your results.
Here is a sample run:
0.125
0.25
0.5
1.0
2.0
4.0
8.0
16.0
32.0
64.0
Press any key to continue.....
So far,I have done the coding up to this far..I can't seem to get it right. - import javax.swing.JOptionPane;
-
-
class PowerOf {
-
public static void main(String[] args){
-
-
String strX;
-
double x;
-
String strN;
-
int n;
-
int counter=1;
-
double value;
-
-
strX = JOptionPane.showInputDialog(null, "Enter the x value: ");
-
x = Double.parseDouble(strX);
-
-
strN = JOptionPane.showInputDialog(null, "Enter the power(n) value: ");
-
n = Integer.parseInt(strN);
-
-
for (counter=1; counter<=10; counter++)//to print the value
-
{
-
pow(x,n);
-
System.out.println(pow(x,n));
-
counter++;
-
}
-
-
while (counter!=10)//for checking the answer
-
{
-
counter++;
-
-
-
value=Math.pow(x,n);
-
System.out.println(value);
-
n++;
-
}
-
}
-
-
public static double pow(double x, int n){
-
-
double value=1;
-
-
for (int counter=1,counter<n, counter++)
-
{
-
value=1/value*2;
-
-
}
-
-
value=value*2;
-
-
n++;
-
return value;
-
}
-
-
}
-
-
Hope u can spare the time to look through. Would appreciate it. Thanks
When postind code please use code tags.
You need to handle all the possible scenarios using if-else
I have modified your program a bit. You should be able to finish it up now - import javax.swing.JOptionPane;
-
class PowerOf {
-
public static void main(String[] args){
-
String strX;
-
double x;
-
String strN;
-
int n;
-
int counter=1;
-
double value;
-
strX = JOptionPane.showInputDialog(null, "Enter the x value: ");
-
x = Double.parseDouble(strX);
-
strN = JOptionPane.showInputDialog(null, "Enter the power(n) value: ");
-
n = Integer.parseInt(strN);
-
double check = Math.pow(x, n);
-
value = PowerOf.pow(x, n);
-
System.out.print("Correct value is "+check+" : My value is "+value);
-
}
-
public static double pow(double x, int n){
-
double value = 0.0;
-
if(n < 0) {
-
//write logic here
-
}
-
else if(n == 0) {
-
//write logic here
-
}
-
else {
-
for (int i = 1; i < n; i++) {
-
//write logic here
-
}
-
}
-
return value;
-
}
-
}
-
-
Sign in to post your reply or Sign up for a free account.
Similar topics
by: da Vinci |
last post by:
Hi Gents,
This is what I am trying to do.
Say you have a double or a float with the value 14.5624 for example.
How could I take that variable and get the 14 into an integer variable
and the...
|
by: Young J. Putt |
last post by:
I have a vb.net datagrid bound to a dataview on a windows form. I want to
use the datagrid to display and filter a list of items, but since the data
is complex, I don't want the user to edit the...
|
by: Joe |
last post by:
Is there a way to get 1000000 to display as 1,000k using double.ToString()?
-Joe
|
by: matrim |
last post by:
What I'm trying to do:
1. Attempt to open the file. The filename, c:\\windData.txt, should be hardcoded into your program. Note the two slash characters in the file name. If the file cannot be...
|
by: Chris Stankevitz |
last post by:
Q1: Does c++ provide pow(int,int)?
Q2: If not, why not?
Thanks,
Chris
|
by: Sarath |
last post by:
Why the following happening?
UINT m_uArrXpos;
UINT **p = m_uArrXposSchema; // Failed to compile
UINT **p = (UINT**)m_uArrXposSchema; // Success compilation
|
by: Maarten van der Cammen |
last post by:
Hi,
In a table I have two numeric fields (e.g. FieldA and FieldB). Both fields
have double precicion (field size "double"), Format "Fixed" and Decimal
places "3" since I want to calculate with...
|
by: sharptool |
last post by:
Please, please, please make suggestions on how to resolve these errors.
I am trying to build a program that computes a, b, c for the quadratic equation but I'm not having any luck. It's been 5...
|
by: jombloboy |
last post by:
I'm trying to find the biggest fibonacci number a double variable can handle, my loop looks like this:
double num1 = 1;
double num2 = 2;
double fib = 0;
{
while ( fib >= 0 )
...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
| | |