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 2 of 2

Thread: Population class

  1. #1

    Default Population class

    I have made a class that gets the starting population, the percent increase daily and the number of days the organisms will be multiplied.
    public class popul
    {
    	//Creakte private fields
    	private double pop_organism;
    	private int percentIncrease;
    	private int days_multiply;
    	private double new_population=0;
     
     
    	//Make a contructor
    	public popul()
    	{
    		pop_organism=0;
    		percentIncrease=0;
    		days_multiply=1;
    	}
    	//take in a argument
    	public void get_popPercent_daysMult(double population,int percent,int days_mult)
    	{
    		pop_organism=population;
    		percentIncrease=percent;
    		days_multiply=days_mult;
    	}
     
    	public void calculate_increase()
    	{
     
    		for(int counter=1;counter<=days_multiply;counter++)
    		{
    			pop_organism=(pop_organism*(percentIncrease/100))+pop_organism;
    			System.out.print("Day"+ " "+counter+ " : "+pop_organism);
     
    		}
    	}
    }]

    My problem seems to lie in the public void calculate_increase method. For some aparents reason I'm getting
    day 1 : 4.0 day 2: 4.0 day 3: 4.0 day 4: 4.0 day 5: 4.0
    I have been working on this for one hour but just can't seem to figure it out! Thanks guys in advance oh and my sample input I'm using is

    pop_organism=4;
    percentIncrease=20;
    days_multiply=5;


  2. #2
    Member
    Join Date
    Oct 2013
    Location
    Manila, Philippines
    Posts
    285
    My Mood
    Amused
    Thanks
    6
    Thanked 64 Times in 61 Posts

    Default Re: Population class

    Yow,
    you have private int percentIncrease (Instance variable)
    and you also have this parameter in get_popPercent_daysMult
    the problem is: it is in Integer. It should be double since your dealing
    with a number that might consist of decimal values.


    Alternative:
    you can divide the percent by 100.0 instead of 100
    that will make your result auto cast to double data type.
    Last edited by dicdic; March 11th, 2014 at 11:33 PM.

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

    GregBrannon (March 12th, 2014)

Similar Threads

  1. Replies: 2
    Last Post: November 18th, 2012, 02:09 PM
  2. need to make basic class and implementation class (base class without void main)
    By javanewbie101 in forum Object Oriented Programming
    Replies: 1
    Last Post: September 19th, 2012, 08:03 PM
  3. create a test class (main method) to start(run) the class in Java
    By curious725 in forum Java Theory & Questions
    Replies: 5
    Last Post: August 1st, 2012, 03:21 AM
  4. Replies: 3
    Last Post: June 17th, 2012, 06:22 PM
  5. [SOLVED] Array Population via .txt File
    By Blackrabbitjack in forum What's Wrong With My Code?
    Replies: 18
    Last Post: March 19th, 2012, 10:07 AM