Jump to content

  • Log In with Google      Sign In   
  • Create Account

Awesome job so far everyone! Please give us your feedback on how our article efforts are going. We still need more finished articles for our May contest theme: Remake the Classics

#ActualByteTroll

Posted 12 October 2012 - 12:14 PM

I am not sure if you are still having issues with this assignment, but I got bored today and figured I would lend a final hand.  I recoded my example in Java.  I generally don't do peoples homework for them, but you have at least given it a shot.  For future reference, however, I would look closely at what I have done and learn.  Hope this helps.

import java.util.Random;
public class flip
{
	public static void main(String[] args)
	{
		//Since these values never get changed, it is good practice to make them
		//final.
		final double start = 0;
		final double end = 1;
	  
		//Declare this as an instantiation of Random, not a double.
		Random random = new Random();
	  
		//Set the initial value of the result variable.  Notice how I use the
		//"random" object.  Intead of using a double that has an initial value,
		//I generate a new value ("random.nextDouble()")
		double result = start + (random.nextDouble() * (end - start));
	  
		System.out.println(result);
	  
		//First result check.
		if(result <= 0.5)
		{
			System.out.println("head");
		}
		else
		{
			System.out.println("tail");
		}
	  
		//Generate a new value and shove it in the result variable.  Same thing
		//as the first call.  I use random to generate a new value.  If I didn't
		//and just used a double, the double variable would still have the same
		//value I set in the beginning.
		result = start + (random.nextDouble() * (end - start));
	  
		//Second result check.
		if(result <= 0.5)
		{
			System.out.println("head");
		}
		else
		{
			System.out.println("tail");
		}
	  
		//Generate a new value and shove it in the result variable.
		result = start + (random.nextDouble() * (end - start));
	  
		//Third result check.
		if(result <= 0.5)
		{
			System.out.println("head");
		}
	  
		//Generate a new value and shove it in the result variable.
		result = start + (random.nextDouble() * (end - start));
	  
		//Fourth result check.
		if(result <= 0.5)
		{
			System.out.println("head");
		}
		else
		{
			System.out.println("tail");
		}
	  
		//Generate a new value and shove it in the result variable.
		result = start + (random.nextDouble() * (end - start));
	  
		//Fifth result check.
		if(result <= 0.5)
		{
			System.out.println("head");
		}
		else
		{
			System.out.println("tail");
		}
	  
		//Generate a new value and shove it in the result variable.
		result = start + (random.nextDouble() * (end - start));
	  
		//Sixth result check.
		if(result <= 0.5)
		{
			System.out.println("head");
		}
		else
		{
			System.out.println("tail");
		}
	  
		//Generate a new value and shove it in the result variable.
		result = start + (random.nextDouble() * (end - start));
	  
		//Seventh result check.
		if(result <= 0.5)
		{
			System.out.println("head");
		}
		else
		{
			System.out.println("tail");
		}
	  
		//Generate a new value and shove it in the result variable.
		result = start + (random.nextDouble() * (end - start));
	  
		//Eighth result check.
		if(result <= 0.5)
		{
			System.out.println("head");
		}
		else
		{
			System.out.println("tail");
		}
	  
		//Generate a new value and shove it in the result variable.
		result = start + (random.nextDouble() * (end - start));
	  
		//Ninth result check.
		if(result <= 0.5)
		{
			System.out.println("head");
		}
		else
		{
			System.out.println("tail");
		}
	  
		//Generate a new value and shove it in the result variable.
		result = start + (random.nextDouble() * (end - start));
	  
		//Tenth result check.
		if(result <= 0.5)
		{
			System.out.println("head");
		}
		else
		{
			System.out.println("tail");
		}
	}
}

#1ByteTroll

Posted 12 October 2012 - 12:10 PM

I am not sure if you are still having issues with this assignment, but I got bored today and figured I would lend a final hand.  I recoded my example in Java.  I generally don't do peoples homework for them, but you have at least given it a shot.  For future reference, however, I would look closely at what I have done and learn.  Home this helps.

import java.util.Random;
public class flip
{
    public static void main(String[] args)
    {
	    //Since these values never get changed, it is good practice to make them
	    //final.
	    final double start = 0;
	    final double end = 1;
	   
	    //Declare this as an instantiation of Random, not a double.
	    Random random = new Random();
	   
	    //Set the initial value of the result variable.  Notice how I use the
	    //"random" object.  Intead of using a double that has an initial value,
	    //I generate a new value ("random.nextDouble()")
	    double result = start + (random.nextDouble() * (end - start));
	   
	    System.out.println(result);
	   
	    //First result check.
	    if(result <= 0.5)
	    {
		    System.out.println("head");
	    }
	    else
	    {
		    System.out.println("tail");
	    }
	   
	    //Generate a new value and shove it in the result variable.  Same thing
	    //as the first call.  I use random to generate a new value.  If I didn't
	    //and just used a double, the double variable would still have the same
	    //value I set in the beginning.
	    result = start + (random.nextDouble() * (end - start));
	   
	    //Second result check.
	    if(result <= 0.5)
	    {
		    System.out.println("head");
	    }
	    else
	    {
		    System.out.println("tail");
	    }
	   
	    //Generate a new value and shove it in the result variable.
	    result = start + (random.nextDouble() * (end - start));
	   
	    //Third result check.
	    if(result <= 0.5)
	    {
		    System.out.println("head");
	    }
	   
	    //Generate a new value and shove it in the result variable.
	    result = start + (random.nextDouble() * (end - start));
	   
	    //Fourth result check.
	    if(result <= 0.5)
	    {
		    System.out.println("head");
	    }
	    else
	    {
		    System.out.println("tail");
	    }
	   
	    //Generate a new value and shove it in the result variable.
	    result = start + (random.nextDouble() * (end - start));
	   
	    //Fifth result check.
	    if(result <= 0.5)
	    {
		    System.out.println("head");
	    }
	    else
	    {
		    System.out.println("tail");
	    }
	   
	    //Generate a new value and shove it in the result variable.
	    result = start + (random.nextDouble() * (end - start));
	   
	    //Sixth result check.
	    if(result <= 0.5)
	    {
		    System.out.println("head");
	    }
	    else
	    {
		    System.out.println("tail");
	    }
	   
	    //Generate a new value and shove it in the result variable.
	    result = start + (random.nextDouble() * (end - start));
	   
	    //Seventh result check.
	    if(result <= 0.5)
	    {
		    System.out.println("head");
	    }
	    else
	    {
		    System.out.println("tail");
	    }
	   
	    //Generate a new value and shove it in the result variable.
	    result = start + (random.nextDouble() * (end - start));
	   
	    //Eighth result check.
	    if(result <= 0.5)
	    {
		    System.out.println("head");
	    }
	    else
	    {
		    System.out.println("tail");
	    }
	   
	    //Generate a new value and shove it in the result variable.
	    result = start + (random.nextDouble() * (end - start));
	   
	    //Ninth result check.
	    if(result <= 0.5)
	    {
		    System.out.println("head");
	    }
	    else
	    {
		    System.out.println("tail");
	    }
	   
	    //Generate a new value and shove it in the result variable.
	    result = start + (random.nextDouble() * (end - start));
	   
	    //Tenth result check.
	    if(result <= 0.5)
	    {
		    System.out.println("head");
	    }
	    else
	    {
		    System.out.println("tail");
	    }
    }
}

PARTNERS