
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++){
}
}
}

July 25th, 2010, 06:09 AM
#2
Re: Brain not working  Need help on 2 methods ...
If you are trying to write a method to calculate the square of a number the method needs to accept the number to square and return the result. For an exponential, your method needs to accept the number to raise to a power and the power to raise it too and again it needs to return the result.
Your code for timing the result doesn't do anything useful:
Firstly every time through the loop you assign a new value to 'difference' and then don't do anything with it. You should at least print out the value.
Secondly unless your computer is steam powered the calculation will be done so quickly you won't get a meaningful time. You need to put a for loop around the method call to call it multiple times (ie 10,000 times  depending on the speed of your system)

July 25th, 2010, 07:44 AM
#3
Re: Brain not working  Need help on 2 methods ...
I did not explain what I need to do very well. I will put the entire instructions, etc from the book. You see what I already have so far in the original post up above.
Straight from the book:
1. Create a java method called squared that uses a “for” loop to calculate and print n2.
2. Create a java method called exponential that uses a “for” loop to calculate and print 2n
Run each program for n = 1 to 20
Modify the timing.java program located below to use and test your methods at each value.
Graph the results in Excel and compare the running times.
timing.java program from book
Code:
/**
*** Measures the time of execution in Nanoseconds ***
***/
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();
//Add code to call method to calculate n^2
end = System.nanoTime();
difference = (end  start);
}
for(int n=1; n<=20; n++){
//Calculate the Time for 2^n.
start = System.nanoTime();
//Add code to call method to calculate 2^n
end = System.nanoTime();
difference = (end  start);
}
}
// Add methods to calculate n^2 and 2^n
}

July 25th, 2010, 12:16 PM
#4
Re: Brain not working  Need help on 2 methods ...
My previous answers still apply. For example if you were writing a method to add two numbers together, the method would have to accept two arguments and return the result ie
Code:
int add(int num1, int num2)
{
return num1 + num2;
}

July 26th, 2010, 02:26 AM
#5
Re: Brain not working  Need help on 2 methods ...
ok, here is hat I have. It is not working, I am not sure where I am going wrong here. This book sucks...not very explanatory.
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(n);
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(n);
end = System.nanoTime();
difference = (end  start);
}
}
// Method to calculate n^2
public static void squared(int n){
int result;
for(int i = 1; i <= 1; i++){
result = n * n;
System.out.println(result);
}
}
// Method to calculate 2^n
public static void exponential(int n){
int result;
for(int i = 1; i <= 1; i++){
result = Math.pow(2,n);
System.out.println(result);
}
}
}

July 26th, 2010, 05:35 AM
#6
Re: Brain not working  Need help on 2 methods ...
You are almost home, what you have left is some faulty logic. Read your code through, you should be able to spot them well enough. Some observations:
1) Your for loops do not make sense. Why do you have for loops inside your calculation methods, that allow only one pass? Rewrite the methods to allow sufficient params, suggest the pow argument for the exponent and the number of iterations for both of them.
2) Why do you assign the difference on each pass of the for loop, and not add? It is by far simpler to move the looping totally inside the calculation methods. In fact, if your aim is simply to test the speed of the methods, why not let them handle the nanotime comparison, and return the resulting long value? Furthermore, both of the loops use same variable, hence the second overwrites the value and the process time of the first method is entirely lost. Use long values there, and two variables.
3) After all is done, you do not do anything with either the result or the difference. How about printing the results?

July 26th, 2010, 05:42 AM
#7
Re: Brain not working  Need help on 2 methods ...
I will look over the logic again...I think that is mainly where I am missing something.
1) Your for loops do not make sense. Why do you have for loops inside your calculation methods, that allow only one pass? Rewrite the methods to allow sufficient params, suggest the pow argument for the exponent and the number of iterations for both of them.
1) I understand how the for loops operate, but they are supposed to run the loop for n = 120 , so it needs to run 20 times total....then the timing statements in the main method calculate the time it took to complete the operation.
2) Why do you assign the difference on each pass of the for loop, and not add? It is by far simpler to move the looping totally inside the calculation methods. In fact, if your aim is simply to test the speed of the methods, why not let them handle the nanotime comparison, and return the resulting long value? Furthermore, both of the loops use same variable, hence the second overwrites the value and the process time of the first method is entirely lost. Use long values there, and two variables.
2) I can't move the looping totally inside the methods. I am supposed to just add the required methods to make the program work correctly, and call them where they are called as of now. I am not supposed to rewrite everything, just add what is missing to make it work, according to the directions.
3) After all is done, you do not do anything with either the result or the difference. How about printing the results?
3) I was waiting to do that until I go the methods correct.

July 26th, 2010, 05:48 AM
#8
Re: Brain not working  Need help on 2 methods ...
Why in the world would you not be able to refactor your code? Of course you can move the for looping where ever you like. In case you do not want to, at least use long for the difference, have two variable (one for each method), and add the process time value to the correct variable upon each iteration. Do not be hesitant on using the printlines when you feel something is not working, why would you leave yourself blind?
And if you do not want to move the looping inside your calculation methods, least remove the obsolete looping you have there, for loop that is hardcoded to allow just one pass is simply silly.

July 26th, 2010, 05:59 AM
#9
Re: Brain not working  Need help on 2 methods ...
The book wants me to add to the code what is needed. I could easily write it all differently, but that defeats the purpose of doing what they ask me to do, and the lesson it is supposed to reinforce. I will try to make some changes and come back if it still does not work.

July 26th, 2010, 03:11 PM
#10
Re: Brain not working  Need help on 2 methods ...
Originally Posted by CarlMartin10
The book wants me to add to the code what is needed. I could easily write it all differently, but that defeats the purpose of doing what they ask me to do, and the lesson it is supposed to reinforce.
A loop that is hardcoded to only execute once is either a typo (and if this is from a book, it's fairly common to find code bloopers in books) or a deliberate error that you're expected to correct. I don't know which, but either way, you should fix it. As it is, it doesn't loop  it's clearly wrong. Having said that, the loop statement should be removed entirely  given the context, it's clearly unnecessary and potentially confusing.
YMMV.
Question authority; but, raise your hand first...
A. Dershowitz
Please use [CODE]...your code here...[/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.

July 29th, 2010, 01:44 PM
#11
Re: Brain not working  Need help on 2 methods ...
ok, I am getting close on this...I am just getting 2 errors.
Timing.java:45: class, interface, or enum expected
public static void exponential(int n){
^
Timing.java:47: class, interface, or enum expected
}
^
2 errors
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);
System.out.println(squared());
}
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);
System.out.println(exponential());
}
}
// Method to calculate n^2
public static void squared(int n){
result = n * n;
}
}
// Method to calculate 2^n
public static void exponential(int n){
Math.pow(2, n);
}
}
}
}

July 29th, 2010, 04:48 PM
#12
Re: Brain not working  Need help on 2 methods ...
You have too many closing curly braces. Try matching each opening and closing curly brace to find where you have the extra one.

July 29th, 2010, 05:52 PM
#13
Re: Brain not working  Need help on 2 methods ...
Originally Posted by keang
You have too many closing curly braces. Try matching each opening and closing curly brace to find where you have the extra one.
I fixed that error, and it really got pissed off, lol.
Timing.java:18: squared(int) in Timing cannot be applied to ()
squared();
^
Timing.java:21: squared(int) in Timing cannot be applied to ()
System.out.println(squared());
^
Timing.java:29: exponential(int) in Timing cannot be applied to ()
exponential();
^
Timing.java:32: exponential(int) in Timing cannot be applied to ()
System.out.println(exponential());
^
Timing.java:38: cannot find symbol
symbol : variable result
location: class Timing
result = n * n;
^
5 errors
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);
System.out.println(squared());
}
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);
System.out.println(exponential());
}
}
// Method to calculate n^2
public static void squared(int n){
result = n * n;
}
// Method to calculate 2^n
public static void exponential(int n){
Math.pow(2, n);
}
}

July 30th, 2010, 01:33 AM
#14
Re: Brain not working  Need help on 2 methods ...
Your methods method expect params, you call them without params.

July 30th, 2010, 10:18 AM
#15
Re: Brain not working  Need help on 2 methods ...
I fixed that error, and it really got pissed off, lol.
Timing.java:18: squared(int) in Timing cannot be applied to ()
squared();
^
If you want to learn to program in Java you are going to have to put some effort into learning to debug your own code. The compiler messages tell you what is wrong and where it is wrong. You just need to learn to decipher them and look at your code.
For example:
The message above tells you that line 18 of the Timing class has an error and it even shows you where on line 18 the error occurs (the '^' points to the position on the line above it, which is line 18 from your code). The message tells you you have a method with the same name as the one you are calling but it cannot be called in the way you are trying to call it. A quick look at the actual method and your call on line 18 shows they have a different number of parameters.
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
This is a CodeGuru survey question.
Featured
