Question

Write a program to help answer questions like the following: Suppose the species Klingon ox has...

Write a program to help answer questions like the following: Suppose the species Klingon ox has a population of 100 and a growth rate of 15 percent, and the species elephant has a population of 10 and a growth rate of 35 percent. How many years will it take for the elephant population to exceed the Klingon ox population? You can assume that this will happen within 10 years.

Use the version of the class Species from Sakai’s Week 7 Source Code folder as your starting point – download it to your Java programs folder. Your program will ask for the data on both species and will respond by telling you how many years it will take for the species that starts with the lower population to outnumber the species that starts with the higher population. The two species may be entered in any order, so you’ll have to figure out which is which; you might assign them to lower and upper Species variables. You can assume the populations are different to start.

The main method in this program has been set up for you to create two Species objects and run a loop with increasing future year numbers (add 1 each time) until the lower population species outnumbers the higher population species, using the predictPopulation() method. Again, you can assume that this will happen within 10 years or not at all.

Note that it’s possible the species with the smaller population will never outnumber the other species. In this case, your program should just display a suitable message.

Hint: If the species with the lower population has a growth rate less than or equal to the species with the higher population then it will never outnumber the other species, otherwise it must outnumber it at some year in the future. You may assume that both species have growth rates greater than 0 for this exercise.

Your program must output one of the following two statements at the end; here and are the names of the two species, and is the number of years it takes for the lower one to overtake the higher one:

Species <lower> will not overtake " <higher>.
Species <lower> will overtake <higher> in <years> years.

IN JAVA:

/**
File name: YearsToOvertake.java

This program does the following:
Reads in the names, populations and growth rates for two species
and calculates the number of years it will take the one
with the lower initial population to overtake the other
or prints a message that it will never have a higher population).

Preconditions: none.

Postconditions:
Displays either the number of years it takes for the species with the
lower population to have a population greater than the other,
or a message that it will not exceed the other
species' population within ten years.
*/

public class YearsToOvertake
{
public static void main(String[] args)
{
Species s1 = new Species();
Species s2 = new Species();
Species lower = new Species(); // has lower initial population
Species higher = new Species(); // has higher initial population

// Read in two species

s1.readInput();
s2.readInput();

// Determine which has the lower initial population - if they are
// the same, assign the 1st species to "lower" and the 2nd to "higher"

/* your code to do that goes here */

// In a loop, find number of years it takes the species with the
// lower initial population to overtake the other one.
// Here are some useful variables for that loop:

int years = 1;
int nextPopulationLower;
int nextPopulationHigher;

/* write the appropriate while loop here - only go to 10 years */


years--; // Adjust years to correct value (it is 1 too many on exit from while-loop)

// If years == 10 "lower" may or may not have overtaken "higher"
// so you must do a final check.

/* write that check here and print out the appropriate statement */


}
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1
import java.util.Scanner;

/**
 * File name: YearsToOvertake.java
 * 
 * This program does the following: Reads in the names, populations and growth
 * rates for two species and calculates the number of years it will take the one
 * with the lower initial population to overtake the other or prints a message
 * that it will never have a higher population).
 * 
 * Preconditions: none.
 * 
 * Postconditions: Displays either the number of years it takes for the species
 * with the lower population to have a population greater than the other, or a
 * message that it will not exceed the other species' population within ten
 * years.
 */

class Species {
    private String name;
    private int population;
    private double growthRate;
    public Scanner in = new Scanner(System.in);

    public void readInput() {
        System.out.println("What is the species’ name?");
        name = in.nextLine();
        System.out.println("What is the population of the species?");
        population = in.nextInt();
        while (population < 0) {
            System.out.println("Population cannot be negative.");
            System.out.println("Reenter population:");
            population = in.nextInt();
        }
        System.out.println("Enter growth rate (percent increase per year):");
        growthRate = in.nextDouble();
    }

    public void writeOutput() {
        System.out.println("Name = " + name);
        System.out.println("Population = " + population);
        System.out.println("Growth rate = " + growthRate + "%");
    }

    /*******************************************************
     * Precondition: years is a nonnegative number. Returns the projected population
     * of the calling object after the specified number of years.
     *******************************************************/
    public int projectedPopulation(int years) {
        double populationAmount = population;
        int count = years;

        while ((count > 0) && (populationAmount > 0)) {
            populationAmount = (populationAmount + (growthRate / 100) * populationAmount);
            count--;
        }
        if (populationAmount > 0)
            return (int) populationAmount;
        else
            return 0;
    }

    public void set(String newName, int newPopulation, double newGrowthRate) {
        name = newName;
        if (newPopulation >= 0)
            population = newPopulation;
        else {
            System.out.println("ERROR: using a negative population.");
            System.exit(0);
        }
        growthRate = newGrowthRate;
    }

    public String getName() {
        return name;
    }

    public int getPopulation() {
        return population;
    }

    public double getGrowthRate() {
        return growthRate;
    }

    public boolean equals(Species otherObject) {
        return ((name.equalsIgnoreCase(otherObject.name)) && (population == otherObject.population)
                && (growthRate == otherObject.growthRate));
    }
}

public class YearsToOvertake {
    public static void main(String[] args) {
        Species s1 = new Species();
        Species s2 = new Species();
        Species lower = new Species(); // has lower initial population
        Species higher = new Species(); // has higher initial population

        // Read in two species
        s1.readInput();
        s2.readInput();

        // Determine which has the lower initial population - if they are
        // the same, assign the 1st species to "lower" and the 2nd to "higher"
        if (s1.getPopulation() > s2.getPopulation()) {
            lower = s2;
            higher = s1;
        } else {
            lower = s1;
            higher = s2;
        }

        /* your code to do that goes here */

        // In a loop, find number of years it takes the species with the
        // lower initial population to overtake the other one.
        // Here are some useful variables for that loop:

        int years = 1;
        int nextPopulationLower = lower.getPopulation();
        int nextPopulationHigher = higher.getPopulation();

        /* write the appropriate while loop here - only go to 10 years */
        while (years <= 10 && (nextPopulationLower < nextPopulationHigher)) {
            nextPopulationLower = lower.projectedPopulation(years);
            nextPopulationHigher = higher.projectedPopulation(years);

            years++;
        }

        years--; // Adjust years to correct value (it is 1 too many on exit from while-loop)

        // If years == 10 "lower" may or may not have overtaken "higher"
        // so you must do a final check.

        /* write that check here and print out the appropriate statement */

        if (nextPopulationLower > nextPopulationHigher) {
            System.out.println("Species " + lower.getName() + " will overtake " + 
                    higher.getName() + " in " + years + " years.");
        } else {
            System.out.println("Species " + lower.getName() + " will not overtake " + 
                    higher.getName() + ".");            
        }
    }
}

E Console & ㄨ吮ㄧ泳SE Egl E) !ゴ曰▼ R ▼ D AnimalTestava LinkedListjava YearsToOvertake java <terminated> YearsToOvertake Java Application] CAProgram FilesJava\idk1.8.0 73in105 106 107 198 109 110 if (s1.getPopulation() > s2.getPopulationO) lower -s2; higher - s1; What is the species name? sad What is the population of the species? 100 Enter growth rate (percent increase per year): 15 What is the species name? else { lower s1; higher - s2; 112 your code to do that goes here What is the population of the species? 30 114 115 116 // In a loop, find number of years it takes the species with the /I lower initial population to overtake the other one // Here are some useful variables for that loop Enter growth rate (percent increase per year): Species sasa will overtake sad in 8 years 118 int years1; int nextPopulationLowerlower.getPopulation); int nextPopulationHigher- higher.getPopulation); 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 /* write the appropriate while loop here - only go to 10 years while (years 10 && (nextPopulationLower<nextPopulationHigher)) [ nextPopulationLower-lower.projectedPopulation (years); nextPopulationHigher- higher projectedPopulation(years); years++ years-- // Adjust years to correct value (it is 1 too many on exit from while-loop) /I If years1 lower may or may not have overtaken higher // so you must do a final check /* write that check here and print out the appropriate statement if (nextPopulationLower > nextPopulationHigher) l else ( System.out.println( Species lower.getName will overtake higher.getName ) +in yearsyears.) System.out.println( Species + lower.gete will not overtake + higher.getName ) .);

Hi. Please find the answer above.. In case of any doubts, you may ask in comments. You may upvote the answer if you feel i did a good work!

Add a comment
Know the answer?
Add Answer to:
Write a program to help answer questions like the following: Suppose the species Klingon ox has...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • I need help programming this program in C. 1.) Write a program that reads a message,...

    I need help programming this program in C. 1.) Write a program that reads a message, then checks whether it's a palindrome (the letters in the message are the same from left to right as from right to left), example is shown below: Enter a message: He lived as a devil, eh? Palindrome Enter a message: Madam, I am Adam. Not a Palindrome 2.) Ignore all characters that aren't letters. Use integer variables to keep track of positions in the...

  • Could I get some help with the following Task? The program should be implemented in JavaScript and running on Firefox, a web browser independent to operating systems. The client has specified the foll...

    Could I get some help with the following Task? The program should be implemented in JavaScript and running on Firefox, a web browser independent to operating systems. The client has specified the following requirements for the functionality of the program: 1. The program should be running without errors throughout two Phases: Information Gathering and Information Presenting. 2. Information Gathering is to gather the information such as state/territory name, the population and population change over previous year for calculation of APG;...

  • In java write a command-line program that helps to decrypt a message that has been encrypted...

    In java write a command-line program that helps to decrypt a message that has been encrypted using a Caesar cipher1. Using this method, a string may contain letters, numbers, and other ASCII characters, but only the letters (upper- and lower-case) are encrypted – a constant number, the shift, is added to the ASCII value of each letter and when letters are shifted beyond ‘z’ or ‘Z’ they are wrapped around (e.g. “Crazy?” becomes “Etcba?” when shifted by 2). When your...

  • Could someone check my answers, please? 1. Suppose that a species of fish has split into...

    Could someone check my answers, please? 1. Suppose that a species of fish has split into two populations in a lake: one on the eastern side and one on the western side. How could you determine whether the two populations constitute different species? A. Examine the populations for morphological, color, or other physical differences. B. Examine the populations for different genetic markers and allele frequencies. C. Attempt to breed members from the two populations in captivity. D. Introduce members from...

  • ANY HELP PLEASE. PLEASE READ THE WHOLE INSTRUCTION THANK YOU Write a program GS.java that will...

    ANY HELP PLEASE. PLEASE READ THE WHOLE INSTRUCTION THANK YOU Write a program GS.java that will be responsible for reading in the names and grades for a group of students, then reporting some statistics about those grades. The statistics to be gathered are the number of scores entered, the highest score and the name(s) of the student(s) who earned that score, the lowest score and the name(s) of the student(s) who earned that score, and the average score for the...

  • Your program must meet the following specifications: 1. At program start, assume a stock of 10 nickels, 10 dimes, 10...

    Your program must meet the following specifications: 1. At program start, assume a stock of 10 nickels, 10 dimes, 10 quarters, and 10 pennies. 2. Repeatedly prompt the user for a price in the form xX.xx, where X denotes a digit, or to enter q' to quit 3. When a price is entered a. If the price entered is negative, print an error message and start over requesting either a new price or to quit (indicated by entering a 'q)...

  • Requirement Write pseudocode and translate it to ONE C-program for each the following problems. In your...

    Requirement Write pseudocode and translate it to ONE C-program for each the following problems. In your pseudocode and C-program, use only what you have learned in this class so far. (Menu) Design a menu for question 2 and 3. So far, you use one program to solve all lab questions. But some of you may feel awkward when you want to demo/test only one lab question. To overcome that, your program should show a menu so that the users of...

  • Consider the following C++ program. It reads a sequence of strings from the user and uses...

    Consider the following C++ program. It reads a sequence of strings from the user and uses "rot13" encryption to generate output strings. Rot13 is an example of the "Caesar cipher" developed 2000 years ago by the Romans. Each letter is rotated 13 places forward to encrypt or decrypt a message. For more information see the rot13 wiki page. #include <iostream> #include <string> using namespace std; char rot13(char ch) { if ((ch >= 'a') && (ch <= 'z')) return char((13 +...

  • Instructions: Consider the following C++ program. It reads a sequence of strings from the user and...

    Instructions: Consider the following C++ program. It reads a sequence of strings from the user and uses "rot13" encryption to generate output strings. Rot13 is an example of the "Caesar cipher" developed 2000 years ago by the Romans. Each letter is rotated 13 places forward to encrypt or decrypt a message. For more information see the rot13 wiki page. #include <iostream> #include <string> using namespace std; char rot13(char ch) { if ((ch >= 'a') && (ch <= 'z')) return char((13...

  • For this lab you will write a Java program that plays the dice game High-Low. In...

    For this lab you will write a Java program that plays the dice game High-Low. In this game a player places a bet on whether the sum of two dice will come up High (totaling 8 or higher), Low (totaling 6 or less) or Sevens (totaling exactly 7). If the player wins, they receive a payout based on the schedule given in the table below: Choice Payout ------ ------ High 1 x Wager Low 1 x Wager Sevens 4 x...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT