Question
Write a public instance method called runExperiments() which takes no argument and returns no value.(done) The method should attempt to decrement powerLevel by a random
Write a public instance method called runExperiments() which takes no argument and returns no value.(done) 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... I have attempted this below but I don't think it is correct. If the powerLevel can be reduced, a message indicating the experiment number (starting at 1) should be displayed. If not, a suitable message should be displayed and remaining experiments should not be attempted. When all experiments have finished it should display "Experiment run stopped". Write this method, choosing a suitable kind of loop to control the number of iterations required. Hints: 1. Don't forget to make use of the return value from decrementPower(). 2. Experiments should not yet run as the default value of powerLevel is 0. 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; }}
question above: This is what I have attempted so far but not sure if it is correct and how I would do the rest of it:
public void runExperiments() {{ for (int i = 0; i < this.numberOfExperiments; i++ ) { this.powerLevel -= randomInteger(); } 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; }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started