Zachary Griggs

Ranch Hand
+ Follow
since Apr 29, 2016
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 Zachary Griggs

I am making an Android app right now, and part of this app is performing analysis on a photo. The current implementation uses ColorUtils.colorToLAB to retrieve the color at a specific pixel, like this:

The issue is that this code is taking forever to run. Somewhat to be expected because it's iterating over thousands of pixels, but I'd like to reduce the time taken by this code as much as possible. I've benchmarked it and found that this part of the code is taking ~90% of the time for the procedure. I'm wondering if there is any way that is known to improve the performance, or an alternative for how to get the pixel of a color in an that performs better.

Javadoc of the ColorUtils class:
3 months ago
When you see words like "Has a" (i.e. Animal has an ArrayList of customer) it's usually an indication that it should be a class.

After all, how else can something really have an attribute/element unless it is a class or something similar to a class?
1 year ago
Realistically, it's only used in some very specific circumstances.
Demo is from here with slight modifications:

Code is supposed to increment an integer and report when that integer is incremented. Try it with and without the volatile keyword on MY_INT and see what happens.
1 year ago
You don't need to worry about volatile unless you are doing multithreading.

To increase performance in Java, when setting a variable (say an int), the new value will not be written into memory yet because writing that is costly. Instead, it will be cached so it is easier to read/write again, until it is later written to memory. In multithreading code, this is a problem because the cache is local to the thread. This means that one thread can set a value for an int, then another can read that int and get an old value.

So, volatile tells the jvm to never cache it. This means that any read/write operation is thread-safe on a volatile data type (note that incrementing is still not safe).

Use volatile in multithreaded code if multiple threads access data that can change.
Do not use volatile.. any other time.
1 year ago
Clearly this is the most efficient way of writing fizzbuzz   :
1 year ago

how does Saturn even have any moons?

Saturn has moons because of this code here:

In memory:
1) The object reference "temp" is made to point to a new Body object in memory, with name "Saturn"
2) Adds a copy of the object reference to solarSystem and planets
3) The object reference "tempMoon" is made to point to a new Body object in memory with name "moon1"
4) Adds a copy of the tempMoon reference to solarSysem (which points to a Map)
5) Adds a copy of the tempMoon reference to temp (which points to the Saturn object)

Now in memory we have:
- A Body object named "Saturn" with orbital length 120.
- This Body stores a reference to another Body. This other one has name "moon1" and orbital length 88

However much you re-assign temp and tempMoon, those reference copies won't change. This Saturn object will remain unchanged, pointing to moon1.

because we added temp to the set of planets BEFORE we added any moons to temp which in the first case was Saturn?

It doesn't matter what order you do it in here, because it's an object. Remember, "temp" is a reference, not an object. If you add a copy of that reference to a Set (points to the same object), and then edit that object using any of the references pointing to it, it will change everywhere because there is really only one object there, we just refer to it in many different ways.
1 year ago
So if I understand the question, you're asking why this line does not overwrite everything else in the Map that was added before:

The answer is because of how java passes arguments.
In your java lessons, you may learn that, "natives are passed by value, objects are passed by reference". This is incorrect, everything in Java is passed by value. It's just that there is no such thing as an object value, only object references.
With this in mind, consider what the program does, line by line.

This line allocates an object reference, of type Body. It also assigns the object reference to point to a Body object in memory (name is "Earth", orbital length is 356).

Here, the following things happen:
1) A method call to getName() is made. This returns a reference to the String with the contents "Earth". Due to String internal details, this may or may not be the same reference as used in other areas of the code, but this is unimportant.
2) A copy of the reference temp is made. Both these references point to the same object in memory, but they are not the same.
3) The put method then stores the copied object reference

This performs a method call to Set.put. This is where by value passing really matters. The following happens:

1) A copy of the reference temp is made. Both these references point to the same object in memory, but they are not the same.
2) This copy of the reference is passed to the put method
3) The add method then stores the copied object reference

Now, we re-assign temp.

This changes where the reference "temp" points. It no longer points to the Earth object, but a new object for Mars.
But remember, when we stored temp in the map, we did not store temp's reference at all but a copy of temp's reference. Therefore the thing we put into the map is not changed; it points to the old Earth object. We can go on like this and reassign references to new objects as we wish, and we are guaranteed none of our previous method calls will be affected. Note that this is NOT the same as calling methods that change the actual object that is pointed to. If we do that, then it will affect the data that is stored in those data structures because we're now changing the object itself, rather than just the reference.

Also, on a sidenote: consider refactoring your code. For example these 3 copy paste lines are just asking to be put into a method.

Something like this:
1 year ago
When running threads, there is no guarantee of any sort of ordering. You'll receive different orderings when you run this multiple times. Both of these were from running the app:

Duke: Who's there?
Coffee shop: Knock Knock!
Coffee shop: Duke
Duke: Duke who?

Coffee shop: Knock Knock!
Duke: Who's there?
Duke: Duke who?
Coffee shop: Duke

As to why: because the operating system decides arbitrarily what threads to run. There's really no predictable order that the operating system will run your threads. I tried to get this code working for some time, and was unable to get the threads to print in order. Due to the use of Exchanger, normal locking or synchronizing will not work. Threading is a difficult topic! Does the book have a solution that prints in order?
1 year ago
Parsing JSON is not something you should do yourself. There is a useful class called JSONObject made for parsing JSON. You can create it directly from a JSON string:

Then you can retrieve data from it by using the get methods. It looks like that JSON above is made up of 4 JSON Arrays, which then have the contents, so you could use:

And then use the getString methods to parse inside there.

1 year ago
From the StringBuilder source code: with a default constructor, capacity starts at 16

With a constructor that initializes to a String: capacity is size + 16

When appending: it only extends the capacity if the new length would exceed it.

When it DOES need to extend the capacity, it does so by taking the current size of the StringBuilder +1 and multiplying that by 2. If that's not enough, then it expands to exactly the amount of space needed.

Given the example from your post: I find it very likely that you were constructing a StringBuilder with "abcd" which would give a capacity of 20, not appending it.
Luckily you don't really need to worry about any of this. The only time I would explicitly set the capacity of a StringBuilder is if I'm going to create and use them like, thousands of times in a loop, or read in a file line-by-line that has 10,000 lines or something; then the performance might be significant.
1 year ago
Right. I always forget that interfaces extend interfaces instead of implementing them.
1 year ago

List implements Collection. In other words, a List is a more specific type of Collection. Therefore anything that works on a Collection will also work on a List.

This is also the reason why you should declare your variables and methods to take the most general type of object as you can and still have the method function as intended. For example, let's say we declare this function

This is almost always poor practice. Why? Because a LinkedList behaves the same as an ArrayList, so why shouldn't it work in this method? Instead:

Or - if you don't need the operations specific to List, you could declare it a Collection. And an ArrayList passed in would still work, but now the code is far more flexible. If later on, you decide that a different data structure would be preferable, you only need change the declaration of your List. You don't need to go through and change all the different methods that do operations on it.

Also consider the following code:

Likewise, this is usually poor practice. Why do you need an ArrayList declared? You can declare it:

In the same way, when you create an interface and have multiple objects implementing it, you can declare a reference variable of that interface type instead of the concrete implementation. Then you can swap out implementation details however you wish, and know that it will still fulfill what you need.
1 year ago
That is just another way of saying what the original sentence said - but yes that is correct.
1 year ago
The people posting in that thread explained it quite clearly.

While a String does use an array of characters internally, args[] is not actually an array of characters. It is an array of Strings, which are an Object that internally uses an array of characters to represent it. There's no difference in C because there aren't any objects, but here there is. For example, you can call instance methods on Strings to create new ones based off the old, and such like that. Java is not the same as C, your knowledge of C strings doesn't apply here.
1 year ago
What exactly was your assignment? Just to come up with an example of OOP? Because I don't see any real mistakes in the description itself; it shows OOP. Was the UML part of the assignment, and if so how accurate does it need to be, do you need to code this out later? Because there are some mistakes in the UML. If it's supposed to be a UML you can later code off of, then there will need to be some corrections.

UML is not loading on this page but works if you go to the source for it. Here is the embedded version:

UML Feedback (not in any order, just as I notice it)
- Why does Company implement WorkingHours? Did you mean employee?
- How would workTime() function? Work has a start and end time, but there's only one function. What would be an example return value from it?
- whatIsTheDeveloperWorkingOn() is not the best method name. Perhaps getProjects() or would be better.
- You declare several variables as "set". This is not specific. A Set of what?
- You underlines the class names in the UML (all besides Project). This means they are static, I believe.
- No multiplicity is stated from project to developer. How many projects are there per developer? If there's only 1, then it can be 1 to 1. If not, it should be declared like: 1 developer to 1... project, or 1 developer to 0... projects (if they can not be working on anything).
- I'm not sure experience as an integer field makes much sense. What is 1 experience? Years?
- No return types are specified on the UML diagram. For example: getEmployeeInfo() - what does this return? A String? A custom EmployeeInfo class?
- No constructors specified. This means that your private fields can never be set in any way.
- addProject() takes no arguments. What project are you adding?
- Make sure all your fields make sense where they are. For example: what is the purpose of a Company having time?
- Company should probably have some employees. Right now it has a count of employees, but not any actual employees.
Try generating code from that UML (either using a tool or manually), and write it EXACTLY as it appears in the UML. See what you can do with it. If it doesn't function, then first make adjustments to the UML, then code exactly what it says again.
1 year ago