Dreke Droga

+ Follow
since Oct 10, 2020
Cows and Likes
Total received
In last 30 days
Total given
Total received
Received in last 30 days
Total given
Given in last 30 days
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Dreke Droga

Paul Clapham wrote:There's a concept called "N-and-a-half loop" which means that a loop will be executed N and a half times. This situation occurs when the loop looks like this:

It happens fairly often, and that's what Campbell has posted there. Only in the posted code, the "initialize" part is score = myScanner.nextInt() and the break-out test is score < 0. So the "initialize" and "break out" parts can be combined and put into the loop header.

But sometimes the "initialize" part is too complicated to do that and then the loop looks like this:

You don't see this very often, though, because there's somewhat of a prejudice against breaking out of the middle of a code block which dates back a couple of decades or so, back to when "structured programming" was the new big thing. So if the "initialize" part is too complex to roll into the loop header, people will make it into a method and then put that into the loop header.

Thank you for explanation. I understood your code. I don't know Structural programming. Although I didn't grasp what you said completely, I will go through this again. I will start learning Structural programming along with java now.
2 weeks ago
Thank you. This is definitely better code. May I please know how you analyzed your algorithm inorder to come to this approach?
2 weeks ago

Paul Clapham wrote:I put your code fragments into the "code" tags. Please do that in future so that it's easier to discuss code fragments. (Select the code fragment in your post and click on the "Code" button at the top of the box.)

Anyway: Look at line 3 in your second code fragment. Notice that you don't have the same thing in the first code fragment, so the two aren't equivalent.

Thank you for pointing out. I didn't notice that. Even no compiler error (Obviously). When I changed it, it stopped with one iteration while we want it to stop iteration only if input is -1. So I wrote below code.

This worked.

Thanks again.
3 weeks ago
Hello, Please point me mistake in my for loop.

I thought this is equivalent to below while loop:

3 weeks ago

Tim Moores wrote:It will not be the same Activity object, because Android does not know beforehand which activity will be called as the result of a startActivity call (that can change dynamically at runtime). This is easy to test by comparing the references of the different activity objects.

There are other circumstances under which an existing Activity object might get reused; the Activity Lifecycle documentation talks about that.

Thank you. I will print the references and verify. You said "Activity object might get reused". Can you please give me an example?

3 weeks ago

Norm Radder wrote:

will it be same Activity B object that got called previously or a new object

Why do you need to know that?  I imagine the old object is collected and a new one created as needed.

Well, I may not be able to give you why I need to know, but I need to concretely understand how the objects are behaving. Any reference to article or documentation in this subject area in order to understand will be greatly helpful.

Thank you,
3 weeks ago
I'm not passing the Activity B reference. Below is the method signature.

public void onActivityResult(int requestCode, int resultCode, Intent data) {
 super.onActivityResult(requestCode, resultCode, data);
 switch(requestCode) {
     if (resultCode == Activity.RESULT_OK) {
       String returnValue = data.getStringExtra("ab");
3 weeks ago
With activityResult, yes
3 weeks ago

     I'm new to Android. I have basic question. Consider two Activity A, Activity B.

     Activity A start Activity B and returns back to Activity A After Activity B process complete. Now image Activity B gets called once and it will have Activity B object created. once that is done, it will return back to Activity A. And if Activity B again gets called from Activity A, will it be same Activity B object that got called previously or a new object with new reference will be created for Activity B called second time?
3 weeks ago

Liutauras Vilda wrote:

Dreke Droga wrote:It's more than 5 attempts to get the success for easy program. What are the best methods to study and write successfully?

I won't be original here, but that's normal. Over the time you'll develop your senses what likely can go wrong in your so called "easy" programs and you'll be able to recognise those beforehand execution.

Other than that, you'll face problem solving failures in your career more often than you think. When you get more experience, you'll find yourself spending more time in planning and thinking about the problem, than you actually bang the keyboard. That will reduce surprises along the way, but won't eliminate unforeseen issues completely. The key would be, that you'd allocate some buffer for those.

Actually I try to write algorithm first and then code. Issue is if you look into my replied post example, It never occurred to me using for loops in operation on arrays in linear fashion. Seems programming thinking is different.
1 month ago

Junilu Lacar wrote:If you're going to be a programmer, you'll need to get used to failure. Programmers, even experienced ones, hardly ever avoid bugs. In fact, there's one style of development where you intentionally start with a failure. To people who develop programs this way, failure is nothing more than a convenient way to detect that there's more to be done to make the program work properly. If you can turn around and use failures to your advantage, then you'll have a much better experience as a programmer.

Thank you! I agree. We can learn more from mistakes. Your suggestion was helpful.
1 month ago
@Campbell Ritchie Below is one of the example question and my program:

Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target. You may assume that each input would have exactly one solution, and you may not use the same element twice. You can return the answer in any order.
Example 1:
Input: nums = [2,7,11,15], target = 9
Output: [0,1]
Output: Because nums[0] + nums[1] == 9, we return [0, 1].

The mistakes I made:
1. The for loop in creating array, I put that code at class level. (I'm still confused why it's wrong)
2. During array creation, in the if block, I used below code:

This is wrong because input.nextInt()==0 is taking another additional inout. Because of this wrong array is also being created.

3. I didn't make twoSum static.
4. The algorithm in twoSum method. I will not lie. Thats not my own thought. I borrowed it from one of my Java book examples and used it here. I don't know how to get to point thinking in two for loops for the solutions. It never occurred to me.
5.  In the below snipet, I didn't put int for e.

6. In the twoSum method, you can see in method level I assigned values before for loop.

7. There are few more errors I modified code through IDE suggestions.
1 month ago

      I'm new to Java and Android. I'm learning java coding. I'm going through the topics and practicing. But I'm failing in programs which are marked as easy in leetcode website. I don't know why I'm unable to write successful program at first chance even for the easy ones. I had to use IDE and correct mistakes to get the program successful. It's more than 5 attempts to get the success for easy program. What are the best methods to study and write successfully?

Thank you,
1 month ago

     I'm new to Java and Android. Just started. I'm going through Head First Android book. In the 3rd chapter, it discussed on starting another activity from launch activity and through Intent. In the launch activity for the button action onClick method, below is the code:

In the other Activity, ReceiveMessageActivity the message from launch activity, typed in edittext is displayed.

My question is EXTRA_MESSAGE is constant and static. How can the value is replaced by another text? Sorry if my question way too noob. But I'm noob!  
1 month ago