Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

jAVA. Stop the Stranger Insects We used to have very clean classroom. However, d

ID: 3850736 • Letter: J

Question

 jAVA. Stop the Stranger Insects  We used to have very clean classroom. However, due to the unusual weather this year, some strange insects (we call them Java)  were spotted in this room (do not be scared, they do nothing harmful, except disgusting). Female Java can give birth to about  30 baby Java each week. These mature quickly, and within a week can have litters of their own. And so, a few weeks later, Java  were overrunning the classroom and everyone feel disgusting. One of our students finds a special way to kill these insects by  writing java program. His program kills the insects as follows:  It assumes there are E female Java and E male Java in a week, his program kills a percentage p of all Java, males and females in equal proportion (if after this slaughter you end up with 30.7 males and 32.8 females, round these down). It assumes that every surviving  female will give birth to R baby Java within a week, and if R is odd, the odd baby Java will be a female. These Java will reach  reproduction age by next week and will be ready to reproduce.   Ultimately, we would like to be able to enter E, p, and R parameters into the model, and get an answer of how many Java are expected  to be alive in X weeks from the start of the model. For example, if there were 100 female and 100 male Java in the week 1.  the query for week one will return 200, no matter what p and R values are. If the query is for week 2, and the parameters  are p=50, R=9, there will be 50 females and 50 males surviving into week 2, and each of those 50 females will bear 5 female baby Java  and 4 male baby Java that week. 50 by next week, there will be 550 Java: 300 females and 250 males. In week 3, 150 surviving female Java  will again bear 5 female and 4 male baby Java to add to the population. A sample solution is provided for testing.  lnput: Input consists of a number of n (n <= 1000) independent problems to be solved. Each following line contains four positive integers:  E s<=1000, p<=100, R<=50 and X<=10  Sample input: 4 100 50 9 1 100 50 9 2 100 50 9 3 100 50 9 8  Output: Output should consist of n lines. Each line is a solution to a single problem from the input. Each line should be a single e positive integer:  the number of Java alive in the week X from the start of the model. The number may be large, but will not exceed 1,000,000,000.   Sample output: Female=100  Male=100  Female=300  Male=250  Female=900  Male=725  Female=218700  Male=174960  

Explanation / Answer

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package chegg.june;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

/**
*
* @author Sam
*/
public class StrangerInsect {
    public static void calculate(int e, int p, int r, int x) {
        int male = e;
        int female = e;
        int newFemale;
        int newMale;
      
        for (int i = 1; i < x; i++) {
            //System.out.println("male was:"+male+" tfemale was:"+female);
            male -= (int)(male *(double)p/100);
            female -= (int)(female *(double)p/100);
            int maleChild = r/2;
            int femaleChild = r - maleChild;
          
            //System.out.println("male is:"+male+" tfemale is:"+female);
            //System.out.println("maleChild is:"+maleChild+" tfemaleChild is:"+femaleChild);
            newFemale = female * femaleChild;
           newMale = female * maleChild;
          
            female += newFemale;
            male += newMale;
        }
        System.out.println("Female="+female+" Male="+male);
    }
  
    public static void main(String[] args) throws FileNotFoundException {
        Scanner sc = new Scanner(new File("testFile"));
        String x = sc.nextLine();
        System.out.println(x);
        int n = Integer.parseInt(x);
        for (int i = 0; i < n; i++){
            int E = sc.nextInt();
            int p = sc.nextInt();
            int R = sc.nextInt();
            int X = sc.nextInt();
            calculate(E, p, R, X);
        }
      
    }
}


Your code is ready. Hope you like it.