Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

i need massive help

 
jay bob
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12081
29
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Wouter Oet
Saloon Keeper
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
jay bob
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 20880
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Take a look at the java.util.Random class.

Henry
 
Campbell Ritchie
Sheriff
Posts: 48363
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12081
29
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 2984
12
Firefox Browser IntelliJ IDE Java Mac Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic