• Post Reply Bookmark Topic Watch Topic
  • New Topic

Possible Recursion??  RSS feed

 
Frank Ponce
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I need some help figuring out what this program does....I understand how the knapsack object with capacity 30 and the array object of size 5 are created. The thing that i cant seem to figure out is the "answer" method, Is that method using recursion?? The output when the program is run is:
Total Value: 38

The data.txt file has the following values:
30
5
20 10
10 7
15 14
18 18
7 15

Ive tried everything i can think of and if i


after



The output is:
20 10 20 15 20 10 20 18 20 10 20 15 20 10 20 27 20 10 20 15 20 10 20 38 20 10 20 35 20 30 20

No idea where this comes from

As well as putting



after




which outputs:

0 0 0 0 0 0 0 0 0 0 0 0 20 20 20 0 0 0 0 0 20 20 0 0 20 0 0 20 30 35 38

and again no idea where dontTake is equated to these values


 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16028
87
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you know what "recursion" means? It means that a method calls itself. Now look carefully at the "answer" method. Do you see it calling itself somewhere?

By the way, the code that you posted will not compile. (Did you try compiling it?). This line is not valid Java code:

if (lastAllowed >< 0)

If you want to test if a variable is not equal to a value, you should use the "!=" operator instead of "><".

if (lastAllowed != 0)
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!