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.
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.
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.
posted 17 years ago
The program which I've written is:
Am I writing it correctly? This program gives me an error. Thanks for your help.