lundi 27 janvier 2020

Write a public instance method called which takes no argument and returns no value using supplied helper method

The method should attempt to decrement powerLevel by a random number between 1 and 3 inclusive for each experiment up to numberOfExperiments. You should make use of the supplied helper method randomInteger() for this.

Can someone please help me with my code below is where I am stuck. I am not sure how to get random number by using the info above.

and here is the full code:

 public class SpaceRocket extends FlyingObject implements Launchable
 {
private int maxPowerLevel;    
private int numberOfExperiments;
private int powerLevel;

public int getMaxPowerLevel()
{
    return this.maxPowerLevel;
}

public int getNumberOfExperiments()
{
    return this.numberOfExperiments;
}

public int getPowerLevel()
{
    return this.powerLevel;
}

public SpaceRocket(String aName, int aNumberOfExperiments)
{
    this.name = aName;  
    this.numberOfExperiments = aNumberOfExperiments;
    this.powerLevel = 0;
    this.maxPowerLevel = 15;
}

public boolean decrementPower(int powerReduction)
{
    if (powerReduction > this.getPowerLevel()){
        this.powerLevel = 0;  
    return false;
} 
else
{
    this.powerLevel = powerReduction;
    return true;
}
}

This is what I have done so far:

public void runExperiments()
{
this.powerLevel -= randomInteger(1,3);
}

and this is what has been privided:

/** * provided * return a random integer between 1 and 3 inclusive

  public int randomInteger() 
   {
   java.util.Random r = new java.util.Random();
   return r.nextInt(3) + 1;
   }  



Aucun commentaire:

Enregistrer un commentaire