2 Replies - 1489 Views - Last Post: 06 April 2007 - 06:42 AM Rate Topic: -----

#1 gamefyre2  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 05-April 07

Roach Trouble Help

Posted 05 April 2007 - 09:11 PM

Hello everyone I'm having a little problem trying to figure this problem out. I have come up with the code that tells you how many roaches (or bugs) will fill up your house in a certain amount of time. Now, I have to figure out how to change the program to when it hits 12,000 bugs the program will stop. Please help or comment. The code is:


import java.util.*;

/**
 Program to calculate how long it will take a population of
 roaches to completely fill a house from floor to ceiling.
*/
public class BugTrouble
{
	public static final double GROWTH_RATE = 0.95;//95% per week
	public static final double ONE_BUG_VOLUME = 0.002;//cubic feet
	public static void main(String[] args)
	{
		System.out.println("Enter the total volume of your house");
		System.out.print("in cubic feet: ");
		Scanner keyboard = new Scanner(System.in);
		double houseVolume = keyboard.nextDouble( );
		System.out.println("Enter the estimated number of");
		System.out.print("roaches in your house: ");
		int startPopulation = keyboard.nextInt( );
		int countWeeks = 0;
		double population = startPopulation;
		double totalBugVolume = population*ONE_BUG_VOLUME;
		while (totalBugVolume < houseVolume)
		{
			population = population + (GROWTH_RATE*population);
			totalBugVolume = population*ONE_BUG_VOLUME;
			countWeeks++;
		}
		System.out.println("Starting with a roach population of "
											 + startPopulation);
		System.out.println("and a house with a volume of "
								+ houseVolume + " cubic feet,");
		System.out.println("after " + countWeeks + " weeks,");
		System.out.println("the house will be filled");
		System.out.println("floor to ceiling with roaches.");
		System.out.println("There will be " + (int)population + " roaches.");
		System.out.println("They will fill a volume of "
						  + (int)totalBugVolume + " cubic feet.");

		System.out.println("Better call Debugging Experts Inc.");
	}
}


Is This A Good Question/Topic? 0
  • +

Replies To: Roach Trouble Help

#2 1lacca  Icon User is offline

  • code.rascal
  • member icon

Reputation: 44
  • View blog
  • Posts: 3,822
  • Joined: 11-August 05

Re: Roach Trouble Help

Posted 06 April 2007 - 04:03 AM

Augment the while (totalBugVolume < houseVolume) condition, so taht it checks if the population is above the limit you've specified - or depending on your goal, it might be more appropiate to put it into an if statement right after the line you increase the population, and break out of the while loop, if it becomes true.
Was This Post Helpful? 0
  • +
  • -

#3 Programmist  Icon User is offline

  • CTO
  • member icon

Reputation: 252
  • View blog
  • Posts: 1,833
  • Joined: 02-January 06

Re: Roach Trouble Help

Posted 06 April 2007 - 06:42 AM

Add a "bug bomb" button, just for fun, that kills a certain percentage of the roaches. Even cooler would be to show a line graph shows the population growing and shrinking (bug bomb). :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1