• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Incrementing trouble

 
Ranch Hand
Posts: 48
Mac OS X Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Sheriff
Posts: 3063
12
Mac IntelliJ IDE Python VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Mac OS X Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
May be modify your next method like this:

 
John Vorona
Ranch Hand
Posts: 48
Mac OS X Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That does absolutely nothing T_T.
 
John Vorona
Ranch Hand
Posts: 48
Mac OS X Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 3063
12
Mac IntelliJ IDE Python VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Marshal
Posts: 79180
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch Suryateja Pericherla
 
John Vorona
Ranch Hand
Posts: 48
Mac OS X Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Mac OS X Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here is my updated code which i believe to be the final product.

 
reply
    Bookmark Topic Watch Topic
  • New Topic