Win a copy of Learning OpenStack Networking: Build a solid foundation in virtual networking technologies for OpenStack-based clouds this week in the Cloud/Virtualization forum!

Piet Souris

+ Follow
since Mar 08, 2009
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 Rancher Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Piet Souris


Say, you have the word (= String) s, and you put it on the boardd, starting at location (x, y), going in the direction of (deltax, deltay), you would get someting like:

Before I get hanged, this is of course just a possibility. In reality you would probably use Tiles, BoardFields, et cetera, all more object oriented. But as I said: it is an easy way to start.

One other remark: as you see, I used the method map.getOrDefault(key, defaultvalue). That means that if the key is present, its corresponding value is returned. If it is not present, then the default value is returned. So,I can leave out all the chars from letters that have a value of 1, and all the boardfields from fields with a value of 1, since I specify 1 as the default value. Saves a lot of typing!
1 hour ago
Again, I do not quite understand this reply. The whole issue is to come up with a class that implements a Comparator<Film>, where the comparing is based on the average rating of a film, supplied by a Map<Film. List<Rating>>.
So, the only thing about Maps is the Map that is input to the FilmComparator class.

That is all. There is no mentioning of 'sorting a HashMap' or similar. Given that we have that FilmComparator class, and let comp be an instance of that class, then we can simply do:
16 hours ago
[quote=Al Davis](...)
Piet - It does seem like figuring the averages in the Rating Enum might simplify things; I haven't tried that yet, but I will.  Any suggestions as to how I should handle re-associating the averages with the HashMap keys?  I'm thinking I'll need to create another map (or a set, or something) to pair up the keys and averages for the actual sort.
You can create a separate Map, if you like. If you have a Map<Film, List<Rating>>, then it is an easy matter to create a Map<Film, Double>, where the Double represents the average value of all the Ratings. If you only want to, say, print the averages, then you can do something like:
[code=java]ratings.forEach((k, v) -> System.out.format("Film: %s, average rating: %f %n", k, Rating.averageRating(v));[/code]
17 hours ago
hi Jordi,

Welcome to the Ranch and enjoy the stay!

There are some ways to calculate the value of a Scrabble word. An easy way is to work with HashMaps. A HashMap maps elements called 'keys' to other elements called the values. It is therefore an extension to the array, that maps integers to other elements.

I'm thinking of two HashMaps here. One that maps characters to their normal values (HashMap<Character, Integer>), and one that maps a Location (a square of the board) to a bonus integer (a simple solution could be a HashMap<Point, Integer>). So, if you have a char at some fileld, look up that char's value in the first Map, and look up, given its position, if it gets some bonus value according to the second Map. To reduce the length of that second map, you could only mention those positions that do have a bonus value.

So, I'd say: read about HashMaps.
20 hours ago
There is talking about sorting a (Hash)Map, but that is not the question. The idea is to create a class FilmComparator, where the comparing of Films must be done based on the list of Ratings, given in the constructor-Map. That means also that that Map can hardly be a TreeMap, since you would need a FilmComparator for that TreeMap (or let the Film class implement Comparable<Film>, but that sorting would be useless for this exercise).

To simplify things, I suggest to add a static method to the Rating enum, that determines the average of a List of Ratings. You could use the avarage method of an IntStream here, but it is easy enough to determine the average "manually".
20 hours ago

Campbell wrote:I know you have been told to write comments to explain the code, but most of those comments don't say anything that isn't already obvious from reading the code. Methods shouldn't have // comments anyway; they should have proper documentation comments with /** ... */ .

These comments, perhaps looking superfluous, are not meant for some reviewer or so. When I started, I used that kind of comments too. But the intention was to reassure myself that I knew what I was doing, and where I was doing it. In this forum this remark about unnecessary comments is quite often made, but I think that this is just part of the learning process (for the same reason I believe that a Beginner is entitled to a 'Main is a Pain' type of coding).

There are at least two problems with your code. First of all, you did not mention it, but didn't you get an ArrayIndexOutOfBounds exception? Secondly: you are comparing pairs of consecutive elements. Have a look at this topic: example. Do you notice an important difference?
1 day ago

Jordi Sedo wrote:Sorry Piet, the nbrAuthors it’s a variable I have defined in the OnlineCourse, and initialized it at the constructor. So I had to stop using the static modifier for my sorting method so I could use nbrAuthors as the limit of my for

I understand, but the Author array is a parameter to the method ('private void sortStringBubble(Author b[]) {...'), so can be any Author array (that is why Campbell said this method could be static), and in the body you are using a variable that is specific to a certain array. That is incorrect. So, either make this nbrAuthors a parameter too, or in the body of the method, check from what index the nulls start.
1 day ago
This is an assignment, concerning possibly partly filled arrays. This is not common, so putting it in a utility class seems not obvious. But why isn't the variable 'nbrAuthors' a parameter in this case?
1 day ago

Carey Brown wrote:Streams...
The String class has a chars() method that creates a stream of characters as ints. What I want to put after that is a black box that keeps taking in characters and buffers them and has a state machine to determine when it needs to take some of the buffer and output a Stream of Strings. I'm not familiar with any Stream operations that take one Stream and turn them into another Stream that has a different rate of input to output.

It is possible to write a suitable Collector, that takes chars one at the time. Problem is that when a space is found, it is not known whether to split the String at that point. You would have to build in some complex delay system.

Here is my first Stream version, but it is not very elegant, to say the least. The non-stream solutions are much easier. But it makes for a decent exercise.
1 day ago
Well done!

But if you are working with an array that may or may not be fully filled, why not use a List (an ArrayList or so)? That would be much simpler.
1 day ago
hi Jordi,

I have not checked your code yet, but could there be some nulls in your Author[] array?
1 day ago
Hmm...   hopefully that doesn't spoil my well deserved nightrest.
1 day ago
You're welcome!

@Campbell and Paul
that was still hard... what on earth is 'escaping html'?
But finally I managed, and using the 'x' that I constantly overlooked helped as well! Indeed, me dummy, and for dummies like me: to get that Celsius symbol I had to type "& #x2103" (without the space), and to get "&#x2103" I had to type "& amp#x2103" (again without the space).

Thank you, guys!
1 day ago
The assignment  mentions Student records, and an array of these. All I see are some arrays, one for the name, one for the id, et cetera. Are you sure that that is as it is meant?
3 days ago
Still puzzled...

If I have this MyTask

Then the code compiles fine, but returns the wrong answer (3, the result of getRawResult).
Since the class is abstract, I can leave out that getRawResult method, but then I get an error in the FindMin class, that the gerRawResult method is not implemented. Only if I implement this metod in the FindMin class, like

do I get a correct answer, but the given code in the book does not have this method implemented. Beats me for now.

I also don't know why the book declares to throw these exceptions, since the code compiles without that. But I must obviously rehearse my knowledge about Exceptions, since I would expect the compiler to throw an error if Exceptions are declared that are never thrown... but that'll be tomorrow.
3 days ago