PDA

View Full Version : Explanation of assignment


NichoLe
03-25-2005, 11:27 AM
Good Afternoon

I have an assignment due Sunday ... The problem is that i don't understand the question cam someone explain it for me


Question:

Create a class Power that contains two methods:
Write a method power(double a , int n) that calculates and displays the result of the double parameter a raised to the power n. For example

power(3.0 , 2); display 3 to the power 2=9
power(2.0 , 3); display 2 to the power 3=8

Call it from the main method

tboss132
03-25-2005, 01:30 PM
You obviously haven't read this (http://www.codingforums.com/showthread.php?t=53446)

NichoLe
03-25-2005, 01:55 PM
Well i didn't ask anyone 2 do it for me ... i just asked for explanation .. i don't understand the question and i wanted explanation thants all :mad: anyways sorry for asking and am sorry for registering in this forum




bye!

tboss132
03-25-2005, 02:08 PM
Nichole, I'm not trying to drive you away from coding forums, neither am I accusing you of asking people to help you do your assignment. I just wanted you to read that thread so you'll know how best to ask your question.
For starters, you've not told us what programming language the question is in. You should realise there's no way anyone can help you if you don't provide that. So post here the language you want to use and someone will set you on the right track

NichoLe
03-25-2005, 02:31 PM
if u saw my previous thread in the same section you'll know my intensions ... anyways am sorry the language is java and all i wanted is that someone explains the question for me ... and i read that page before i post my first thread and i know the policy :D Thank you

NichoLe
03-25-2005, 03:13 PM
I tried to understand it and i came up with this :confused:

import javax.swing.JOptionPane;

class Power
{

public static void main( String[] args )
{

int power;
int result;

// call power method with some test data
power(result, number);

}


public void power( double a, int n )
{

// write some code which calculates a to the power of n
result = Math.pow(a,n);

// now use System.out to write out the result
System.out.println(Math.round(result);
}
}


I still have doubts thou

Roelf
03-25-2005, 05:15 PM
close,

you defined the int result in the main method, and are using it in the power method, that wont work.

you don't define "number" anywhere

you missed a closing ) in the system.out.println command

the method power is not defined as static

you dont provide values for the given arguments

the import of the JOptionPane is not used anywhere

but your interpretation of the question was right.

fix those errors and it will work, still room for improvement though

NichoLe
03-25-2005, 06:39 PM
Thank you ill fix them now :D

NichoLe
03-25-2005, 07:52 PM
WHO CHANGE THE TITLE OF MY THREAD :mad:

tboss132
03-26-2005, 02:16 PM
Probably a moderator who prefers it to have a more descriptive title. That way, others will be able to find your page quickly when they search from google and other search engines.

NichoLe
03-26-2005, 02:55 PM
Fine ... can someone help me ...

this is the last code i did for this question :confused:

import javax.swing.JOptionPane;

class Power
{

public static void main( String[] args )
{
double result;
double a;
int n, num1, num2;
String strBase, strPower;
int numa;
int numn;

strBase = JOptionPane.showInputDialog("Enter the base: ");
num1 = Integer.parseInt(strBase);

strPower = JOptionPane.showInputDialog("Enter the power: ");
num2 = Integer.parseInt(strPower);

power( num1, num2);

}

private static void power(double a, int n)
{
double result = Math.pow(a, n);


JOptionPane.showMessageDialog(null,"" + num1 + " to the power of " + num2 +
" = " + result);

}
}

Roelf
03-29-2005, 10:53 AM
Hi,

in the main method, you declare the following variables:

double result;
double a;
int n, num1, num2;
String strBase, strPower;
int numa;
int numn;

You only use strBase, strPower, num1 and num2 there. The rest is allowed to delete.

In the power method, you are using num1 and num2 but they are not defined here, they are defined in the main. But you can access their values because they are provided as an argument to the method call, but they are known inside the method as a and n (see in the signature for the method) so you have to change your outputstring to use these variables instead of num1 and num2

when passing the num1 into the power call, you are actually providing an integer, where a double is expected by the method. So cast the integer to a double, or better declare num1 as a double and parse the string from the prompt to a double instead of an int

Lozza
04-03-2005, 03:05 AM
Just a little advice to help stop the creation of redundant variables:
Try to keep the variable names the same throughout your programme. What I mean by this is that if you create a method called "addition" that takes two numbers and the main method parses variables num1 and num2, then call the variables in the "addition" method num1 and num2. There is little point in changing the variable name and it will just get confusing. Keeping the names the same allows you to trace through the programme with little difficulty.

lukeproductions
04-03-2005, 08:13 PM
I had to do similar coding assignment for my class.
This was the problem:
Write a method public static int power (int base,
int expo) for MathOp: It returns base to the expo power. Return -1 in all cases in
which the power cannot be computed using int values. But return zero if either parameter
is negative. Hint: The largest int value is 2,147,483,647.
(just to clarify MathOp in these directions, its a class we had to develop and power was one of its methods)

And this is the code i submitted to my teacher and received 10/10 on it. The thing is that it does not include any work around double values but logic behind it is much simpler than what i saw. I don't know if its gonna be helpful to you or not, but i hope it will.

public static int power (int base, int expo)
{ if(base<=0 || expo<0)
return 0;
int limit = 2147483647 / base;
int power = 1;
for(; expo > 0 && power <= limit; expo--)
power = power * base;
if (expo == 0)
return power;
else
return -1;
}