Ryan McClain

Ranch Hand
+ Follow
since Nov 27, 2010
Merit badge: grant badges
For More
Cows and Likes
Cows
Total received
1
In last 30 days
0
Total given
0
Likes
Total received
4
Received in last 30 days
0
Total given
28
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Ryan McClain

We need to distribute x amount of dollars evenly between 4 people. When you change the share of one person, the rest of the total share gets distributed evenly among the other people.

Simply put, it could be implemented like this:
4 sliders. Change the value of any slider and the rest of the sliders automatically gets the remainder of the amount distributed evenly.

Here are a few examples of that:

person 1: 70%
person 2: 10%
person 3: 10%
person 4: 10%

person 1: 50%
person 2: 30%
person 3: 10%
person 4: 10%

I was thinking of using the Observer pattern for this but I'm not sure because each dependent seems to be a subject.
Ohh.. I was reading it wrongly: from LTR instead of RTL. I also forgot about the byref/byval principle, which made me confused here.
Yes, the last line nullifies the last element in the array.

Thanks

Henry Wong wrote:

Ryan McClain wrote:
I don't understand what the remove() method is doing in relation to what was stated: how are we moving everything else down by 1 if list[i+1] seems to be moving everything else up by 1?



I recommend using a pencil and paper, draw out an array, picking one of the elements as index i, and follow the for loop in the example (basically, pretend you are the JVM). You will see that the algorithm is indeed moving everything after index i down by 1.

Henry


I did that before I posted this question and it doesn't help me.. here's what I wrote down:



I must be getting something wrong.
A snippet from Head First Design Patterns:

I don't understand what the remove() method is doing in relation to what was stated: how are we moving everything else down by 1 if list[i+1] seems to be moving everything else up by 1?

What exactly is happening in the remove() method? The current explanation doesn't inform me well enough.

Junilu Lacar wrote:

Ryan McClain wrote:
My arguments to this are:
- Looping through an ArrayList is an incredibly inefficient way to implement this. For every topping, you would have to traverse the ArrayList.
Additionally, ArrayLists are terrible at sorting and searching, inserting an deleting because the underlying algorithms are of O(n) complexity.

- Not only is the loop inefficient, but you are asking the JVM to traverse a complex object graph each time, which adds to the inefficiency.

In the end, this is bad for performance.


How do you know it's really bad for performance? Did you perform benchmark tests? Can you back that up with quantitative data? Or are you optimizing based on what many programmers have been proven over the years to be very bad at: gut feeling and intuition? If you had 15 million different toppings, and customers could add 13 million toppings to an order, then you *might* be justified to worry about performance. But a typical pizza shop has what, 20 different toppings, maybe 40 tops (pun intended)? Is running through a list of 20-40 items really going to tax your computer to the limit of its capacity? I think not.  It all smells like premature optimization to me (Donald Knuth calls it the root of all evil).


I don't need to benchmark something that has been proven to be bad for performance. We are scientists, so of course we don't act on intuition. Thus, we use algorithmic complexity theory to know what language constructs are bad for performance and which ones are not (CRUD on an ArrayList is O(n) complexity. I have no doubt you know about this). Since we know that O(n) grows exponentially, things can get out of hand really fast. We don't need to benchmark this to know it will be bad. I could reach into my Algorithms syllabus and show you how fast O(n) grows, but I don't need to because we both understand it.
I think it is important to think for yourself. It's not like Donald Knuth is a god of all truth.

Now, going back to the video, there were a few things I didn't like about how he implemented his ToppingDecorator and other classes. First, in ToppingDecorator, he called his field "tempPizza". That's lazy. Why "temp"?  Bad name.


As you know, programmers are lazy. A lot of programmers say being lazy makes you a good programmer, but I don't believe that.


Conceptually, I don't like the Pizza and Toppings example for Decorator. I always imagine a Decorator as being the same kind of thing as what it is decorating, like in the canonical example of a GUI Window being decorated (wrapped) by a ScrollableWindow which can be wrapped by perhaps a ResizableWindow, etc.  I've never liked the Pizza/Topping example, nor the other common Coffee/Additions example.  Pizza and the Toppings that you can put on it don't have that "same kind of thing" relationship in my mind.  Jalapeno, Cheese, Mushroom, Sausage are not special kinds of Pizza. They are just things I will add on top of a pizza.  Same thing for Coffee to cream, sugar, nutmeg, etc.


Yeah, but this video is made for laymen and the common layman doesn't understand GUI Windows. I believe this is why he used that example. Then, he argues in his other videos that people should trust him because he's been programming for 30 years (which I don't think is a valid argument). Now that you mention it, it is indeed a bad example; toppings are not a type of pizza. He also admitted in the comments that he wrote all of the code from the top of his head (which I think is bad practice). Well, anyway.


Thank you for your insight. It was helpful.

Please observe the following video on the Decorator Pattern:


I saw someone challenge the Decorator Pattern in this video and providing the following arguments:

But why would you do that instead of just making an abstract class topping and defining many different toppings and having an ArrayList of those inside your Pizza. If you want to know the price of the Pizza instance now, you just have to loop through the ArrayList inside your getCost() method and add the price of a plain Pizza to it. Same goes for the description. Just call getName on all the toppings. Besides now you coul have all the toppings subscribe to a sales department or something like that and have it manage their prices.



What is wrong with his approach?
What design principles are being violated there?

My arguments to this are:
- Looping through an ArrayList is an incredibly inefficient way to implement this. For every topping, you would have to traverse the ArrayList.
Additionally, ArrayLists are terrible at sorting and searching, inserting an deleting because the underlying algorithms are of O(n) complexity.

- Not only is the loop inefficient, but you are asking the JVM to traverse a complex object graph each time, which adds to the inefficiency.

In the end, this is bad for performance.

If someone could provide better insight into how his solution is a bad one, I would love to hear it.
Thanks.


Thanks for the replies everyone. It helped me out.
8 years ago
If you use L as a number suffix in an arithmetic operation, then does that mean that all the numbers that follow it on the same line get converted to long types as well? Thus, 365L * 24 * 60 * 60 * 1000 actually means 365L * 24L * 60L * 60L * 1000L ?
8 years ago
This is probably too late, but here are some tips:

1. Write a main function and keep running the code inside it until the user provides a right answer.
2. Print out the question
3. Use a variable that holds the user input
4. Write an if-else statement that checks for the right answer.
5. Escape the program when the user provides the right answer.
8 years ago
This statement:
gives me a 'Numberoverflow warning' in my IntelliJ IDE.

What exactly makes this number overflow? What component is overflowing? Does the value at the right side evaluate as an integer and therefore overflows the maximum integer range? It does not automatically convert to a long type (since I am assigning it to a long)? I don't understand much what's happening here.
As I understand Java, the syntax is: destinationVariable = assignmentValue. Logically follows that I am assigning a value of 31.536.000.000 (31 billion) to a constant that holds a long type. That should be enough to hold the value, right? Doesn't Java automatically cast less specific types (integer) to more specific types (long, double)?

The solution is this:

Why do I explicitly have to convert that value to a long?
8 years ago
On this Minecraft server control panel website (which I didn't write), you can see the amount of players currently logged into the server in the 'Online Players 0/8' section at the top right.

I want to listen to the DOM for changes in that particular 'Online players' text. If the number changes to anything greater than '0 players online', I want to generate an alert('new player!'); and possibly play a sound.

What would be the best approach to do this? What scripting languages? I prefer to work with languages I already know such as Javascript. I read online it should be possible with jQuery.
I tried to inject a script at runtime when the page is loaded (Chrome Developer tools -> Elements section), but that doesn't seem to trigger anything. I tried to put jquery code between the <script></script> tags of the <head></head> tags, but that didn't seem to do anything. Not even a simple alert(); works, so I'm not sure what I'm doing wrong.

Here's what I know:
The control panel seems to be refreshing (polling) the 'status_onlineplayers' td element every second. If you inspect that element, you get this code:
<td id="status_onlineplayers">0 / 8</td>

So, does anyone know how to make this happen? I haven't been successful thus far.

Thanks.
For my college internship at a company I am required to have strong analytical skills (basically for understanding a project assignment, understanding the problem domain, finding the right patterns (OO, UML, etc), the right system design etc).

My question is: how do you develop strong analytical skills? Is it a talent you are born with? Is it something you can learn in software engineering books?
I've been through all the software engineering (101, 201 and 301) classes and I passed them, but I am afraid of not having these skills.

This is where you have to change it: Run/Debug>Console>Standard out text color
It works, I tried it myself.
8 years ago