Roach Trouble Help

Page 1 of 1

2 Replies - 2018 Views - Last Post: 06 April 2007 - 06:42 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=26029&amp;s=5ca8e07dbcde9507b4ffc8b969eac92a&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

#1 gamefyre2

Reputation: 0
• 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");
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

• code.rascal

Reputation: 44
• 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.

#3 Programmist

• Refactorer in Chief

Reputation: 255
• Posts: 1,843
• 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).