Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 11 of 11

Thread: multiple classes problem

  1. #1
    Junior Member
    Join Date
    Sep 2014
    Posts
    6
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default multiple classes problem

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

    }


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: multiple classes problem

    Welcome to the forum! Please read this topic to learn how to post code in code or highlight tags and other useful info for new members.

    Please post your code correctly using code or highlight tags per the above link.

  3. The Following User Says Thank You to GregBrannon For This Useful Post:

    mgarcia920 (September 20th, 2014)

  4. #3
    Junior Member
    Join Date
    Sep 2014
    Posts
    6
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: multiple classes problem

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

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

  5. #4
    Member
    Join Date
    Aug 2013
    Posts
    95
    Thanks
    3
    Thanked 14 Times in 14 Posts

    Default Re: multiple classes problem

    Are you supposed to print the population and growth or return them to the first class?

  6. #5
    Junior Member
    Join Date
    Sep 2014
    Posts
    6
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: multiple classes problem

    its supposed to return them to the first class, store them in an array list and display them when the user enters a selection.

    --- Update ---

    that part that confuses me is that Im given many constants for the population and the population growth, Im not sure of how to store them in one variable to make them accessible in the first class and be able to store them in the arraylist. After thats done, Im guessing I can simply display an element per selection

  7. #6
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: multiple classes problem

    Classes don't have return statements to other classes to 'return' values but the use of setter (mutator) methods can be used to pass values from the second class to the first. The second class needs a reference to an instance of the first which can be accomplished through the second class' constructor or through a setter method in the second class. Since that's all a bit abstract for someone not familiar, consider this:
    // a simple class to pass an instance of itself to a second class. the second
    // class will then send a value to the first
    public class TestClass 
    {
        SecondClass secondClass;
        int value;
     
        // default constructor
        public TestClass()
        {
            new SecondClass( this );
     
        } // end default constructor
     
        // method setValue() sets value to the argument int
        public void setValue( int value )
        {
            this.value = value;
     
        } // end method setValue()
     
        // method main() to 
        public static void main(String args[])
        {
            new TestClass();
     
        } // end method main()
     
    } // end class TestClass
     
    // accepts an reference to an instance of the first class and uses that
    // reference to pass a value back to the first
    class SecondClass
    {
        TestClass testClass;
     
        // a constructor that accepts a reference to the first class as
        // a parameter
        public SecondClass( TestClass testClass )
        {
            this.testClass = testClass;
     
            // sets the value of a field in the first class
            // ('this.' is unnecessary here in the constructor but is
            // used for clarity.)
            this.testClass.setValue( 5 );
     
        } // end constructor
     
    } // end class SecondClass
    The code doesn't output anything, so you'll have to study the code to grasp the technique. Multiple setters in the first class can be used as needed to pass a number of values, or the values could be passed in another class.

  8. The Following User Says Thank You to GregBrannon For This Useful Post:

    mgarcia920 (September 20th, 2014)

  9. #7
    Junior Member
    Join Date
    Sep 2014
    Posts
    6
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default problems with set methods using multiple classes

    I have been working on my code the whole day and have changed it a bit. So my species class must add attributes for the name of the species, the population and the growth rate then it should all be added to an array list on the first class. However Im still confused on what to do on set methods with all the private variables given. Should I try and use switch statements or if/else statements to determine the newName, populationNumber, and growthNumber?


    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) 
    	{
     
    		String name = "";
    		int population=0;
    		int growth =0;
     
     
    		Species speciesObject = new Species(name,population,growth);
     
     
    		System.out.println(speciesObject.getGrowthRate());
     
    		ArrayList<Species> endangeredSpecies = new ArrayList <Species>();
     
    		endangeredSpecies.add(speciesObject);
     
    		//System.out.println(endangeredSpecies);
     
    		Scanner scanner = new Scanner(System.in);
     
    		String menuChoice="";
     
    		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.next();
    		if (menuChoice.equals("1"))
    		{
    			 System.out.println ( "You picked Florida Panther" );
    		}
     
    		else if ((menuChoice.equals("2")))
    		{
     
    			System.out.println ( "You picked LoggerHead Sea Turtle" );
    		}
    		else if ((menuChoice.equals("3")))
    		{
    			 System.out.println ( "You picked Large MetalMark Butterfly" );
    		}
    		else if ((menuChoice.equals("4")))
    		{
    			 System.out.println ( "You picked Mississippi Gopher Frog" );
    		}
    		else if ((menuChoice.equals("5")))
    		{
    			 System.out.println ( "You picked White River Spinedace" );
    		}
    		else if ((menuChoice.equals("6")))
    		{
    			System.out.println ( "You picked San Joaquin Kit Fox" );
    		}
    		else if ((menuChoice.equals("7")))
    		{
    			System.out.println ( "You picked Bald Eagle" );
    		}
    		else if ((menuChoice.equals("8")))
    		{
    			System.out.println ( "You picked Giant Panda" );
    		}
    		else if ((menuChoice.equals("9")))
    		{
    			 System.out.println ( "You picked Grizzly Bear" );
    		}
    		else
    		{
    			 System.out.println ( "Invalid Selection" );
    		}
     
     
     
     
    	}
    }

    second class

     
    public class Species 
    {
    	//species names
    	private final String name1 = "Florida Panther ";
    	private final String name2 = "Loggerhead Turtle ";
    	private final String name3 = "Metalmark Butterfly ";
    	private final String name4 = "Mississippi Frog ";
    	private final String name5 = "White River Spinedace ";
    	private final String name6 = "San Joaquin Fox ";
    	private final String name7 = "Bald Eagle ";
    	private final String name8 = "Giant Panda";
    	private final String name9 = "Grizzly Bear";
     
    	//population constants
    	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 = 5;
    	private final double LOGGERHEAD_TURTLE_DOUBLE_GRW = 24;
    	private final double METALMARK_BUTTERFLY_DOUBLE_GRW = 7;
    	private final double MISS_FROG_DOUBLE_GRW = 10;
    	private final double WHITE_RIVER_SPINEDACE_DOUBLE_GRW = 3.5;
    	private final double SAN_JOAQUIN_FOX_DOUBLE_GRW = 60;
    	private final double BALD_EAGLE_DOUBLE_GRW = 10;
    	private final double GIANT_PANDA_DOUBLE_GRW = 15;
    	private final double GRIZZLY_BEAR_DOUBLE_GRW = 32;
     
     
    	//class variables
    	String newName;
    	int populationNumber;
    	double growthNumber;
     
    	public Species()	
    	{
    		this("", 0 , 0.0);
    	}
    	public Species(String name)
    	{
    		this(name, 0 , 0.0);
    	}
    	public Species(String name, int pop, double growthRate)
    	{
    		newName = name;
    		populationNumber = pop;
    		growthNumber = growthRate;
    	}
     
     
     
    	/*public void setSpeciesInfo(String name, int pop, double growthRate)
    	{
    		newName=name;
     
     
    		if (newName.equals(name1))
    		{
    			populationNumber= pop;
    			growthNumber = growthRate;
    		}
    	}*/
     
    	public void setName(String name)
    	{
     
    	}
     
    	public String getName()
    	{
    		return newName;
    	}
     
    	public void setGrowthRate(double growthRate)
    	{
     
    	}
     
    	public double getGrowthRate()
    	{
    		return growthNumber;
    	}
     
    	public void setPopulation(int population)
    	{
     
    	}
     
    	public int getPopulation()
    	{
    		return populationNumber;
    	}
     
    	public boolean equals(Species obj)
    	{
    		return (newName.equals(obj.newName)) &&
    	           (populationNumber == obj.populationNumber) &&
    	           (growthNumber == obj.growthNumber);
    	}
     
    	public String toString() 
    	{
     
    		        return newName + " (population of " + populationNumber + " with growth rate " +
     
    		            growthNumber + "%)";
    	}
     
    }

  10. #8
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: multiple classes problem

    Please do not start multiple threads on the same topic. Threads merged.

    To be clear, state your requirements using the names of the two classes, Tester and Species.
    However Im still confused on what to do on set methods with all the private variables given.
    What do you mean, "private variables given?" Is some of this code provided by the instructor with instructions that it cannot be modified? If so, please make that clear. Specifically indicate which code can be modified and which can't. Also explain why Species has code commented out and some empty methods.

    In general, the details for each of the possible Species objects are already given and don't need to be modified. That's why they are private and final. My guess as to what you must do, based on the sketchy instructions you've given and zero code comments, is that the Tester class is used to create Species objects chosen by the user and added to an ArrayList<Species> as each is created. Once the user chooses a specific animal type from the menu, a Species object is created using that name. Looking at the available Species constructors, the default and single-parameter constructors both create an unnamed or named Species object, respectively, with the population and growth rates set to zero. so those values must be collected from the given final values and set for each object. (Is that what the code commented out was/is meant to do?)

    Is the commented out method in Species what you're asking about in your latest post? If so, please explain your question clearly as I've tried to do above, though it's just a guess at this point. Providing answers to the wrong question is a waste of everyone's time. If my guess is correct or close to correct, I have some ideas about how to proceed.

    Posting the assignment's instructions exactly as they were given to you might also be helpful.

  11. #9
    Junior Member
    Join Date
    Sep 2014
    Posts
    6
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: multiple classes problem

    these are the instructions:
    Assignment
    Create a class to track endangered species.
    You will need to record the species name, current population, and growth rate.
    You will need methods to construct a new instance of an endangered specifies, calculate the projected population after x number of years, appropriate accessor and mutator methods.
    You will also need to override the equals() and the toString() method.
    Your tester class will provide the data to construct several endangered species and store the objects in an ArrayList.



    My understanding is that in the Species class I should add attributes for name, population and growth rate and then put them into the arrayList in the class tester.



    the empty set methods are the ones I need help on, the commented out method is a method I was working on but decided to delete. I feel that if the user were to enter the name, population, and growth rate it would be much easier for me. What confuses me is working with the given name, population, and growth rate constants and assigning them to the variables newName, populationNumber, and growthNumber in order for me to pass the values to the tester class and store them in the arrayList.

  12. #10
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: multiple classes problem

    The constants will be assigned as each instance of the Species class is created. There's no real "passing" of just the values as you've been saying. Here's a suggestion:

    Step 1: In the Species class, put the species names, population and growth rate constants into their own collections.

    I like the idea for your setSpeciesInfo() method, and this is how it might work:
    After the object is created with only the name identified, determine at which index the species name exists in the species names collection, possibly using the indexOf() method. That same index will correspond to the population and growth rate constants for that species in the population and growth rate collections created in step 1. Use that index to collect and set the constants from the other collections.

    Once building the object is complete, add it to the ArrayList<Species>.

  13. #11
    Junior Member
    Join Date
    Sep 2014
    Posts
    6
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: multiple classes problem

    thank you very much! Ill give that a try =)

Similar Threads

  1. problem accessing variables in multiple classes
    By javaiscool in forum Java Theory & Questions
    Replies: 4
    Last Post: March 20th, 2013, 06:15 PM
  2. Best way to turn a single class into multiple classes
    By JoeBrown in forum What's Wrong With My Code?
    Replies: 2
    Last Post: February 21st, 2012, 11:57 AM
  3. Dice Wagering Game new to Multiple Classes
    By Laxman2809 in forum What's Wrong With My Code?
    Replies: 24
    Last Post: September 16th, 2011, 08:22 PM
  4. help with using multiple classes
    By finalfantasyfreak15 in forum What's Wrong With My Code?
    Replies: 0
    Last Post: March 5th, 2011, 12:18 AM
  5. Replies: 6
    Last Post: May 15th, 2009, 05:06 PM

Tags for this Thread