This is my first semester of Java and still trying to embrace the idea of multiple classes. My program is meant to show the population and growth rate of an endangered species that the user has selected from a given menu. On the first class, only the menu is meant to be shown then all the data is meant for the seconds class which should be stored in an array list in the first class. As simple as it sounds, Im a little stuck on the second class when setting the population and the population growth. The highlited methods are where my problem is.Im not exactly sure how to send over the population and growth to match the to be stored in the array list .Can somebody give me a hint or show me the path towards the right direction?
first class:
import java.util.Collections;
import java.util.Scanner;
import java.util.ArrayList;
import java.lang.Object;
public class Tester
{
public static void main(String[] args)
{
int population=0;
double growth =0;
Species speciesObject = new Species(population, growth);
ArrayList <Species> endangeredSpecies = new ArrayList <>();
endangeredSpecies.add(speciesObject);
System.out.println(endangeredSpecies);
Scanner scanner = new Scanner(System.in);
int menuChoice=0;
System.out.println("Please select an option");
System.out.println("1) Florida Panther");
System.out.println("2) Loggerhead Sea Turtle");
System.out.println("3) Large Metal Mark Butterfly");
System.out.println("4) Mississippi Gopher Frog");
System.out.println("5) White River Spinedace");
System.out.println("6) San Joaquin Kit Fox");
System.out.println("7) Bald Eagle");
System.out.println("8) Giant Panda");
System.out.println("9) Grizzly Bear");
menuChoice = scanner.nextInt();
switch ( menuChoice ) {
case 1:
System.out.println ( "You picked Florida Panther" );
System.out.println(endangeredSpecies.get(0));
break;
case 2:
System.out.println ( "You picked LoggerHead Sea Turtle" );
break;
case 3:
System.out.println ( "You picked Large MetalMark Butterfly" );
break;
case 4:
System.out.println ( "You picked Mississippi Gopher Frog" );
break;
case 5:
System.out.println ( "You picked White River Spinedace" );
break;
case 6:
System.out.println ( "You picked San Joaquin Kit Fox" );
break;
case 7:
System.out.println ( "You picked Bald Eagle" );
break;
case 8:
System.out.println ( "You picked Giant Panda" );
break;
case 9:
System.out.println ( "You picked Grizzly Bear" );
break;
default:
System.out.println ( "Unrecognized option" );
break;
}
}
}
second class:
public class Species
{
//constants for current population
private final int FLORIDA_PANTHER_DOUBLE_POP = 120;
private final int LOGGERHEAD_TURTLE_DOUBLE_POP = 4200;
private final int METALMARK_BUTTERFLY_DOUBLE_POP = 150;
private final int MISS_FROG_DOUBLE_POP = 100;
private final int WHITE_RIVER_SPINEDACE_DOUBLE_POP = 50;
private final int SAN_JOAQUIN_FOX_DOUBLE_POP = 7000;
private final int BALD_EAGLE_DOUBLE_POP = 11000;
private final int GIANT_PANDA_DOUBLE_POP = 1000;
private final int GRIZZLY_BEAR_DOUBLE_POP = 500;
//constants for growth rate
private final double FLORIDA_PANTHER_DOUBLE_GRW = .005;
private final double LOGGERHEAD_TURTLE_DOUBLE_GRW = .024;
private final double METALMARK_BUTTERFLY_DOUBLE_GRW = .007;
private final double MISS_FROG_DOUBLE_GRW = .010;
private final double WHITE_RIVER_SPINEDACE_DOUBLE_GRW = .035;
private final double SAN_JOAQUIN_FOX_DOUBLE_GRW = .60;
private final double BALD_EAGLE_DOUBLE_GRW = .010;
private final double GIANT_PANDA_DOUBLE_GRW = .015;
private final double GRIZZLY_BEAR_DOUBLE_GRW = .032;
//
private int populationNumber;
private double growthNumber;
//Constructor
public Species(int pop, double grw)
{
setSpecies(pop, grw);
}
public void setSpecies(int pop, double grw)
{
setPopulation(pop);
setGrowth(grw);
}
public void setPopulation(int pop)
{
switch (pop)
{
case 1:
populationNumber = FLORIDA_PANTHER_DOUBLE_POP;
break;
case 2:
populationNumber = LOGGERHEAD_TURTLE_DOUBLE_POP;
break;
case 3:
populationNumber = METALMARK_BUTTERFLY_DOUBLE_POP;
break;
case 5:
populationNumber = MISS_FROG_DOUBLE_POP;
break;
case 6:
populationNumber = WHITE_RIVER_SPINEDACE_DOUBLE_POP;
break;
case 7:
populationNumber = SAN_JOAQUIN_FOX_DOUBLE_POP;
break;
case 8:
populationNumber = BALD_EAGLE_DOUBLE_POP;
break;
case 9:
populationNumber = GIANT_PANDA_DOUBLE_POP;
break;
case 10:
populationNumber = GRIZZLY_BEAR_DOUBLE_POP;
break;
}
}
public void setGrowth(double grw)
{
String growth = String.valueOf(grw);
switch (pop)
{
case 1:
populationNumber = FLORIDA_PANTHER_DOUBLE_POP;
break;
case 2:
populationNumber = LOGGERHEAD_TURTLE_DOUBLE_POP;
break;
case 3:
populationNumber = METALMARK_BUTTERFLY_DOUBLE_POP;
break;
case 5:
populationNumber = MISS_FROG_DOUBLE_POP;
break;
case 6:
populationNumber = WHITE_RIVER_SPINEDACE_DOUBLE_POP;
break;
case 7:
populationNumber = SAN_JOAQUIN_FOX_DOUBLE_POP;
break;
case 8:
populationNumber = BALD_EAGLE_DOUBLE_POP;
break;
case 9:
populationNumber = GIANT_PANDA_DOUBLE_POP;
break;
case 10:
populationNumber = GRIZZLY_BEAR_DOUBLE_POP;
break;
}
}
public int getPopulation()
{
return populationNumber;
}
public double getGrowth()
{
return growthNumber;
}
public String toString()
{
return String.format("population:" , populationNumber);
}
}