Results 1 to 12 of 12

Thread: Exersices

  1. #1
    Zosden's Avatar
    Zosden is offline Senior Member
    Join Date
    Apr 2008
    Posts
    384
    Rep Power
    7

    Default 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.
    My IP address is 127.0.0.1

  2. #2
    Join Date
    Apr 2008
    Location
    Fort Wayne, Indiana
    Posts
    62
    Rep Power
    0

    Default

    Recursion is fun...you did start easy.

    Java Code:
    	static int factorial(int temp) {
    		if (temp < 3) {
    			return temp;
    		} else {
    			return temp * factorial(temp - 1);
    		}
    	}

  3. #3
    Zosden's Avatar
    Zosden is offline Senior Member
    Join Date
    Apr 2008
    Posts
    384
    Rep Power
    7

    Default

    @ 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.
    My IP address is 127.0.0.1

  4. #4
    Join Date
    Apr 2008
    Location
    Fort Wayne, Indiana
    Posts
    62
    Rep Power
    0

    Default

    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.

    Java 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;
    	}

  5. #5
    Zosden's Avatar
    Zosden is offline Senior Member
    Join Date
    Apr 2008
    Posts
    384
    Rep Power
    7

    Default

    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.
    Last edited by Zosden; 05-28-2008 at 07:48 PM.
    My IP address is 127.0.0.1

  6. #6
    Join Date
    Apr 2008
    Location
    Fort Wayne, Indiana
    Posts
    62
    Rep Power
    0

    Default

    And how does mine not do that?

  7. #7
    Zosden's Avatar
    Zosden is offline Senior Member
    Join Date
    Apr 2008
    Posts
    384
    Rep Power
    7

    Default

    how does urs check to see if there is an odd man out.
    My IP address is 127.0.0.1

  8. #8
    Join Date
    Apr 2008
    Location
    Fort Wayne, Indiana
    Posts
    62
    Rep Power
    0

    Default

    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.

  9. #9
    Zosden's Avatar
    Zosden is offline Senior Member
    Join Date
    Apr 2008
    Posts
    384
    Rep Power
    7

    Default

    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)).
    My IP address is 127.0.0.1

  10. #10
    Zosden's Avatar
    Zosden is offline Senior Member
    Join Date
    Apr 2008
    Posts
    384
    Rep Power
    7

    Default

    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.
    My IP address is 127.0.0.1

  11. #11
    Zosden's Avatar
    Zosden is offline Senior Member
    Join Date
    Apr 2008
    Posts
    384
    Rep Power
    7

    Default

    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.
    My IP address is 127.0.0.1

  12. #12
    Zosden's Avatar
    Zosden is offline Senior Member
    Join Date
    Apr 2008
    Posts
    384
    Rep Power
    7

    Default

    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.
    My IP address is 127.0.0.1

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •