ajanic0le
ajanic0le

Reputation: 383

Java - code compiles but entire code doesnt run

I am working on an assignment where I need to find how many years before the population of the smaller species exceeds the population of the larger species.

The program I've created compiles, but only runs through the first half. It does not have output for anything following the two system.out.println commands, before the if-else loop starts.

What steps do I need to take to adjust the program code so that it runs through the if-else loop successfully?

CLASS:

import java.util.Scanner;

public class Species
{
    private String name;
    private int population;
    private double growthRate;

    public void readInput()
    {
        Scanner keyboard = new Scanner(System.in);
        System.out.println("What is the species' name?");
        name = keyboard.nextLine();

        System.out.println("What is the population of the species?");
        population = keyboard.nextInt();

        while(population < 0)
        {
            System.out.println("Population cannot be negative.");
            System.out.println("Reenter population:");
            population = keyboard.nextInt();
        }

        System.out.println("Enter growth rate (% increase per year):");
        growthRate = keyboard.nextDouble();
    }

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

    public int predictPopulation(int years)
    {
        int result = 0;
        double populationAmount = population;
        int count = years;

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

        if (populationAmount > 0)
            result = (int)populationAmount;
        return result;
    }

    public Species(String name)
    {
        name = name;
        population = 0;
        growthRate = 0.0;
    }

    public Species(int population)
    {
        name = "";
        if (population > 0)
            population = population;
        else
        {
            System.out.println("ERROR: using a negative" + "population.");
            System.exit(0);
        }
        growthRate = 0.0;
    }

    public Species(double growthRate)
    {
        name = "";
        population = 0;
        growthRate = growthRate;
    }

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

    public Species()
    {
        name = "";
        population = 0;
        growthRate = 0;
    }

    public void setSpecies(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 void setName(String name)
    {
        name = name;
    }

    public void setPopulation(int population)
    {
        if (population > 0)
            population = population;
        else
        {
            System.out.println("ERROR: using a negative" + "population."); 
            System.exit(0);
        }
    }

    public void setGrowthRate(double growthRate)
    {
        growthRate = growthRate;
    }

    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);
    }
}

PROGRAM:

import java.util.Scanner;
public class KlingonOx extends Species
{
    public static void main(String[] args) 
    {  
        new KlingonOx().run();
    }

    public void run()
    {
        Species klingonSpecies = new Species("Ox", 100, 15);
        Species earthSpecies = new Species("Elephant", 10, 35);

        klingonSpecies.readInput();
        klingonSpecies.writeOutput();
        klingonSpecies.predictPopulation(10);
        klingonSpecies.setSpecies("Ox", 100, 15);
        klingonSpecies.setName("Ox");
        klingonSpecies.setPopulation(100);
        klingonSpecies.setGrowthRate(15);
        klingonSpecies.getName();
        klingonSpecies.getPopulation();
        klingonSpecies.getGrowthRate();
        klingonSpecies.equals(earthSpecies);

        earthSpecies.readInput();
        earthSpecies.writeOutput();
        earthSpecies.predictPopulation(10);
        earthSpecies.setSpecies("Elephant", 10, 35);
        earthSpecies.setName("Elephant");
        earthSpecies.setPopulation(10);
        earthSpecies.setGrowthRate(35);
        earthSpecies.getName();
        earthSpecies.getPopulation();
        earthSpecies.getGrowthRate();
        earthSpecies.equals(klingonSpecies);

        System.out.println("The " + klingonSpecies.getName() + " starting population is: " + klingonSpecies.getPopulation());
        System.out.println("The " + earthSpecies.getName() + " starting population is: " + earthSpecies.getPopulation());


        int year = 0;
        int newkpop = 100;
        int newepop = 10;
        if (klingonSpecies.getPopulation() < earthSpecies.getPopulation())
        {
            while (klingonSpecies.getPopulation() < earthSpecies.getPopulation())
            {
                newkpop = (int)(klingonSpecies.getPopulation() + (klingonSpecies.getPopulation()*1.15) );
                newepop = (int)(earthSpecies.getPopulation() + (earthSpecies.getPopulation()*1.35) );
                year ++;
            }
            System.out.println("The population of " + klingonSpecies.getName() + " exceeds the population of " + earthSpecies.getName() + " in " + year + "years.");
        }
        else
        {
        while (klingonSpecies.getPopulation() > earthSpecies.getPopulation())
            {
                newkpop = (int)(klingonSpecies.getPopulation() + (klingonSpecies.getPopulation()*1.15) );
                newepop = (int)(earthSpecies.getPopulation() + (earthSpecies.getPopulation()*1.35) );
                year ++;
            }
            System.out.println("The population of " + earthSpecies.getName() + " exceeds the population of " + klingonSpecies.getName() + " in " + year + "years.");
        }

    }
}

Upvotes: 0

Views: 427

Answers (3)

MadProgrammer
MadProgrammer

Reputation: 347234

Start with the fact that you never change the population of either species in your loops, so the population actually remains the same.

When making your calculations, you will need to apply the changes back the instances of the objects, presumably using the setPopulation method OR you will need to maintain separate variables of each species which takes there current population as the loop progresses.

Upvotes: 0

Manuel Darveau
Manuel Darveau

Reputation: 4635

The problem is that you never update the population of your species:

klingonSpecies.setPopulation(newkpop);
earthSpecies.setPopulation(newepop)

Upvotes: 0

irrelephant
irrelephant

Reputation: 4111

You have a Species#setPopulation() method, but it looks like you forgot to use it. Use klingonSpecies.setPopulation(newkpop); and earthSpecies.setPopulation(newepop); in both of your loops.

Unrelated to the loop issue: you might want to use Species#getGrowthRate() instead of typing out the numbers 1.15 and 1.35.

Upvotes: 1

Related Questions