Ryan Sykes

Ranch Hand
+ Follow
since Jan 18, 2012
Merit badge: grant badges
For More
Cows and Likes
Cows
Total received
In last 30 days
0
Forums and Threads

Recent posts by Ryan Sykes

Thanks a lot for your help Michael. I really appreciate it. I'll keep that in mind the next time I'm working with MouseListeners.
12 years ago
Thanks a lot for your reply Michael! That worked perfectly and makes sense. I should have thought of that ahead of time.

On a related note, is there any guideline on how frequently the MouseMotionListener polls the mouse movement? The reason I ask is that I had written a similar program, that drew a circle and allowed me to click inside the circle and drag the circle around. However, I noticed that if I jerked the mouse very fast, the circle would no longer follow the mouse. It turned out that when I jerked the mouse very fast, between two consecutive polls of the mouse position, my mouse moved outside the circle and thus triggered the circle to stop following (my basic logic to start the drag was to keep updating the center co-ordinates of the circle to those of the mouse as long as the mouse was within the circle).

I was able to fix this by using a boolean flag that was triggered when I started a drag event, thus causing the circle to always follow the mouse during the course of the mouse drag action. Still, I'm curious about the fidelity of the MouseMotionListener and any advice/best practices involved with implementing it with regards to the finite polling rate of the mouse location.

PS - I can post the full code to the program if needed.
12 years ago
Hi folks,

I've been learning Java and OOP in my spare time for fun lately. After finishing with Head First Java, I'm currently reading through Daniel Liang's excellent Introduction to Java Programming Comprehensive and Core Java Vol 1. I'm currently programming some of the exercise problems at the end of the Event driven programming chapter of the first book. Here is the exercise problem:


Write a program that displays a circle of radius 10pixels filled with a random color at a random location on a panel. When you click the circle, it is gone and a new random-color circle is displayed at another random location. After twenty circles are clicked, display the time spent in the panel.



I've coded this up and it works perfectly, except that when clicking in rapid succession, some of the clicks are not registered. I have noticed this with other programs I have written as well, and when I run the compiled class files provided by the author of the book (to try running all the exercise problems), I observe a similar behavior. Is this a concurrency issue due to conflicts with the GUI EDT? I haven't read up on threads and concurrency yet, but if that is the case, I'd like to identify where the issue is being produced. With the bug as is, this exercise is a bit pointless because you can never get a great "score" due to the unregistered click events.

PS - I have added in some stuff to avoid bugs when resizing the window. I know that I could have set it to not be resizable, but I was just playing around with the code to see if I could handle that case without making a hash of things. Also, I'd appreciate any general comments about the program and what I could do to improve it.



12 years ago

Winston Gutkowski wrote:

Ryan Sykes wrote:An abstract class cannot be instantiated, and must have at least one unimplemented (i.e. abstract) method.


The second part of that is not true (although more often than not an abstract class will have at least one abstract method); perhaps you're thinking of C++.


Thanks for the correction Winston. I was under the mistaken assumption that abstract classes had to have at least one abstract method. I guess if it didn't, it would be equivalent to a non-abstract class with a private constructor?

Jeff Verdegan wrote:

Winston Gutkowski wrote:

Jeff Verdegan wrote:Composition is a HAS-A relationship, not IS-A.


Yes, but it can be used to simply extend a class's capabilities, especially when you're not really sure if you want to tie them together or not (or publicize the relationship to clients). I guess what I was trying to say is that inheritance should be used sparingly.

Winston



Exactly. Prefer composition over inheritance, as they say.


I found this article on Composition v/s Inheritance to be quite helpful in understanding the concept when I was looking this up a week ago: http://www.artima.com/designtechniques/compoinh.html
12 years ago
An abstract class cannot be instantiated, and must have at least one unimplemented (i.e. abstract) method. However, it can have other completely implemented methods that are inherited by subclasses. In the case of interfaces, all methods are abstract.

This link has a nice description of abstract classes v/s Interfaces: http://www.codeproject.com/Articles/11155/Abstract-Class-versus-Interface. It should help you understand the role of each and when you would want to use one over the other.

I think you need to clarify your understanding of Inheritance. "Inheritance or Abstract class" doesn't really make sense as Abstract classes are vital parts of class hierarchies, and the subclasses of abstract classes do in fact inherit from their parent abstract class. Perhaps what you are looking for is Concrete v/s Abstract classes.
12 years ago
Matt, you will probably want to have an int instance variable in SurgeProtector, like int lastIndex, that keeps track of the lastIndex of the elements inserted into the array. Use that index value to add an element to the array and then increment lastIndex by 1. I'd recommend looking at the ArrayList source code in Java. I always find it fun to see how things are implemented and you can learn quite a bit. ArrayList essentially consists of an internal array, that is dynamically resized (rather, duplicated) when the size of the ArrayList exceeds the initial size of the internal array representation.
12 years ago
Hi Juan,

I did notice this issue when I read the book. I found the solution on the forums for the book (although surprisingly, there was no response from the authors on the book forums). Dennis, thanks for looking it up and posting why this solution works. I didn't really dig too deep as I figured that I probably never would be using MIDI in anything I write. I was a little put-off that the book chose a MIDI based application as the main programming project, as it seems that they ended up devoting far too many pages on explaining how to get a working MIDI sequence...pages that I personally feel could have been devoted to some of the things they left out in the book. Still, it is an excellent introductory book to OOP concepts and to Java.
12 years ago
Shouldn't you also post the main program that invokes CalendarGUI, since that is what is likely causing the issue? If I understood you correctly, the CalendarGUI works and displays correctly when tested separately?
12 years ago

Rajesh Shekar wrote:Also i assume if i move the method outside main i need to create an object for the class "MethodTest" and call the calcArea method is that right?


Not if you declare the method to be static (which it should be in the case you listed above). A static method from within the same class can be called by its name alone, i.e. calcArea(w, h). If you are calling it from another class and if the function has the appropriate access modifier (eg: public), then you should call the static method by ClassName.staticmethodname(), i.e., MethodTest.calcArea(w, h). Note that this method call is not dependent on any instance of the MethodTest class.
12 years ago
Couple of things:

1) It should be "four" not four. The former is a String, the latter just looks like an undefined variable name to the compiler.

2) More importantly, even if you fix the issue in 1), Strings in Java are Objects, not primary data types. As a result, you should not compare equality of Strings using ==, only use that for comparing primary data types like int, double, char, float, etc. When you do an == comparison on objects, you are actually checking if both object references are referring(pointing) to the same object. This however does not do a check based on the contents of the 2 objects, and is not a correct way to check equality. For Strings (and all other Objects), you will want to use the equals() method.

So it should be something like if(x.equals("four")). Note, you could also re-write it as if("four".equals(x)).

Also note that when you start writing your own classes, if you want to check the equality of 2 objects of your custom Class, you will need to implement your own equals() method for your class. Read any Java textbook/reference for more information on how to do that. Otherwise, Google is your best friend
12 years ago
Here are some of my must have apps:

1. Chrome Browser Beta (on ICS)
2. Newsrob RSS News Reader
3. Android Agenda Widget (Best, free calendar widget app)
4. Google Music for offline and cloud music - Integrates well with ICS OS
5. Widgetsoid - For customizable powercontrol style widgets in the notification menu/homescreen
6. Weatherbug for Temperature notifications in menu bar and pushed notifications for local weather alerts
7. Beautiful Widgets - I primarily like their 4x1 weather widget
8. Realcalc - Great, free calculator app

If you are rooted:
1. Android Wifi Tether
2. Titanium Backup Pro

And it goes without saying that the integrated GMail and Google maps apps are essential and pretty awesome
12 years ago
Also, you should follow naming conventions and call your method compareTo() not CompareTo ...which would be confused as a Class name.
12 years ago

Christina Bremmerman wrote:
Ah I know of this...and I guess I missed it because of my other issues

I can either change the 100 to 101 or change the 99 to 98? Would it work as well to change >99 to ==99?


You could do any of those...they would all work. == 99 might be the clearest from a clarity perspective (at least to me).
12 years ago
Okay, so that part is clear. So from your answer above, and the code you had previously posted, the criteria for the while loop should be something like while(integer != -1). So that is a start. Now, what should go within the while loop? Also, since the loop is checking the value of the int integer, how is this variable being initialized? What do you think should precede the while loop?
12 years ago

Heather Hamrick wrote:I have a while loop because thats what my professor has told us to do within our program, so I guess that should be in my main?


But you're not trying to understand why...you seem to be making guesses. So why do you think your professor asked you to use a while loop in the program? He/she obviously had a reason to ask you to use one. Just inserting one for the sake of it, and then moving it around is obviously not going to be of any use till you understand why your program needs a loop.

So - Why do you think a loop is needed in this program? What purpose should it serve?
12 years ago