Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8734001
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T09:45:30+00:00 2026-06-13T09:45:30+00:00

I am trying to create a program that calculates different data about butterfly populations.

  • 0

I am trying to create a program that calculates different data about butterfly populations. My main issue is that I keep receiving an error for my potentialPopulation equation, where I square my ratioFactor value. The error is that the value I am squaring “may not have been initialized”. I know I need to set the ratioFactor to a value, but the value will be unknown until the inputs are entered. Also, I am a beginner, so if anyone sees any other errors I would really appreciate any help. Thank you

// This program calculates butterfly population estimates
//   Inputs  : males,   estimated number of male butterflies
//             females, estimated number of female butterflies
//   Outputs : total butterflies, sex ratio, variance
// Written by: Charlie
//   Modified: Oct 26, 2012 by Daniel Kellogg
//

import java.util.Scanner;
import java.text.DecimalFormat;
public class Hwk7 {
    public static void main (String[] args) {
            int males, females;

            int totalButterflies, sexRatio, ratioVariance, genderDifferences, matingPairs, growthFactor, ratioFactor, potentialPopulation, x;

            Scanner stdin = new Scanner(System.in);

            System.out.println("\nButterfly Estimator\n");
            System.out.print("Enter the estimated males population: ");
            males = stdin.nextInt();
            System.out.print("Enter the estimated females population: ");
            females = stdin.nextInt();

            totalButterflies  = males + females;
            sexRatio          = males / females;
            ratioVariance     = males % females;
            genderDifferences = males - females;
            matingPairs       = males * females;
            growthFactor      = (int)(Math.sqrt(matingPairs));

            if (sexRatio != 0){
                    ratioFactor       = growthFactor / sexRatio;

             if (sexRatio == 0){
                   ratioFactor = (int)(Math.sqrt(ratioVariance));
            }
            ratioFactor = x;
            potentialPopulation = x^2;

            System.out.println("\nTotal Butterflies: " + totalButterflies );
            System.out.println("Sex Ratio        : " + sexRatio );
            System.out.println("Variance         : " + ratioVariance );
            System.out.println("Gender Differences: " + genderDifferences );
            System.out.println("Possible Mating Pairs: " + matingPairs );
            DecimalFormat oneDigit = new DecimalFormat("#.000");
            System.out.println("Growth Factor: " + growthFactor + oneDigit.format(growthFactor));
            DecimalFormat twoDigit = new DecimalFormat("#.0");
            System.out.println("Ratio Factor: " + ratioFactor + twoDigit.format(ratioFactor));
            DecimalFormat threeDigit = new DecimalFormat("##0");
            System.out.println("Potential Population: " + potentialPopulation + threeDigit.format(potentialPopulation));
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T09:45:31+00:00Added an answer on June 13, 2026 at 9:45 am

    The number one best piece of advice you can get about writing code is to make each function do exactly one thing. I’ve refactored your class below to break out your key functionality into their own, particular methods. Then, when you have a problem with each method you can solve the problem in that method.

    Note too that in your sexRatio zero divisor case, you are getting the sqrt of a variable (ratioVariance) you never set, or ask for input on. You then immediately reset the ratioFactor to x – a mysterious variable that is also never set.

    import java.util.Scanner;
    import java.text.DecimalFormat;
    public class Hwk7 {
        private Scanner stdin = new Scanner(System.in);//This needs to be used throughout your class
    
        //Do these defaults make sense?
        private int males = 0;
        private int females = 0;
    
        private int totalButterflies  = 0;
        private double sexRatio       = 0;
        private int ratioVariance     = 0;
        private int genderDifferences = 0;
        private int matingPairs       = 0;
        private double growthFactor   = 0;
        private int potentialPopulation = 0;
    
    
        public static double getInput(String message, int input) {
            System.out.print(message);
            input = stdin.nextInt();
        }
    
        public static void main (String[] args) {
            Hwk7 hw = new Hwk7();
            hw.run();
        }
    
        public void run() {
            System.out.println("\nButterfly Estimator\n");
    
            getInput("Enter the estimated males population: ", males);
            getInput("Enter the estimated females population: ", females);
    
            calculateResults();
            printResults();
        }
    
        public void calculateResults() {
            totalButterflies  = males + females;
            sexRatio          = males / females;
            ratioVariance     = males % females;
            genderDifferences = males - females;
            matingPairs       = males * females;
            growthFactor      = (int)(Math.sqrt(matingPairs));
            ratioFactor       = calculateRatioFactor(growthFactor, sexRatio);
            potentialPopulation = x^2;//where are you getting x from!?
        }
    
        //Note in your original implementation you calculate this and then immediately
        //change it to the value 'x'! This is clearly wrong.
        public static double calculateRatioFactor(int growthFactor, int sexRatio) {
            if (sexRatio == 0) {
                return Math.sqrt(RATIOVARIANCE);//Ratio variance is never set!
            } else {
                return growthFactor / sexRatio;
            }
        }
    
        public static void printResults(int males, int females) {
           System.out.println("\nTotal Butterflies: " + totalButterflies );
           System.out.println("Sex Ratio        : " + sexRatio );
           System.out.println("Variance         : " + ratioVariance );
           System.out.println("Gender Differences: " + genderDifferences );
           System.out.println("Possible Mating Pairs: " + matingPairs );
           DecimalFormat oneDigit = new DecimalFormat("#.000");
           System.out.println("Growth Factor: " + growthFactor + oneDigit.format(growthFactor));
           DecimalFormat twoDigit = new DecimalFormat("#.0");
           System.out.println("Ratio Factor: " + ratioFactor + twoDigit.format(ratioFactor));
           DecimalFormat threeDigit = new DecimalFormat("##0");
           System.out.println("Potential Population: " + potentialPopulation + threeDigit.format(potentialPopulation));
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to create a program that will get data directly from socket
I am trying to create a program that will be passed input data from
I am trying to create a simple phonebook program that reads data from a
I'm trying to create a program that takes a text file of c++ code
I am trying to create a program that will do some simple calculations, but
I am trying to create a program that has one tableview and when you
I'm currently trying to create a program that estimates location based on signal strength.
I'm trying to create a c program that implements the radon transform algorithm. I
I am trying to create a Jenkins job that restarts a program that runs
I have a small program that I'm trying to create to get ip addresses

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.