If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

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.

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)

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?

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 = 1-20 , 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?

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.

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.

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.

A loop that is hard-coded 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 &#91;CODE]...your code here...&#91;/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.

Re: Brain not working - Need help on 2 methods ...

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.

* The Perfect Platform for Game Developers: Android
Developing rich, high performance Android games from the ground up is a daunting task. Intel has provided Android developers with a number of tools that can be leveraged by Android game developers.

* The Best Reasons to Target Windows 8
Learn some of the best reasons why you should seriously consider bringing your Android mobile development expertise to bear on the Windows 8 platform.