• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • paul wheaton
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Tim Holloway
  • Carey Brown
  • salvin francis

help with a program

 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm trying to create a class First which contains at least 10 sayings as static String data members.It also contains getFortune() method which picks and returns one of the sayings at random.
Can someone help me write the code?
Thanks.
 
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sarah, sounds like a your class assignment, Have YOU tried to write a code by yourself first. we are more than happy to help you but you need to show that you want to LEARN java. I suggest write a code for this problem than we take a look at it and help you to resolve the problems.
I just want to encourage you to try yourself first, failing is not a bad thing but not trying is....I hope you understand.
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am not going to write the code for you because the only way to truly know how to program is to practice by doing. I will give you some tips to point you in the right direction:
1) You need to store your elements so that you can easily access them. The easiest data structure for this implementation would be an array.
2) Create a method like 'int psuedoRandom()' which will return a random int value. There is a reference in the API for a Random class. You should start there:
http://java.sun.com/j2se/1.3/docs/api/java/util/Random.html
*note: Make sure the value is within the number of elements that you have. Try to mod the value with the number of elements in your array, with % modulus operator.
3) In the method that prints out the phrase, you will want to print out the phrase by accessing it in the array with the random number you got back from the pseudoRandom() method as the index, like:
System.out.println("The phrase is :" + array[i]);

Hope this helps.
 
Sarah Haideri
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The program which I've written is:

Am I writing it correctly? This program gives me an error.
Thanks for your help.
 
Ranch Hand
Posts: 1067
2
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Code looks ok, what is the error you are getting?
 
Creativity is allowing yourself to make mistakes; art is knowing which ones to keep. Keep this tiny ad:
professionally read, modify and write PDF files from Java
https://products.aspose.com/pdf/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!