question about a class

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • kalar
    New Member
    • Aug 2007
    • 82

    question about a class

    i want to ask a question about a class i found on a book
    my problem is in projectedPopula tion method. I want to ask why we use the count and populationAmoun t variable .I mean where is the problem if i use years and population variables?
    thanks !

    Code:
    public class Species {
    	
    	private String name;
    	private int population;
    	private double growthRate;
    	
    	public void readInput()
    	{
                  //  ................................
    		System.out.println("What is the species's name?");
    		name = keyboard.nextLine();
    		
                 //more code
    
    			population = keyboard.nextInt();
    		System.out.println("Enter growth rate (percent 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 projectedPopulation(int years)
    	{
    		double populationAmount = population;
    		int count = years;
    		while((count > 0) && (population > 0))
    	    {
    			populationAmount = (populationAmount + (growthRate/100)*populationAmount);
    			count--;
    		}
    		
    		if (populationAmount > 0)
    			return (int)populationAmount;
    		else
    			return 0;
    		
    	};
  • BigDaddyLH
    Recognized Expert Top Contributor
    • Dec 2007
    • 1216

    #2
    That method could have used years instead of introducing the local variable count. But there's a convention of treating method parameters as though they were constants. For example, you may want to amend the method with a print statement and use the original value of years in it. In any case, this is not a big issue.

    On the other hand, not using field population in this method is crucial. This method shouldn't change the state of the Species object, right? So after calling it, the value of population should be the same. Therefore, don't touch it in this method.

    Comment

    • kalar
      New Member
      • Aug 2007
      • 82

      #3
      Thank you @BigDaddyLH.
      I am new to java so i make these questions.

      So we don't use the years variable because maybe we need it . I mean this is not a java rule?
      The second i understand it

      Comment

      • BigDaddyLH
        Recognized Expert Top Contributor
        • Dec 2007
        • 1216

        #4
        Originally posted by kalar
        So we don't use the years variable because maybe we need it . I mean this is not a java rule?
        It is a convention and not a rule of syntax. To verify this, edit the code and run it.

        Comment

        • kalar
          New Member
          • Aug 2007
          • 82

          #5
          i run the code with years variable and it runs ok and the numbers are ok(should i got an error?)

          but when i run the programm with population and not populationAmoun t the compile got an error

          Comment

          • BigDaddyLH
            Recognized Expert Top Contributor
            • Dec 2007
            • 1216

            #6
            Originally posted by kalar
            i run the code with years variable and it runs ok and the numbers are ok(should i got an error?)

            but when i run the programm with population and not populationAmoun t the compile got an error
            I left my crystal ball at home so I don't know what compile-time error you got, but it was probably because population has type int and populationAmoun t has type double. This is easy to fix.

            Comment

            • kalar
              New Member
              • Aug 2007
              • 82

              #7
              i got an error in line:
              population = (population + (growthRate/100)*population );
              with error:
              possible loss of precision

              Comment

              • BigDaddyLH
                Recognized Expert Top Contributor
                • Dec 2007
                • 1216

                #8
                Originally posted by kalar
                i got an error in line:
                population = (population + (growthRate/100)*population );
                with error:
                possible loss of precision
                Yes, that is because population should be a double.

                Comment

                • Ganon11
                  Recognized Expert Specialist
                  • Oct 2006
                  • 3651

                  #9
                  I'm not so sure - you can't have a half a person, or a quarter a rabbit, so it makes sense to keep it as an int. When estimating size of populations, the decimal places might be included to increase accuracy of prediction (so you don't always round up or always round down). But I could see this temporary variable as an int instead of a double.

                  Comment

                  • BigDaddyLH
                    Recognized Expert Top Contributor
                    • Dec 2007
                    • 1216

                    #10
                    Originally posted by Ganon11
                    I'm not so sure - you can't have a half a person, or a quarter a rabbit, so it makes sense to keep it as an int. When estimating size of populations, the decimal places might be included to increase accuracy of prediction (so you don't always round up or always round down). But I could see this temporary variable as an int instead of a double.
                    I had half a chicken for lunch ;-)

                    Comment

                    • Ganon11
                      Recognized Expert Specialist
                      • Oct 2006
                      • 3651

                      #11
                      I bet you had half a dead chicken. And the other half was dead, too. Even if you had half a live chicken, I'm betting all parts of the chicken are now dead.

                      Besides, there's no boolean member of Species indicating the species deliciousness.

                      Comment

                      • BigDaddyLH
                        Recognized Expert Top Contributor
                        • Dec 2007
                        • 1216

                        #12
                        Originally posted by Ganon11
                        I bet you had half a dead chicken. And the other half was dead, too. Even if you had half a live chicken, I'm betting all parts of the chicken are now dead.

                        Besides, there's no boolean member of Species indicating the species deliciousness.
                        All species are delicious if you cook 'em right. Except perhaps Bitter Melon.

                        Comment

                        • JosAH
                          Recognized Expert MVP
                          • Mar 2007
                          • 11453

                          #13
                          Originally posted by BigDaddyLH
                          All species are delicious if you cook 'em right. Except perhaps Bitter Melon.
                          Bitter melons are ferocious little predators; they have fangs and claws. When
                          another creature like a cow (or Dan Quaille) accidentally trots around in a field
                          with bitter melons they react like piranhas: they rip the other animal to pieces
                          within a minute. Nasty little creatures they are, just like jalapenos ...

                          kind regards,

                          Jos ;-)

                          Comment

                          Working...