Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
854 views
in Technique[技术] by (71.8m points)

java - boolean trouble from different class

For my assignment question it is a method with the signature public int applyNutrientCoefficient(), that calculates which Guppies in the Pool have died of malnutrition, and returns the number of deaths.

Use an Iterator<Guppy> to iterate over the guppiesInPool in the Pool. For each Guppy generate a different random number between 0.0 and 1.0 inclusive using the Random method nextDouble(). If this randomly generated number is greater than the Pool's nutrient coeffcient, kill that Guppy by setting the appropriate boolean field in the Guppy. Note that this method does not remove any dead Guppies from the Pool, it just kills them. Do not do anything else.

I have 2 classes one is Guppy one is Pool in my guppy class I made a boolean -

private boolean isAlive{}
public boolean getIsAlive(){ 
    return isAlive
}

in my Pool class....

public int applyNutrientCoefficient() 

int deathCount = 0

Iterator<Guppy> it = guppiesInPool.iterator()

while (it.hasNext() ) 

Guppy guppyOne = it.next()

    if (randomNumberGenerator.nextDouble() > nutrientCoefficient) 
    if (guppyOne.isAlive() ) 
    guppyOne.setAlive(false)
    deathCount++

    return deathCount

The error message I'm getting is cannot find symbol - method isAlive()

Can someone help please

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

you have to acces isAlive private field via the public getter that you provided getIsAlive()

In the Pool class

if (randomNumberGenerator.nextDouble() > nutrientCoefficient) 
if (guppyOne.isAlive() ) 
guppyOne.setAlive(false)
deathCount++

return deathCount

the line

if (guppyOne.isAlive() )

should instead be

if (guppyOne.getIsAlive() )

same thing with the setter: you need to provide a setter to the Guppy class and use that

public void setIsAlive(boolean alive){
this.isAlive = alive}

end result should be

if (randomNumberGenerator.nextDouble() > nutrientCoefficient) 
if (guppyOne.getIsAlive() ) 
guppyOne.setIsAlive(false)
deathCount++

return deathCount

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...