# Exersices

Printable View

• 05-28-2008, 03:59 PM
Zosden
Exersices
Since I think exercises are fun I will post some in this thread for you guys to try. Hopefully if this is implemented I could give points to completed projects. I hope you enjoy these. If you think you have completed the exercise then post your code for others to see. Hopefully this will become a learning experience for us all.

Exercise 1:
Use recursion to make an algorithm that computes the factorial of a certain number. For instances 2 factorial is 2. 3 factorial is 6.

Sorry for the simplicity of this first exercise, I promise I will make more challenging ways later. I will post another exercise later. If you have one that you would like me to post pm me and I will give you credit and will post it most likely.
• 05-28-2008, 04:53 PM
Chris.Brown.SPE
Recursion is fun...you did start easy.

Code:

```        static int factorial(int temp) {                 if (temp < 3) {                         return temp;                 } else {                         return temp * factorial(temp - 1);                 }         }```
• 05-28-2008, 05:56 PM
Zosden
@ Chris good job.

Exercise 2:
N number of people keeping flipping their coins until there is a odd man out. Write a program that will find the average number of tries it will take in order for their to be an odd man out. What I mean is if there is 3 people then 2 of them have to have heads and one person has to have tails. or 2 people have to have tails and one person has to have heads. Write this program so that it keeps flipping all the coins and keeps track of the number of tries. Try to find an average number. Bonus for any one who finds the average for up to 10 people.
• 05-28-2008, 06:17 PM
Chris.Brown.SPE
Why not just make it not care about the number of people? And you never mentioned how many times to test to get the average. So those are my two parameters.

Code:

```        static double flip(int people, int flips) {                 Random r = new Random();                 double average = 0;                 for (int x = 0; x < flips; x++) {                         int count = 0;                         int total = 0;                         while ((total != people - 1) & total != 1) {                                 total = 0;                                 for (int y = 0; y < people; y++) {                                         total += Math.abs(r.nextInt()) % 2;                                 }                                 count++;                         }                         average += count;                 }                 return average / flips;         }```
• 05-28-2008, 06:31 PM
Zosden
Think of it this way Chris.

Ten people are in a line they all toss their coins. They check to see if there is an odd man out. If not they all flip their coins again. The program is suppose to try and figure out the average number of flips it will take for N number of people to get an odd man out.
• 05-28-2008, 06:59 PM
Chris.Brown.SPE
And how does mine not do that?
• 05-28-2008, 07:08 PM
Zosden
how does urs check to see if there is an odd man out.
• 05-28-2008, 07:24 PM
Chris.Brown.SPE
it does a running total of random numbers (example 0 for heads 1 for tails). A 1 or # of people -1 would mean an odd man out.
• 05-28-2008, 07:33 PM
Zosden
ok good job sorry I can't really test it out cause im on my iPhone. Could you please post the average number of flips for n = 3 through n = 10.

Exercise 3:

This one should shock you guys. If you take two random integers and put them in a fraction the probability that they will be in lowest terms has to do with pi. Here's the formula pi = the square root of (6 / (number of fractions in lowest terms / number of tries)).
• 05-29-2008, 04:06 AM
Zosden
Exercise 4:

Write a program where one computer is a server and the other is a client. The server will ask the client if he wants to hear a joke then if the client sends back yes the server sends a joke. This can all be done with system.out but it needs to be networked. I would also apperiated it if people would pm me some of their ideas.
• 05-29-2008, 01:48 PM
Zosden
I will now only post an exercise once a day.

Exercise 5:

This one is a bit longer, but I thought it was fun to make. I think I'm going to leave it open and see your creativity at hand. If you guys would like I can post mine too.
Here's the exercise: make a screensaver like app that just looks like a screensaver. Bonus if you actually make it an actual screensaver.
• 06-03-2008, 04:54 AM
Zosden
Exercise 6:

Make an appilcation that has one class the implements runnable that takes in a value and increments it then prints out the new value. Then in your main method make an array of these that keep incrementing the same value.