We hope you enjoy your visit.

You're currently viewing our forum as a guest. This means you are limited to certain areas of the board and there are some features you can't use. If you join our community, you'll be able to access member-only sections, and use many member-only features such as customizing your profile, sending personal messages, and voting in polls. Registration is simple, fast, and completely free.


Join our community!


If you're already a member please log in to your account to access all of our features:

Username:   Password:
Add Reply
Java Programmers please review this
Topic Started: Jan 8 2005, 02:18 PM (521 Views)
Deleted User
Deleted User

we have this program called javagods in my class that randomly pairs everyone but cant have a duplicate person.

and when i saw its source i was appalled at it and said i could write a better code. turns out rewriting it is rather difficult.

here is the source i have come up with and please review it and make suggestions/comments if u have any advice on how to make it better.

Code:
 

import java.util.Random;

public class JavaGods
{
   public static void main (String[] args)
   {
       Random generator = new Random();
       int zero = 0;
       int num[] = new int [22];

       String[] name = {"Alexander", "Benca", "Bremer", "Burns", "Church", "Daily", "Gregus", "Hanson", "Keen", "Kiel", "Kisunzu", "Kramarczyk", "Lawson", "Lilly", "Maheshwari", "Rahkhumov", "Roy", "Sebastian", "Seelye", "Syed", "Wild", "Windsor"};
               
       for(int start = 0; start < 22; start++) num[start] = 0;

       //num[zero] = generator.nextInt(22)+1;
       for(int x = 0; x < 22; x++)
       {
           num[x] = generator.nextInt(22)+1;
       
           if(x > 0)
           {
            for(int y= (x-1); y >= 0; y--)
            {
                   while(num[x] == num[y])
                   {
                         num[x] = generator.nextInt(22)+1;
                   }
                }
           }
           else
           {
               for(int z= (x+1); z <= 22; z++)
               {
                   while(num[x] == num[z])
                   {
                         num[x] = generator.nextInt(22)+1;
                   }
                }
            }
           
           System.out.println(name[num[x]]);
        }
     
    }
}


the last line to print the names was just to check if everyone was used and no duplicates were found. this is just the main part of the code that is needed to run the pairing, the actual last part i can whip up later
Quote Post Goto Top
 
kingy
1 in 10 people understands binary, the other 1 doesn't
[ *  *  *  *  *  * ]
picking straws is so much simpler...

as for the java it looks alright...i havent seen anything i could simplify. but then again i only just learnt java
Offline Profile Quote Post Goto Top
 
Deleted User
Deleted User

thx,
Quote Post Goto Top
 
Seth
Member Avatar
I has a pony
[ *  *  *  *  *  *  *  *  * ]
:'( Java makes me cry, so many lines to do such a simple task. But it looks like you've done it well.

The one thing I would say is that it is very hardcoded. You have the number 22 everywhere. Why not assign the array of names, then define a variable counting the number of array entries. That way, if a new person joins the class, no code editing required.
Offline Profile Quote Post Goto Top
 
kingy
1 in 10 people understands binary, the other 1 doesn't
[ *  *  *  *  *  * ]
yeah but other than that its fine
Offline Profile Quote Post Goto Top
 
dirty-devil
the power of christ compelles you !!!
[ *  *  *  * ]
can we use this in our forums ?
Offline Profile Quote Post Goto Top
 
kingy
1 in 10 people understands binary, the other 1 doesn't
[ *  *  *  *  *  * ]
lol...no

its not even a complete code, and its java, not javascript

i suppose a javascript equivilant would be possible
Offline Profile Quote Post Goto Top
 
dirty-devil
the power of christ compelles you !!!
[ *  *  *  * ]
whats the difference

java javascript
Offline Profile Quote Post Goto Top
 
kingy
1 in 10 people understands binary, the other 1 doesn't
[ *  *  *  *  *  * ]
webteacher
 
Actually, the 2 languages have almost nothing in common except for the name. Although Java is technically an interpreted programming language, it is coded in a similar fashion to C++, with separate header and class files, compiled together prior to execution. It is powerful enough to write major applications and insert them in a web page as a special object called an "applet." Java has been generating a lot of excitment because of its unique ability to run the same program on IBM, Mac, and Unix computers. Java is not considered an easy-to-use language for non-programmers.

Javascript is much simpler to use than Java. With Javascript, if I want check a form for errors, I just type an if-then statement at the top of my page. No compiling, no applets, just a simple sequence.
Offline Profile Quote Post Goto Top
 
Hero_of_time
I love Emily :wub:
[ *  *  *  * ]
dirty-devil
January 8, 2005 05:11 PM
whats the difference

java javascript

What is JavaScript?
JavaScript is a programming language that originated with Netscape. It's very easy to use and makes your Web site more interactive to visitors.

What is Java?
Introduced by Sun Microsystems in 1995, Java is a programming language that was designed for the internet environment. It can be used to write programs which can be embedded in your Web site.

What is the difference between JavaScript and Java?
Java creates stand alone applications, like applets, JavaScript does not. JavaScript was developed to interact with the HTML of a Web site.
Offline Profile Quote Post Goto Top
 
korn_mosher_4life-ZNS
the korn mosher 4life
[ *  *  *  * ]
Seth
January 8, 2005 02:32 PM
:'( Java makes me cry, so many lines to do such a simple task. But it looks like you've done it well.

so true, it hurts my eyes trying to read all these damn lines of it. Just to do something I could do in javascript. Anywho the hardcoded part is true, do what seth said it would help. Other then that looks good.


and as for me, java is easier then javascript.
Offline Profile Quote Post Goto Top
 
Rory
Member Avatar
i;m a mess
[ *  *  *  *  *  *  * ]
i hate java, but have to learn it. It is similar to php, but at the same time, not.... if that makes any sense.
Offline Profile Quote Post Goto Top
 
Deleted User
Deleted User

well....its "almost" correct. i could do it by setting 22 variables...but i dont want to. i just wanted to make a friggen array...but no their is no

Code:
 
range(int index, int index)[code]

all i ask is to be able to set a range within an array such that it checks all of it rather than having this as the code:

[code]
for(int y= (x-1); y >= 0; y--)
           {
                  while(num[x] == num[y])
                  {
                        num[x] = generator.nextInt(22)+1;
                  }
               }

and the second part i could do it like this:

Code:
 

while(num[x] == range(num[], 0, (x-1)))num[x] = generator.nextInt(22)+1;
while(num[x] == range(num[], (x+1), 22))num[x] = generator.nextInt(22)+1;


i mean what the hell!? i swear im e-mailing Sun Microsystems tonight to have them update their API and update this to the JRE and SDK

note: the use would be range(object, int index, int end)
Quote Post Goto Top
 
kingy
1 in 10 people understands binary, the other 1 doesn't
[ *  *  *  *  *  * ]
Iostream
January 12, 2005 10:51 PM
i could do it by setting 22

i think seth meant make 22 a variable, since it comes up so much
Offline Profile Quote Post Goto Top
 
1 user reading this topic (1 Guest and 0 Anonymous)
« Previous Topic · Technology Chat · Next Topic »
Add Reply