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

Need help with Java Coding data.txt 4 100 50 9 1 100 50 9 2 100 50 9 3 100 50 9

ID: 3851213 • Letter: N

Question

Need help with Java Coding

data.txt

4
100 50 9 1
100 50 9 2
100 50 9 3
100 50 9 8

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 input: Input consists of a number of n (n 1000 independent problems to be solved. Each following line contains four positive integers E s

Explanation / Answer

make sure your input file is available to StopInsects.java

and make sure you name your input file as "data.txt"

import java.io.*;
public class StopInsects
{
   public static void main(String a[])
   {
       try{
           //reads contents of file data.txt
           File file = new File("data.txt");
           BufferedReader br = new BufferedReader(new FileReader(file));
           //reading no of test cases will be there in data.txt(number mentioned in first line of data.txt)
           int testCases = Integer.parseInt(br.readLine());
           for(int i=0;i<testCases;i++){
               //reading line by line contents of data.txt
               String line = br.readLine();
               //splitting into separate values which are inputs of testcase i,e s,p,r,x
               String ar[] = line.split(" ");
               int s=Integer.parseInt(ar[0]),p=Integer.parseInt(ar[1]),r=Integer.parseInt(ar[2]),x=Integer.parseInt(ar[3]);
              
               int loop=1;
               long females=s,males=s;
               //looping till we reach x th week
               while(loop<x)
               {
                   long pFemale = calculatePercentage(females,p);
                   long pMale = calculatePercentage(males,p);
                   //killing insects of p percent in males and females
                   females-=pFemale;
                   males-=pMale;
                   //reproduction
                   long tempFem = females;
                   females+=(females*((int)(r/2)));
                   males+=(tempFem*((int)(r/2)));
                  
                   if(r%2!=0)
                   {
                       females+=tempFem;
                   }
                   //System.out.println(females+" "+males);
                   loop++;
               }
               System.out.println("Female="+females+" Male="+males);
           }
       }
       catch(Exception e){
           e.printStackTrace();
       }
   }
   //calculates percentage of population
   public static long calculatePercentage(long population,int percentage)
   {
       return (long)population*percentage/100;
   }
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote