-
July 25th, 2010, 01:14 AM
#1
Brain not working - Need help on 2 methods ...
Here is what I am trying to do here. I am trying to write 2 methods, but I can not seem to get that going. This is not my first Java program, but I am definitely a novice -- trying to teach myself with a couple of books...I am stuck, though.
Methods I am trying to write (have written these a few times, but now starting over):
1. Method called squared that uses a “for” loop to calculate and print n2 (n squared).
2. Method called exponential that uses a “for” loop to calculate and print 2n (2 to the Nth power)
This will all be ran through the main method to see which method runs the fastest for n= 1- 20.
Here is my code:
Code:
public class Timing {
public static void main(String[] args){
long start, end;
double result,difference;
for(int n=1; n<=20; n++){
// Calculate the Time for n^2.
start = System.nanoTime();
// Call method to calculate n^2
squared();
end = System.nanoTime();
difference = (end - start);
}
for(int n=1; n<=20; n++){
// Calculate the Time for 2^n.
start = System.nanoTime();
// Call method to calculate 2^n
exponential();
end = System.nanoTime();
difference = (end - start);
}
}
// Method to calculate n^2
public static void squared(){
}
// Method to calculate 2^n
public static void exponential(){
for(int n = 1; n <= 1; n++){
}
}
}
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|