aspose file tools*
The moose likes Beginning Java and the fly likes algoritem writing problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "algoritem writing problem " Watch "algoritem writing problem " New topic
Author

algoritem writing problem

Yuri Paliy
Greenhorn

Joined: Feb 06, 2008
Posts: 3
I am new to Java for 4 months, unfortunately we had a 2 month strike at school and now we need to catch up on "simple" things on our own. the coding seems to be right but I may be wrong but its the algorithm the one i find difficult for me. a few ground rules on how to write action (function) in an algorithm would be very helpful



any correction to the code would be welcomed too, thank you.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39436
    
  28
It worked nicely when I tried it, but I can't read Hebrew and you haven't told us what it ought to do, so I don't know whether it is working.

Your putToOrder method ought only to take one array at a time; you are doing the same thing twice. It would be better to have a method which does whatever it does (I do think I know what!) to one array, and call it twice.
Yuri Paliy
Greenhorn

Joined: Feb 06, 2008
Posts: 3
Oh yes i have forgot to say that it does, so here it is:

the program creates two arrays of 20 and 15 and randomly selects numbers from 1 to 100 and puts them in the arrays.

1. arrange the arrays from smallest to biggest.
2. create an array of all the numbers within the two random arrays (no double numbers are allowed to appear) and print it.
3. crate an array of the common numbers between the groups and print it.
4. arrange the arrays from 2,3 (remove the zeros between the numbers).

thanks for the advice i understand how its better to send both arrays in separate.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39436
    
  28
Well, I had guessed that the first method represents a bubble sort.

The fullBucket method counts whether a particular number appears in an array, and sets its value to 1 (it starts with 0 then ++). Then if the same number appears in the other array, it sets the number to 2. Then if it isn't 0 you print it. That sounds the same as a sort of set union, which appears correctly to implement what you want.

The emptyBucket method seems to print out those members in both arrays similarly, a sort of set intersection.

And I think the arrange method does actually get rid of the 0s. Only you never print it out.

I would have preferred to see the arrays set up as fields in the class and the methods not static. Better object-oriented style.
I would suggest you would get a better mark as a beginner by always putting {} round the bodies of your for-loops and if blocks. Apart from that it is good.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39436
    
  28
. . . but your bubble sort method will have to be static if you call it twice.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: algoritem writing problem