Results 1 to 3 of 3
  1. #1
    _inase is offline Member
    Join Date
    Jan 2011
    Posts
    13
    Rep Power
    0

    Default How do I get my while loop to execute?

    This is a program for computing a close enough value for the half-life of radioactive carbon-14.
    The loop should terminate if the absolute value of the differences is within an epsilon of 1.0E-7.
    When I run the program nothing happens. What's wrong with my while loop?
    The instructions says that the while loop should be controlled by a comparison of half of the initial amount and the computed amount remaining.

    import java.util.*;
    public class Lab4Part2
    {
    public static void main(String[] args)
    {
    final double EPSILON = 1.0E-7;
    final double INITIAL_AMOUNT = 2.0E-5;
    final double DECAY = 1.14E-4;

    double amount = INITIAL_AMOUNT;
    double amountRemaining;
    int year = 0;

    while (EPSILON < Math.abs(2.0E-5 / 2) - amountRemaining))
    {
    year++;
    amountRemaining = amount - (DECAY / 100) * amount;
    }
    System.out.println("The approximate value for the half-life of carbon-14 is " + year + " years.");
    }

    }
    Last edited by _inase; 02-27-2011 at 08:15 PM.

  2. #2
    berkeleybross's Avatar
    berkeleybross is offline Senior Member
    Join Date
    Feb 2010
    Location
    England
    Posts
    187
    Rep Power
    5

    Default

    It sounds like you've gone into an infinite loop.

    To analyse what's happening, you should put a few printouts inside the loop.
    For instance
    Java Code:
    while (EPSILON < Math.abs(2.0E-5 / 2) - amountRemaining))
    {
        year++;
        amountRemaining = amount - (DECAY / 100) * amount;	
        System.out.println (amountRemaining);
    }
    This should give you a clue as to why the loop isn't working.

    Also, you seem to have two right parentheses after the amountRemaining in the while condition. I cant see the matching left parentheses - copying mistake? It shouldnt compile like this.

    berkeleybross

  3. #3
    subith86 is offline Senior Member
    Join Date
    Jan 2011
    Location
    Bangalore, India
    Posts
    102
    Rep Power
    0

    Default

    Are you sure nothing happened? I think compiler should have thrown an error here because "amountRemaining" is not initialized:
    Java Code:
    while (EPSILON < Math.abs(2.0E-5 / 2) - amountRemaining))
    Well, coming to the logic of the code, this line doesn't make any sense. In every loop, amountRemaining will have no change at all because on the right hand side of this assignment, all are constants.
    Java Code:
    amountRemaining = amount - (DECAY / 100) * amount;
    I have modified it a little. See if this is the result that you expect.

    Java Code:
    		final double EPSILON = 1.0E-7;
    		final double INITIAL_AMOUNT = 2.0E-5;
    		final double DECAY = 1.14E-4;
    
    		double amount = INITIAL_AMOUNT;
    		[B][I][COLOR="YellowGreen"]//double amountRemaining = amount;[/COLOR][/I][/B]
    		int year = 0;
    
    		while (EPSILON < Math.abs((2.0E-5/2) - [B][COLOR="Red"]amount[/COLOR][/B]))
    		{
    			year++;
    			[COLOR="red"][B]amount[/B][/COLOR] = amount - (DECAY / 100) * amount;	
    		}
    		System.out.println("The approximate value for the half-life of carbon-14 is " + year + " years.");

Similar Threads

  1. JTextField loop 2x for-loop WEIRD!
    By Streetproject in forum AWT / Swing
    Replies: 2
    Last Post: 02-16-2011, 05:46 PM
  2. jar file won't execute.
    By Splat in forum New To Java
    Replies: 5
    Last Post: 10-22-2009, 05:26 AM
  3. Execute() throws an NPE
    By mjz in forum JDBC
    Replies: 0
    Last Post: 08-06-2009, 02:25 AM
  4. Execute Jar file
    By barney in forum New To Java
    Replies: 1
    Last Post: 08-07-2007, 07:13 AM
  5. why does not execute this?
    By aleplgr in forum New To Java
    Replies: 6
    Last Post: 08-01-2007, 11:37 AM

Posting Permissions

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