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

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.

Related

How to check whether all valid values entered to my program will be correctly calculated? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I created a same sized - stack based simple calculator for adding 2 operands. I want to know whether my addition becomes incorrect when specific (valid) values are entered. The 2 stacks take integer values and are having the same number of digits (i.e. 400 [3 digits] and 900 [3 digits]).
It depends on the algorithm you're using. From the question it is not clear, but let's assume your calculator can perform basic arithmetic. First off, you want to test each operation separately, because they have different equivalence classes of their inputs. For example, for multiplication it would be: 0, 1, minimum and maximum values, and their negations. Testing almost always will not be exhaustive, but using equivalence classes, you can pick one value from each class to make sure that each class is covered with a test.
Back to your question, you may use min/max values, and anything that you think may break your code.

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

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

how to get the largest sum of indexes in array that have a negative numbers in java [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 5 years ago.
Improve this question
hy , i'm a beginner java student and we were asked to take the sequence of indexes with the largest sum , the array have negative and positive numbers and may contain 0
our job is to get the largest sum of indexes that are in a row and it can be one index , with method not with algorithm because we haven't learned that yet
thank you for your help
I don't want to do your homework for you but here's some tips:
How do you know if a sequence is the largest?
When should you stop counting a sequence?
If the next number in the sequence is a negative is your sum getting larger?
How would you brute force this problem?
Do you have to brute force it? (No. Look at points 2 and 3 and think about it)
You should be able to figure it out from the questions above. If not I would definitely go ask your professor to get some help.
Now that you've figured it out, here are some questions to further your understanding:
What if I only wanted the largest 3 numbers in a row? 5? 7?
What if all of the numbers are negative? Positive?
If the parameters for question 5 and 6 were added into your question, even though you might think that they narrow down the problem, it actually makes it much harder to solve because of the constraints. This is why we need algorithms, so that we can solve problems with a set of constraints without having to brute force.

Arraylist index calculation java [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
I have array list that each indexes has value either 0 or 1.I want to find that continuously three indexes has the value 1.How do I find it?
Without actual code:
iterate thorough the array
count the 1s you found
if you found a 0 then start over the count
check after increasing the counter: if the counter reaches 3 then return with true
if you reached the end of the array and no more elements then return with false
Something like this, I hope I could help.

How to create random cell number for testing purpose [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
I have to create a random cellnumber 07939393914 for automation testing purpose.
Last 079393(5 digits) digits should change randamly.. each time test runs..
Can any one suggest JAVA code for this ? or else Selenium Java code ?
Thanks
Is this a number or a String? I ask as it has a leading zero.
Take you initial number as 7939300000
then add to it Math.round(Math.Random()*10000)
If you want it as a String, take your string as "079393" and use Integer.toString on the result above, then concatentate them
Use RandomStringUtils class.
String randomNumbers = RandomStringUtils.randomNumeric(5);
String phNo = 079393+randomNumbers;
var num=Math.ceil(Math.Random()*100000)
for random first 5 digit numbers
and add
var your_last_5digits; //as string to your last 5 digits
cellNumber='0'+num+your_last_5digits;

Categories