A table of powers

Hi,

I sort of have an idea of how to start with this program, but I need help on how to code it so that it generates a table as output like this:

ATableofPowers

IntegerSquareCubeQuarticQuinitc

00 0 00

11 1 11

24 8 16 32

/**

* Write a static method square that will take an integer and return its square

* Write a static method cube that will take an integer and return its cube

* Use these methods to write methods quartic and quintic

* They will return the fourth and fifth power of an integer

*/

import javax.swing.*;

publicclass powers

{

publicstaticvoid main (String args[])

{

int x = 0;

String input;

publicstaticint powerSquare(int x)

{

input = JOptionPane.showInputDialog("Enter an integer from 1 to 25.");

x = Integer.parseInt(input);

for (int i=0; i < 26; i++)

{

return x * x;

}

publicstaticint powerCube(int x)

{

input = JOptionPane.showInputDialog("Enter an integer from 1 to 25.");

x = Integer.parseInt(input);

if(x >= 0 && x < 26)

return x * x * x;

}

}

I have an idea of what the methods are supposed to do, but the rest of this program I really need help on.Any help is appreciated. Thanks.

[2433 byte] By [asian_cajun_2000a] at [2007-10-3 4:23:08]
# 1
I guess you need some loops, System.out.print(), System.out.println(), and maybe the '\t' char.
CeciNEstPasUnProgrammeura at 2007-7-14 22:25:26 > top of Java-index,Java Essentials,Java Programming...
# 2

System.out.println( 0 + " " + 0 + " " + 0 + " " + 0 + " " + 0 );

System.out.println( 1 + " " + 1 + " " + 1 + " " + 1 + " " + 1 );

System.out.println( 2 + " " + 4 + " " + 8 + " " + 16 + " " + 32 );

System.out.println( 3 + " " + 9 + " " + 27 + " " + 81 + " " + 243 );

System.out.println( 4 + " " + 16 + " " + 64 + " " + 256 + " " + 1024 );

System.out.println( 5 + " " + 25 + " " + 125 + " " + 625 + " " + 3125 );

System.out.println( 6 + " " + 36 + " " + 216 + " " + 1296 + " " + 7776 );

System.out.println( 7 + " " + 49 + " " + 343 + " " + 2401 + " " + 16807 );

System.out.println( 8 + " " + 64 + " " + 512 + " " + 4096 + " " + 32768 );

System.out.println( 9 + " " + 81 + " " + 729 + " " + 6561 + " " + 59049 );

Michael.Nazarov@sun.coma at 2007-7-14 22:25:26 > top of Java-index,Java Essentials,Java Programming...
# 3

It looks to me like your main problem is to understand the text of the assignment .

As I understand it the program should look somewhat like this

import javax.swing.*;

public class Powers

{

public static void main (String args[])

{

int x=0;

while(x<1 || x>25) {

String input = JOptionPane.showInputDialog("Enter an integer from 1 to 25.");

if(input!=null) x = Integer.parseInt(input);

}

String output="Integer "+x+"\nSquare "+square(x)+"\nCube "+cube(x)

+"\nQuartic "+quartic(x)+"\nQuinitc "+quintic(x);

JOptionPane.showMessageDialog(null, output, "Table of Powers", JOptionPane.PLAIN_MESSAGE);

}

public static int square(int x)

{

return x * x;

}

public static int cube(int x)

{

return x * x * x;

}

public static int quartic(int x)

{

return square(x) * square(x);

}

public static int quintic(int x)

{

return square(x) * cube(x);

}

}

Zoidberg42a at 2007-7-14 22:25:26 > top of Java-index,Java Essentials,Java Programming...
# 4

You're trying to define methods inside of other methods. That doesn't work (unless they're inside inner classes but you don't need to worry about that right now).

Also from the assignment description in the comment, it sounds like the methods to find squares and cubes are not supposed to have UI elements, or loop. You're not creating the table or getting user input in those methods; you're just doing math. Then some other method will be creating the table using those methods. It doesn't sound like you need to get any user input at all, actually.

paulcwa at 2007-7-14 22:25:26 > top of Java-index,Java Essentials,Java Programming...
# 5
Good idea, Zoidberg 42. By doing his homework for him, he'll never have to learn anything!
paulcwa at 2007-7-14 22:25:26 > top of Java-index,Java Essentials,Java Programming...
# 6

> Good idea, Zoidberg 42. By doing his homework for

> him, he'll never have to learn anything!

I'm waiting for the day when the customer approaches the programmer and tells him he needs this and that, and one of these guys asks back: "can you give me example code"?

Then again, this probably only doesn't happen because the customer doesn't get near the programmers.

CeciNEstPasUnProgrammeura at 2007-7-14 22:25:26 > top of Java-index,Java Essentials,Java Programming...
# 7
I hope that one implementation with all the methods (reply 3) was a joke.anyway, ill throw the poor kid a bone.Math.pow( num, exp );and 2 for loops http://java.sun.com/j2se/1.4.2/docs/api/java/lang/Math.html#pow(double,%20double)
TuringPesta at 2007-7-14 22:25:26 > top of Java-index,Java Essentials,Java Programming...
# 8

> I hope that one implementation with all the methods (reply 3) was a joke.

> anyway, ill throw the poor kid a bone.

>

> Math.pow( num, exp );

> and 2 for loops

>

> http://java.sun.com/j2se/1.4.2/docs/api/java/lang/Math.html#pow(double,%20double)

1. The OP's assignment ASKED for the methods.

2. Math.pow will be slower than using standard multiplication. Multiplication is better for something like this--it gives exact answers in integers without casting the result.

MLRona at 2007-7-14 22:25:26 > top of Java-index,Java Essentials,Java Programming...