Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Help with a program (beginner)

  1. #1
    Junior Member gluecows's Avatar
    Join Date
    Mar 2013
    Posts
    2
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Help with a program (beginner)

    I have a question for one of my high school programming projects and i'm not really sure where to start. Here's the problem.

    "Mexico's population is 62 million and is growing at the annual rate of 7%. The United States' current population is 280 million and is growing at the annual rate of 2%. If these two countries were to maintain their current rates of growth, in how many years will Mexico's population be more than half that of the United States? Your program should answer this question."

    I decided to make it so it would run Mexico's population * 1.07^years and USA's pop * 1.02^years

    but when I run my program it says that Mexico's pop. catches up to half of the USA's in 1 year which is definitely not true. (the answer is 18 years)

    Any help is appreciated!

    import java.util.Scanner;
    public class h3q3 {
    public static void main (String [] args){
    Scanner scan = new Scanner (System.in);
     
    double mex = 62000000;
    double usa = 280000000;
    double years = 0;
     
        while(mex <= usa/2) 
        {   
            years++;
          mex = mex * Math.pow(1.07, years);
          usa = usa * Math.pow(1.02, years);
            if (mex > (usa/2));{
              break;
              }
        }       
        System.out.println ("Mexicos population is half of America's in " + years + "years ");
        }   
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,437
    Thanks
    49
    Thanked 2,217 Times in 2,190 Posts

    Default Re: Help with a program (beginner)

    Try computing the values manually for each year for a couple of years and writing the values on a piece of paper. One line for each year.
    Add a println statement to the code to print out the same values that were manually computed for each year.
    Compare the values.
    Change the program so it computes the same values for a year as were manually computed.
    If you don't understand my answer, don't ignore it, ask a question.

  3. The Following User Says Thank You to Norm For This Useful Post:

    gluecows (March 6th, 2013)

  4. #3
    Junior Member gluecows's Avatar
    Join Date
    Mar 2013
    Posts
    2
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Help with a program (beginner)

    I did what you said. It took some time but I figured out my issue and got the program to work. Thanks!

  5. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,437
    Thanks
    49
    Thanked 2,217 Times in 2,190 Posts

    Default Re: Help with a program (beginner)

    You're welcome.
    If you don't understand my answer, don't ignore it, ask a question.

  6. The Following User Says Thank You to Norm For This Useful Post:

    gluecows (March 6th, 2013)

Similar Threads

  1. beginner java program help
    By Peeboelmeebo in forum Object Oriented Programming
    Replies: 5
    Last Post: February 13th, 2013, 07:24 PM
  2. Need Help on Looping Program!! Beginner!!
    By PinkFly in forum What's Wrong With My Code?
    Replies: 8
    Last Post: March 3rd, 2012, 11:05 PM
  3. Beginner needs help with a program
    By MartinC in forum What's Wrong With My Code?
    Replies: 4
    Last Post: February 8th, 2012, 08:51 AM
  4. Need Beginner Calculator Program help!
    By theJastro in forum What's Wrong With My Code?
    Replies: 18
    Last Post: December 17th, 2011, 06:30 PM
  5. Issue with beginner program
    By suxen in forum What's Wrong With My Code?
    Replies: 3
    Last Post: April 5th, 2011, 08:55 AM