• Post Reply Bookmark Topic Watch Topic
  • New Topic

Incrementing trouble  RSS feed

 
John Vorona
Ranch Hand
Posts: 48
Java Mac OS X Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Question : NumberGiver models a machine that has a current number and an increment,
and when asked for the next number, it increments the current number by
the increment value and returns that value, setting that value to the new
current number.

My Code :


I need to increment the number by 5 at one point and have no idea how to do this. Any suggestions?
 
Greg Charles
Sheriff
Posts: 3015
12
Firefox Browser IntelliJ IDE Java Mac Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have a version of next() that takes an increment parameter. Also, you don't need to have both number and value as member variables. You always set them to be the same value, so it's a needless complication.
 
John Vorona
Ranch Hand
Posts: 48
Java Mac OS X Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am not sure of what to put in the body of the next(int num) method.
Here is my updated code, could you guide me in the right direction?

 
Suryateja Pericherla
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
May be modify your next method like this:

 
John Vorona
Ranch Hand
Posts: 48
Java Mac OS X Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That does absolutely nothing T_T.
 
John Vorona
Ranch Hand
Posts: 48
Java Mac OS X Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I finally got this to work.

I just need someone to tell me whether what i did was right or if it was done by fluke/cheap way etc..

Here is my NumberGiver class



And here is the NumberGiverTester class



The NumberGiverTester code was prewritten so ignore it, my task was to design a NumberGiver class with a constructor and methods which would output the "Expect: x" value when called. The program prints the expected value, I'm just curious whether i did it the right way..?
 
Greg Charles
Sheriff
Posts: 3015
12
Firefox Browser IntelliJ IDE Java Mac Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It looks good to me. However, you still have the value variable. If you removed it, would it affect the function of your program in any way?
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch Suryateja Pericherla
 
John Vorona
Ranch Hand
Posts: 48
Java Mac OS X Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah the value variable is useless, its there because i had a previous method

which i later deleted, so yeah, you are right! Thanks guys.
 
John Vorona
Ranch Hand
Posts: 48
Java Mac OS X Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is my updated code which i believe to be the final product.

 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!