| Author |
i need massive help
|
jay bob
Greenhorn
Joined: Jan 24, 2010
Posts: 3
|
|
1)generateArray populate array with 35 random numbers from 1-100
2)getElement: returns the value of the element of the array specified by a parameter passed to a method. this will be called with a statement suck as number= getElement(25);
3)printArray from 0-34
4)main method call generate Array, printArray, then repeatedly asks the user which element they like to see, and display that element using getElement, until the user enter -1
i managed to get number 3) down and 1)..sort of but i have no idea how to do the getelement method and the main method, btw i'm doing this on the program known as bluej and i need a lot of help please
|
 |
fred rosenberger
lowercase baba
Bartender
Joined: Oct 02, 2003
Posts: 9956
|
|
|
we don't hand out answers to homework here, but folks will bend over backwards to help you. Show us what you've done and tell us what works (and what doesn't), and you'll get tons of responses.
|
Never ascribe to malice that which can be adequately explained by stupidity.
|
 |
Wouter Oet
Saloon Keeper
Joined: Oct 25, 2008
Posts: 2700
|
|
Couple of hints:
You can get element x by calling: number = array[x];
Main method looks lite this: public static void main(String[] args) or public static void main(String... args)
One way of getting input is the Console class
Advice:
Read some java tutorials.
|
"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
|
 |
jay bob
Greenhorn
Joined: Jan 24, 2010
Posts: 3
|
|
fred rosenberger wrote:we don't hand out answers to homework here, but folks will bend over backwards to help you. Show us what you've done and tell us what works (and what doesn't), and you'll get tons of responses.
um well, i failed this part of my homework and i honestly have no clue what to do well sorta but i don't got the right idea.
|
 |
jay bob
Greenhorn
Joined: Jan 24, 2010
Posts: 3
|
|
jay bob wrote:
fred rosenberger wrote:we don't hand out answers to homework here, but folks will bend over backwards to help you. Show us what you've done and tell us what works (and what doesn't), and you'll get tons of responses.
um well, i failed this part of my homework and i honestly have no clue what to do well sorta but i don't got the right idea.
edit: ok heres what i got so far could someone please PLEASE correct this for me
|
 |
Henry Wong
author
Sheriff
Joined: Sep 28, 2004
Posts: 16695
|
|
Take a look at the java.util.Random class.
Henry
|
Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32712
|
|
Since you are new (welcome to the Ranch ) and haven't used the Code button before, I have edited your post and you can see how much better it looks. There appear to be some spelling errors, and spacing/indentation irregularities; whenever you post code be sure to use copy and paste so we see the actual code. People will be better able to help if your thread title tells them what the problem is.
What is your mainMethod for? It is private and you don't appear to call it anywhere, so it will never do anything. You are reminded the format of a main method would be something like thisThis depends on there being a class called SomeClassOrOther which has a doSomething() method.
I would also remind you of the conventional way to write a for loop to iterate an arrayThe "end" comment is not essential; I like it, some other people don't.
The reason that convention has grown up is that you can apply arrays of any size to that for loop secure in the knowledge you will never go beyond the confines of the array. Writing <= 34 is error-prone; the tiniest mistake can cause nasty errors.
There is another version of that for loop, and I'll leave you to work out how it works.The enhanced for loop is good when you want the array elements to be iterated in "read-only" mode. It is described in the Java™ Tutorials; for (int i : numbers) . . . is pronounced "for each int i in numbers . . . " but you mustn't try writing
i = anything;
|
 |
fred rosenberger
lowercase baba
Bartender
Joined: Oct 02, 2003
Posts: 9956
|
|
Every single time I start a new program, I write the following:
I compile that, run it, make sure it works (it usually does), and then i go back and start adding more code. I try to ONLY ad 1-2 lines before i again re-compile, debug, test, fix, etc. it's only after I'm sure each line is right do I start adding more.
what you seem to have done is written 74 lines of code without testing. This is a recipe for disaster. If there is a problem, how do you know where to start looking?
I honestly thing the best advice would be to throw all this away, and start over. Focus on Item 1 and make sure you have it working 100% before you even attempt anything else. Note that sometimes you have to write 'throw away' code to test this stuff. For example, I might write a method that prints out all the elements in an array. Even though you'll never use it in your final program (i.e. it doesn't do anything required by your specs), you need it to verify that some other parts of your code work.
|
 |
Greg Charles
Bartender
Joined: Oct 01, 2001
Posts: 2542
|
|
Beginning programming is the hardest. Everything is new and mysterious, and so beginners just try to keep tweaking things and hope everything falls in place. Incidentally, that's how I started trying to solve a Rubik's Cube. I figured if I kept turning it long enough, I would eventually solve it. Unfortunately, that theory was only good if "eventually" meant several thousand lifetimes, which was more time than I was willing to dedicate to the project.
You've got some good advice already. Break the problem into small tasks and solve them one at a time. Also, try not to get frustrated. Everyone has to go through this stage. From what I see, you've done a lot of things right already. For example, you've done the random number exactly right as far as I can tell, and that's easy to get wrong. Something you may want to look at though: printArray() has a loop; shouldn't generateArray() have one too?
|
 |
 |
|
|
subject: i need massive help
|
|
|