• Post Reply Bookmark Topic Watch Topic
  • New Topic

Help with Java Final  RSS feed

 
J Gallagher
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Inside the "results" method, it flags the array "flips" as "cannot be resolved to a variable, though declared in the method "flip".  What am I missing?


 
Ganesh Patekar
Bartender
Posts: 726
23
Eclipse IDE Hibernate jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to CodeRanch!  
Because you have declared int flips[] in flip() method so it is a local variable whose scope is withing the method flip(). Means it's existance is within flip() method only.
 
J Gallagher
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ganesh Patekar wrote:Welcome to CodeRanch!  
Because you have declared int flips[] in flip() method so it is a local variable whose scope is withing the method flip(). Means it's existance is within flip() method only.


That makes sense, but how do I make it available to the results method?
 
Ganesh Patekar
Bartender
Posts: 726
23
Eclipse IDE Hibernate jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
  • I don't think there is need to declare result as static method.
  • If you want to access int flips[] declared in class in static result then you also have to declare flips[] as static variable because non-static fields can't be accessed in static method so better change result from static to instance method.
  • After declaring result method as instance method invoke it using reference variable.
  • Did you notice this? I tihink you wanted to use equality operator i.e. == rather than assignment operator i.e. =.

  • Do you want to count no of 0 values returned by (int) (Math.random() * 2);?
  • Declare private int flips[] in the class and initialize it in constructor.
  • int heads; is a local variable of method results which should be initialized before using it because when you declare local variable that variable is not initialized with default value.
  •  
    Ganesh Patekar
    Bartender
    Posts: 726
    23
    Eclipse IDE Hibernate jQuery MySQL Database Spring Tomcat Server
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    and you have to enclose flip and results method invocation in x for loop.You don't need to import util.
     
    Suresh Gowda
    Greenhorn
    Posts: 7
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Working Code..



    Hope this is enough
     
    Ganesh Patekar
    Bartender
    Posts: 726
    23
    Eclipse IDE Hibernate jQuery MySQL Database Spring Tomcat Server
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Suresh Gowda wrote:Working Code..
    We are not suppose to give ready made code. It is against the main goal of this forum. Also not beneficial to OP as OP's concepts will not be cleared. As Greenhorn now you can't even delete or update the post. By the time moderators decide,
  • Can you tell me, in your provided code how many times results method will be invoked?
  • What output would it produce? and why?
  • Was there really need to declar int length in results method?
  •  
    Ganesh Patekar
    Bartender
    Posts: 726
    23
    Eclipse IDE Hibernate jQuery MySQL Database Spring Tomcat Server
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Worth reading Not A Code Mill 
     
    Campbell Ritchie
    Marshal
    Posts: 56562
    172
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Welcome again

    You have got some good advice from GP.
    Despite what you see in some books and the Java™ Tutorials, I do not like to see arithmetic done with Math#random. You will find lots of discussion about “random” numbers and also what I would prefer to see, in this thread.
     
    Campbell Ritchie
    Marshal
    Posts: 56562
    172
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Ganesh Patekar wrote:. . . We are not suppose to give ready made code. It is against the main goal of this forum. Also not beneficial to OP as OP's concepts will not be cleared. . . . By the time moderators decide,  . . .
    Agree.

    But I can pull rank and remove the solution. Please don't be annoyed with me; GP has explained the reasons really well.
     
    Ganesh Patekar
    Bartender
    Posts: 726
    23
    Eclipse IDE Hibernate jQuery MySQL Database Spring Tomcat Server
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Opps !! again missed to spot Math#random, despite reading your advice so many times before that prefer Random over Math#random
     
    Campbell Ritchie
    Marshal
    Posts: 56562
    172
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    IF you want to fill an array with pseudo‑random ints, don't use a loop. There is a new up‑to‑date way to do it in Java8. Use an IntStream, which you can read about in the link (Java8 only). It goes through any number of ints, doing all the iteration for you. When you call the toArray method, it creates an int[] of the appropriate size, and only then does it execute the preceding methods; Streams all work on lazy execution. You can create an IntStream from a Random object with its ints method. Note that ints() is overloaded. If you take the last parameter and subtract the preceding parameter from it, you get the number of different options. So using 1000L, 0, 2 will give you a 1000‑element Stream with (2 − 0) = 2 options, the smallest value being 0.
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!