• Post Reply Bookmark Topic Watch Topic
  • New Topic

Trying to build a simple cache memory  RSS feed

 
Daniel Ungerfält
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi there!
Started ok i guess? But when it comes to the save and get methods im totaly lost.. Am I totaly out in the blue here?( see the code)

 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Daniel Ungerfält wrote:But when it comes to the save and get methods im totaly lost..


It would help if you explain to us what you are trying to do. In other words, we can't help you get "un-lost", if we have no idea where you are attempting to go.

Henry
 
Daniel Ungerfält
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry for that.
Im trying to make a calculator that is only counting 2 ^ x ( 2 to the power of x) and its suppose to save every action /result.

I will have an instance of HashMap<Integer,Long> that will works as the memory. called "m".

There will be another class for he main method later that will handle the interaction with the user. (Scanner, output etc)

Using this method to calcuate (2^x) :

public long computePower(int value){
if(value <= 0 )
return 1;
else{
return 2 * computePower(value-1);


Seems to work fine. But I would like to have som help adding it to the memory and to collect it.

The main method will look something like this :
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Daniel Ungerfält wrote:
Seems to work fine. But I would like to have som help adding it to the memory and to collect it.


Still not exactly sure what you want, but will take a guess.

I will speculate that you want to prevent calculating the same request more than once. Basically, when computePower() is called, it will check for previous calls, along with the previous results. If so, it will return the previous result. And if not, it will calculate it, and store the result in the map, which is also returned. Furthermore, since your implementation is recursive, a single method call can generate lots of "saves" of previous results into the map / cache.

Is this what you are looking to do? And if so, what issue are you running into?

Henry
 
Daniel Ungerfält
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is this what you are looking to do? And if so, what issue are you running into?


Yes thats correct! :-)
It just as simple as I dont know if my codes for storing and return the stored result accualy is working, though I dont know how to test it and I dont got any "errors" in eclipse.
 
Daniel Ungerfält
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
After some more coding this is where im stuck right now:
I know what NullpointerException is but cant figure out what to type at theese lines to make it work.

Edit: I also got this message: The method addValueToMemory(Integer, Long) from the type Model is never used locally..?




Add a positive number, -1 to clear or -2 to quit
2
Exception in thread "main" java.lang.NullPointerException
at Model.compute2Power(Model.java:28)
at Program.main(Program.java:32)



And the main method:

 
Liutauras Vilda
Sheriff
Posts: 4917
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Daniel, this statement is misleading, please think about something more accurate.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Daniel Ungerfält wrote:
It just as simple as I dont know if my codes for storing and return the stored result accualy is working, though I dont know how to test it and I dont got any "errors" in eclipse.


To be blunt, it is not working. In fact, there is no coding in the application for storing the values at all. You simply "used the cache" to retrieve values -- values that don't exist because you never previously calculated them (or store them).

Henry
 
Liutauras Vilda
Sheriff
Posts: 4917
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Daniel wrote:Exception in thread "main" java.lang.NullPointerException
at Model.compute2Power(Model.java:28)

Daniel, this method is not correct. "else" block cannot be reached.
 
Daniel Ungerfält
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To be blunt, it is not working. In fact, there is no coding in the application for storing the values at all. You simply "used the cache" to retrieve values -- values that don't exist because you never previously calculated them (or store them).


Ok. Im still pretty new to coding as you can tell. I´ve tried a bunch of different code by now and im feeling pretty lost .

Could someone just help me putting the right code at this two methods and maybe Im in the match again? Though I guess thats whats messes up?

private void addValueToMemory(Integer value, Long result){
______________________?
}
private Long getValueFromMemory(Integer value){
______________________?
}


 
Campbell Ritchie
Marshal
Posts: 56541
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you using a Map definitely? Go through the Java® Tutorials section about Maps.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Daniel Ungerfält wrote:
Ok. Im still pretty new to coding as you can tell. I´ve tried a bunch of different code by now and im feeling pretty lost .


This is because it is not about coding. It is about logic. You need to start there. And the logic (described in English) should be (when you need to fetch the value) ...

Is it in the cache? If yes, then return the value. If not in the cache, then calculate the value, store the value in the cache, and then return the value.


Before you should even attempt any coding, you need the logic that you will be coding for -- coding is similar to anything else in this regard. Trial and error doesn't work. If you don't know what you need to do, you can't really do it right.

Henry
 
J. Kevin Robbins
Bartender
Posts: 1801
28
Chrome Eclipse IDE Firefox Browser jQuery Linux MySQL Database Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Daniel Ungerfält wrote:
Ok. Im still pretty new to coding as you can tell. I´ve tried a bunch of different code by now and im feeling pretty lost .

Trial-and-error coding seldom works. I think this is where it's time to say StopCoding (that's a link; read it). You need to turn off your computer and get a pencil and paper. Write down WHAT you want to do, not HOW you are going to do it. Forget about Java or any programming terminology. This exercise is about logical reasoning. When you can explain to a 10 year old what you are trying to do, then you are ready to start writing code.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!