Christopher McKay

Ranch Hand
+ Follow
since Oct 19, 2013
Christopher likes ...
Android Eclipse IDE Java
Merit badge: grant badges
For More
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
1
Received in last 30 days
0
Total given
26
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Christopher McKay

Matthew Brown wrote:Euclid's algorithm is a classic recursive algorithm. Have you considered trying to implement this with recursion? It's incredibly compact.


Sorry, I don't quite understand what you mean, could you give me an example?

EDIT: Looked up some examples and recursion is definitely the way to go with this algorithm. Thanks for suggesting recursion!
9 years ago

Paul Clapham wrote:I was confused about the if-statement in lines 5 to 8. After staring at it for a while I figured out that its purpose was to make num1 the larger of the two parameters and num2 the smaller. I would have been spared all that if you had called the variables "larger" and "smaller", or something like that, rather than "num1" and "num2".

And then you have a variable named "product", which given the context sounded to me like you were multiplying numbers together. Perhaps "result" would be a better name.

And then you've got a "previousProduct" variable which you initialize to number1. Choosing the number1 variable as the initial value led me to wonder why you chose that until I noticed that it didn't matter. But with a little code change you don't need to pre-initialize the variable at all.

Point being, variable names are important. Make them meaningful. Here's my revised code based on that advice:



Thanks, I should have noticed that 'previousResult' didn't need to be pre-initialized. Also, sorry about the variable naming, I will make sure in future to try keep variable names useful :)
9 years ago
After learning about Euclid's Algorithm for finding the greatest common divisor of 2 integers, I decided to make a demo program in Java. My questions are: Is the following code good/efficient, and how can it be improved?

9 years ago
Java, to me, is a really nice language to program in. I have been looking forward to the release of Java 8. What features that come with Java 8 do you like most?
10 years ago

Keith Earl wrote:

Christopher McKay wrote:

Ulf Dittmer wrote:I know nothing about LibGDX, but it seems that any code generator that can create code for several platforms should have a mechanism for hooking up stuff for one particular platform. (At least, I don't think I'd want to use one that didn't have that capability.)


Ok, I'll look into it and post up what I find.



I know this thread is a bit dated and since I haven't seen any replies to it I figure I'd put my $0.02 in. I'm by no means any expert in java and in fact am just starting off learning it myself. Well I was dabbling with libgdx while studying java se (which java se should be learned first) and I remember running across something about Modules used and what you're asking about was with the Application Module which deals with Gdx.app. With that you can query the platform type and write platform-specific code where it's necessary to know the platform type. And example is

switch (Gdx.app.getType()) {
case Desktop:
// Code for Desktop application
break;
case Android:
// Code for Android application
break;
case WebGL:
// Code for WebGL application
break;
default:
// Unhandled (new?) platform application
break;
}



Again, I'm just starting off with java, but maybe you could add code where needed in order to change things JUST for android (for example) while leaving the desktop coding just the way it is?



Thanks Keith Earl, I didn't realize you could do this. My problem has been solved by you!
10 years ago

Barry Burd wrote:Christopher,
"When" is a difficult question to answer. Some people learn it as their first programming language and others learn it many languages later. I have teenagers e-mailing me about learning Java and 70-year-old retirees. So "when" isn't easy. As for "why," I like Java because it has what I think is an elegant system of object-oriented programming. Other OOP languages have all sorts of rules that I have to memorize. In Java, the OOP rules seem natural and intuitive to me.



Thanks for the reply Good luck with your books!
10 years ago
I have recently started learning Java, and I have enjoyed programming with the language so far. When did you start programming in Java, and why do you prefer it to other languages?
10 years ago

Pawel Pawlowicz wrote:1. Start checking divisors from 2 (instead of forI - 1).
2. Check divisors up to sqrt(forI).
3. Apart from 2 don't try to divide by even number (if a number is even, it is not pime -- except for 2).
4. Check this: Sieve of Eratosthenes

On my PC calculating using your program took 19 seconds.
After I modified it acording to points 1, 2 and 3 (I didn't even try to implement the Sieve) it took... 23 milliseconds. It was almost 1000 times faster!

Apart from that:
Those variables should be declared in main method.

Also, consider writing isPime method. Your code would look cleaner and nicer.



Thank you, I managed to decrease the time it takes to 2.4 seconds so far. I had a bit of a problem when using sqrt. Here is my code, could you tell me where I could fit it in?

10 years ago
I have been working on a Project Euler problem which is to find the 10001st prime number. I made the project in java and it gave me the correct answer (104743) . I couldn't help but notice that it had taken 17 seconds to find the answer. I am fairly new to java and I would appreciate feedback on how to improve the efficiency of my Java program - at the moment it is bruteforce.

10 years ago

Piet Souris wrote:Can't you add the newly created snowflake to some ArrayList of snowflakes
before adding it to the layout?

But, I guess it is time for an Android expert. Hope someone will step in.

Greetz,
Piet



Ofcourse, I was thinking of using arrays, but I never thought of using arraylists. It has been a while since I used them, is the difference between lists and arrays that you can add and remove from arraylists and it resizes the array?
10 years ago

Piet Souris wrote:hi Christopher,

I know nothing about Android, but in a Java program I would do a thing like this:

1) have a Timer object fire every so much milliseconds
2) then call an 'updatePanel/Frame/whatever() method
3) in this method, you update the Y-values of every snowflake you have. Maybe even some
random disturbance of the X values.
4) do the repaint()

But, I don't know if Android works in a similar fashion.

Greetings,
Piet



The problem is, the snowfake 'id' if you will, is lost after the user touches the screen again. e.g. I am basically just calling determining what snowflake is what to set the Y of them might be impossible/difficult.
10 years ago
I have made a class (snowflake) which extends android ImageView, this is what is in it:



then in my main class, I create an instance of snowflake when the screen is touched:



I know that this is android, but this question isn't exactly android related, my question is, how do I make the snow fall to the bottom of the screen. I have tried a for loop, but the snow instantly appears at the bottom of the screen. Help would be very much appreciated!
10 years ago

Paul Clapham wrote:I wouldn't call it a common source of bugs myself. Here's an example of the sort of bug which might occur:

If you had the letter "I" then you would expect toLowerCase() to convert it to "i", right? However if you ran your code in the Turkish locale, you would be using the rules which Turkish uses, namely to produce the "dotless I" like this: "ı". (You can read about that here: Dotted and dotless I.)

You may or may not consider that to be a bug. There are three possibilities:

(1) You want I to be converted to ı when your program is run in the Turkish locale. In this case, leave your code as it is, using the system's default locale.

(2) You want I to be converted to i no matter what locale is the system's default. In this case, pick a suitable locale and use that as the parameter suggested by the message.

(3) You don't care what happens because you don't expect your program to be run by anybody except you. In this case ignore the message.

For background information, here's a link to the relevant Oracle tutorial: Lesson: Setting the Locale.



Thank you both for such detailed responses :)
10 years ago
When I use string.toLowerCase() I get the warning (in eclipse): "Implicitly using the default locale is a common source of bugs: Use toLowerCase(Locale) instead". Could someone explain this in clearer words and how to fix the problem (I have no experience with Locales). Thanks
10 years ago
I received my copy of Murach's Android Programming yesterday and I've been reading through it. There is so much in the book that will help me with future android projects (especially the GMaps part), so I would like to thank Joel Murach for coming over to the Ranch and holding a competition. Bye for now!
10 years ago