Can someone explain what's going on here step-by-step? [closed] - java

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 5 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Improve this question
public int pray(int secondsPrayed){
int randomRecoveryValue = secondsPrayed + new Random().nextInt(3);
int actuallyRecoveredValue = Math.min(this.MAX_MP -this.mp,randomRecoveryValue);
this.mp += actuallyRecoveredValue;
return actuallyRecoveredValue;
}
This is from a book I'm currently using to learn. The objective is to create a pray method for a Priest object that restores its MP by the amount of seconds prayed plus 0 to 2 at random. I don't quite understand two things:
3 after nextInt rather than 2 (the book's answer) - why is this when we're trying to get 0 to 2 at random?
actuallyRecoveredValue returns its value to secondsPrayed; however, to me it looks like that to get actuallyRecoveredValue in the first place we need RandomRecoveryValue, which in turn needs secondsPrayed, which at that point doesn't seem to exist? I'm new to Java so I don't have much experience with returning values and such.

nextInt(int n)
Returns a pseudorandom, uniformly distributed int value between 0
(inclusive) and the specified value (exclusive), drawn from this
random number generator's sequence.
You pass secondsPrayed as variable into the method, so it does exist inside the method (scope). From the code you posted, we can't tell if actuallyRecoveredValue returns its value to secondsPrayed is true.
to me it looks like that to get actuallyRecoveredValue in the first
place we need RandomRecoveryValue, which in turn needs secondsPrayed
it's true, but as stated above, secondsPrayed does exist

1) The nextInt() method returns an integer value between zero and the parameter you used (3 in your case). While zero is included in the range, the last element of the range is not.
You can think that nextInt(n) returns n possible values, meaning the range between "0" and "n-1"
nextInt(5) // can return 0,1,2,3,4
2) The code is a little bit messy, I'll give you that, and as you said to get you have to determine the value of "randomRecoveryValue" in order to determine actuallyRecoveredValue. The method pray() does three things:
Calculates the recovered value, based on the input param and some randomness
Adjusts this value, so that the priest cannot heal over its full health. That happens at the third line, basically the priest heals for a number of points equal to "randomRecoveryValue" only if its current MP is not too high, otherwise he heals for the value that will get him to full health.
Update the value of his MP
Returns how much the he actually healed, not necessary, but may be useful to the caller of this method
Cheers

Related

How can I do this program with arrays? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 5 years ago.
Improve this question
I have a doubt with a problem about arrays, I have array that only recieves 1 and 0, for example {0,1,0,1,0}, after that I need to select 2 positions inside of the array, in this case p1 = 0 and p2=2, then I need to print the number between p1 and p2, in this case 010, after that I need convert that numer to decimal, how can I do that? Im a beginner, really I tried but I can't did it
I won't give you the ready solution but an idea how you can achieve this.
Think if a loop that iterates from p1 till p2 through your array. In each iteration it prints out the value of the array at its current position.
Hint: for loops are perfectly suitable for this. To print use
System.out.println(array[currentPosition])
To compute the decimal value you would contain a variable in which you compute like this.
Initially the variable decimal gets set to 0.
If the value of the array at its currentPosition is 1 you take the value of decimal times 2 and add 1 and again save it in decimal.
If it is zero you take decimal times two.

Java Random Number with seed changes after a day [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
I was using the code below to generate random number with seed so that I can generate back the same results next time.
int seed = 100;
Random rand = new Random(seed);
I manage to get back the same results from the program on the same day, but after a day or two i get a completely different results. Is there a problem of the way I implement the random seed? Anyone encounter this before?
Citing from Javadoc:
If two instances of Random are created with the same seed, and the same sequence of method calls is made for each, they will generate and return identical sequences of numbers.
It really depends on how you use that instance. It has to be strictly the same sequence of methods you call.
This means the problem is in the code that calls the methods on your Random instance. The sequence of the methods called seems to depend on some conditional that can change it.

Unsure why code isn't working [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
I am unsure why my code is not working:
public int caughtSpeeding(int speed, boolean isBirthday) {
if(isBirthday=true){
speed = speed - 5;
}
if(speed<=60){
return 0;
}
if(speed>=81){
return 2;
}
return 1;
}
The question is:
You are driving a little too fast, and a police officer stops you. Write code to compute the result, encoded as an int value: 0=no ticket, 1=small ticket, 2=big ticket. If speed is 60 or less, the result is 0. If speed is between 61 and 80 inclusive, the result is 1. If speed is 81 or more, the result is 2. Unless it is your birthday -- on that day, your speed can be 5 higher in all cases.
You are using an assignment operator here =, where you should be using an == operator for comparison. So it should be
if(isBirthday==true){
speed = speed - 5;
}
Here's the problem:
if (isBirthday = true) {
It should be:
if (isBirthday == true) {
We use two equal signs for comparison, otherwise you're just assigning a true value to the variable, making it always true. We can go even further and simplify the expression like this:
if (isBirthday) {
Your code is not working because you have a typo here:
if(isBirthday=true){
this is setting the variable to true instead of checking its value
if(isBirthday==true){ is what you are looking for
and in almost all languages is better when you write
if(isBirthday){ for checking if true
if(!isBirthday){ for checking if false
instead

How can I go through several possibilities without exhausting loops? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
How can I go through several possibilities without exhausting loops in a program that sum of the proper divisors(divisors including 1 but not itself) of the number is greater than the number, but no subset of those divisors sums to the number itself.
For Example:
12=(factors sum)1+2+3+4+6
Atleast Any Subset Sum should be equal to the number i.e 12 in this case.
Question link
:http://www.practice.geeksforgeeks.org/problem-page.php?pid=301
Approach:
Step1 (Doubt)
By looking at the problem I found that its optimal solution will be Dynamic Programming.(Since I don't know that).
Apart from that I thought of the solution like this
For Example 12=1,2,3,4,6(divisors)
So,Solving it through like this with 1 starting having all permutations: 1+2+3+4+6 or 1+3+4+6 or 1+4+6 or 1+6(checking at each step whether it is <=12.
Similarly,I checked starting with 2: 2+3+4+6 or 2+4+6(I got and moved out of the loop)
The solution I am thinking here is very long.I have to put seperate loop for each number and also I am saving the divisors in a string.
Can anyone give me "Hint" how to start the problem without the Dynamic Programming(Dp) approach as I am learning Dp these days.
I assume you have a method
int[] getDivisors(int number)
the hard task here is to evaluate the possible sums.
An easy but long way would be, to iterate over all of those like that.
Think of such a sum as a binary code, 1 if its in sum, 0 if its not.
Now you can write a for-loop which, from 0...0 to 1...1 (in binary) with #1s = divisors.length;
Maybe a better way would be recursion methods. Like:
boolean hasSameSum(int number, Stack<Integer> divisors, int sum){
if(sum==number)
return false;
if(divisors.isEmpty())
return true;
int div = divisor.pop();
return hasSameSum(number, divisors, sum)&&hasSameSum(number, divisors, sum+div);
}
But honestly, that's not a programming problem, but more a mathematical approach problem :-)

How do you make an array that has 1-3 int, instead of the default 0-3 int? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
How do you make an array that has 1-3 int, instead of the default 0-3 int?
like
int[] anArray;
anArray = new int[3];
has numbers 0-3 I want index starting at 1
You make an array of four elements, and then never use the '0' element.
Sorry, but you can't change the language.
The index starts at 0. You'll just need to set The variable different in your loops if it helps readability
Instead of for I=0; i
You'd use
For I= 1; I
Would recommend you get use to indexing from zero though.
Apologies for typos. Using phone
You don't, arrays in Java start their indices at 0.
And new int[3] does not have numbers 0-3, it has indices 0-2.
Java doesn't allow this. In most programming languages, array indexes start from zero. And in the cases where they start from one, you can't change the lower bound.
The only main-stream1 language I can think of where you could specify both the lower and upper bounds of an array is Ada. (And recent versions of Fortran too ...)
1 ... and calling Ada mainstream these days is "a stretch".

Categories