• 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

learning methods

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i am new to java and i did a problem and i just wanted to know how would you do this problem with implementing methods, i am trying to learn them but they are confusing, any help would be appreciated
 
Sheriff
Posts: 7125
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
First, get your code out of the main() method:

The next thing is to describe in board terms what your program is doing, something like:

Each of these becomes a method.  Remove the code from run() that represents the method and add a call to the method you just created.  Like:

Starting to make sense?
 
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can shorten the main method to one line:-I couldn't be bothered to find correct number for your class, but that is hardly a good name for a class. I suppose you have been told to use such names, but if you come back to that code in January, you won't know what any of it means from the class names.
Now that everybody uses Java8, let's see if we can't make your array with a Stream. You shou‍ld in theory change the getSize() method to return a long, because the Random method takes a long as a parameter, but we'll miss that bit out for the time being.The Random#ints() method is overloaded; that version returns an IntStream with the same number of elements as getSize, between 1 and less than 7, i.e. 1...6. Note the first link I showed you says, “origin (inclusive) and bound (exclusive).” If you subtract 1 from 7 you get 6, which is how many different values you might get.
Now, if you go through the IntStream documentation, you find it has a method which turns it directly into an array. It will contain exactly the number of elements you put into getSize.

Look carefully at your line 12 and the Random documentation and work out what sort of range of results you will get from line 12.
Never use == true or == false, which are both poor style and error‑prone.
Never
if (b == true) ...
Always
if (b) ...
Never
if (b == false) ...
Always
if (!b) ...
Your line 16 shou‍ld be changed; so shou‍ld your line 22, which would be better as
else  {
 
Campbell Ritchie
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

A few minutes ago, I wrote:. . . everybody uses Java8 . . .

Lies, all lies.

There are lots of people who still use older versions.
 
Campbell Ritchie
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The call to the IntStream shou‍ld of course be in a makeIntArray(long, int, int) method. You simply pass those three parameters to the Random object's method. If you manage to pass a larger value for origin than bound, I think you get a 0‑element Stream and a 0‑length array. I don't think you get any Exceptions, but it is a long time since I tried and I might have forgotten what happened.
 
reply
    Bookmark Topic Watch Topic
  • New Topic