Al Davis

Greenhorn
+ Follow
since Aug 08, 2018
Cows and Likes
Cows
Total received
1
In last 30 days
0
Total given
0
Likes
Total received
1
Received in last 30 days
0
Total given
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Al Davis

Thanks.  My brain saw a closing bracket after List<TableColumn which actually isn't there.  Bad brain!!
1 year ago
I'm trying for the first time to contribute to an open source program but I'm having a lot of difficulty understanding the code.  Specifically (for now), the  ?>>  characters are completely new and confusing to me.  Here's the code:



The method is a small part of a large class used to build columns containing bibliographic data.
1 year ago
SUCCESS!!

I've finally gotten everything to do what it needs to do.

My original posting related only to part 6 of an 8 part problem (which I haven't finished yet), but part 6 is definitely the "core" of the exercise.  I've posted all the code for the class below - there are some methods in it which we haven't really discussed here which were required by earlier parts of the exercise.  I don't think my code is beautiful and almost certainly it is not elegant, but it does demonstrate that a bull in a china shop might eventually achieve a good end.  Based on the messages I've gotten from NetBeans, there seem to be plenty of opportunities to use functional expressions to save some typing, but I've made the decision not to use those until I have a more solid grounding on the "old style" code.  Lord knows I need it.

The discussions on this thread have been fascinating, enlightening and helpful.  Thanks to everyone.  I can't say I used anyone's code entirely, but I'm pretty sure I've stolen a piece here or there from almost everyone who posted.

If there's interest, I could write the MOOC organizers and ask for permission to upload the entire exercise.  They're revamping the course anyway over the next few months, so they might not mind.  Piet Souris, in particular, mentioned that he considered this an excellent exercise.  I know it sure as hell taught me a lot, but I'm old enough that my ego can handle the beating.  And it took a real beating...  Anyway, here's the code.  There are a lot of stupid/trivial comments, but I found it necessary to use them to keep my mind from exploding.  I've also included (again - sorry for the repetition) the instructions provided by the devious bastards that designed the course.  The instructions are after the code.



Instructions:
Create the class FilmComparator in the package reference.comparator. The class FilmComparator has to implement the interface Comparator<Film>, and it has to have the constructor public FilmComparator(Map<Film, List<Rating>> ratings). The class FilmComparator will be used later on to sort films according to their ratings.

The class FilmComparator has to allow for film sorting according to the average of the rating values they have received. The films with the greatest average should be placed first, and the ones with the smallest average should be the last.

Thanks again everyone.  I'm gonna have a glass of bourbon and go to bed.  More code in the morning!
1 year ago
Maybe I need to clarify things a little bit.

In the 1st paragraph of my original post I have included, in quotation marks, the complete set of instructions I have for this section of the problem.

The Rating Enum class instructions (from an earlier section of the problem) are :
"Create the enumerated type Rating in reference.domain. The enum class Rating has a public method public int getValue(), which returns the value of the rating. The value names and their grades have to be the following:"

Rating Value
BAD -5
MEDIOCRE -3
NOT_WATCHED 0
NEUTRAL 1
FINE 3
GOOD 5

I posted a minimal amount of code because I'm so unsure of what to do.  Other than the package, class name and interface, and the class constructor - all described in the instructions in the first paragraph - none of the other code I included is required by the course.  But the class declaration and the constructor are required, and are copied verbatim from the instructions.  I inferred that the constructor intends the use of a HashMap because of the form in which the constructor parameter (ratings) printed using the code:
System.out.println("ratings is " + ratings);
Also, the course has not covered any Map data structures other than Map and HashMap (although they have on a couple of occasions pushed us out into the java API to find info for other stuff).

So anyway - Piet is correct that there is no requirement to "sort a HashMap".  That's my phrasing, but more specifically perhaps I should have said "return a list of the Film names used as Map Keys based on the film Ratings found in the Map Value ArrayList."  The final objective is simply to return a list of the movies in the order of highest average rating to lowest average rating.  The example output provided is :
The films before sorting: [Gone with the Wind, The Bridges of Madison County, Eraserhead]
The films after sorting: [The Bridges of Madison County, Gone with the Wind, Eraserhead]
so it appears the course expects the films to be returned as some sort of list.

I apologize if I have caused any confusion.

On a (perhaps) related topic, can anyone recommend a good source of information for the use of the Comparator interface?  Much of my problem arises because the Oracle information (Java Tutorials and Java Language Specification) is obtuse, the API gives no examples for its declaration and use and info on the internet is conflicting and confusing.  At least some sites conflate Comparator and Comparable.  FWIW I have a college textbook I use as a reference, and their info on Comparator sucks, too.

Again, I thank you all for your help and apologize if I've caused any confusion.
1 year ago
Henry - a hashMap is what is supplied in the exercise, e.g. {Gone with the Wind=[FINE], Eraserhead=[BAD], The Bridges of Madison County=[GOOD, BAD]}.

Carey and Stephan - converting to a key/value entry set is the approach I've been playing with; it's what the majority of web sites that address sorting HashMaps recommend.  Once that's done, though, I'm still forced to deal with obtaining the individual ArrayList entries for the values so that I can get their averages - this is where I'm breaking down.  How can I get the values, obtain their averages and sort them, while still maintaining the key /value linkage between the film name and its ratings?  As I mentioned above, I don't think hard-coding new hashMaps is a good approach.  Ultimately, I have to return only a list of film names sorted by the order of the average ratings, but not the ratings themselves nor the averages.

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.

1 year ago
Campbell - thanks for the rapid response.  I know that HashMaps can't be sorted using the Collections.sort methods, but the problem specified the map structure I've posted ( FilmComparator(Map<Film, List<Rating>> ratings) ) and I'm stuck with it.  I simply don't have a handle on the correct approach to dealing with the ArrayList of values.  Those values are ultimately the sort criteria for the problem, but I don't know the best way to pull out the values and sort them while still maintaining the HashMap's Key/Value pairings.  I can use any intermediate structures needed, but eventually I have to return the names of Films sorted by their average ratings.
1 year ago
Apologies for the double post - the first one didn't seem to go up initially.
1 year ago
I'm working on an exercise in the Univ of Helsinki's Java MOOC course and have a problem.  Here are the instructions for part 6 of the problem:
"Create the class FilmComparator in the package reference.comparator. The class FilmComparator has to implement the interface Comparator<Film>, and it has to have the constructor public FilmComparator(Map<Film, List<Rating>> ratings). The class FilmComparator will be used later on to sort films according to their ratings.
The class FilmComparator has to allow for film sorting according to the average of the rating values they have received. The films with the greatest average should be placed first, and the ones with the smallest average should be the last."

The film ratings (words - "GOOD, BAD, etc, of type Rating) are entries in an enumerator class and are associated with numeric values in the class constructor:


The film rating HashMap for the exercise prints like so:
{Gone with the Wind=[FINE], Eraserhead=[BAD], The Bridges of Madison County=[GOOD, BAD]}

So the problem solution appears to be:
1. get the ratings arrayList for each film.
2. call the enum list to get numeric values for each rating in the ArrayList, and compute the average of each ArrayList.
3. use a comparator to sort the arrayList ratings from highest average to lowest average, ensuring that the entire HashMap for each film (i.e. Map<Film, List(<Rating>) is included in the sorting


Here is the code so far:


...and now I'm lost.  I can't directly compare the HashMaps, so I have tried to obtain the ArrayList averages as the first step - but they get "unlinked" from the HashMap and I'm not sure how to address that.  I don't want to "hard code" new HashMaps for o1 and o2, i.e. create a new HashMap named o1Avg (or whatever) with HashMap<Map<Film>, double> o1Avg = new HashMap(); but I think I need some form of new HashMap in the form of <Film, double> (with double being the average value of the film's ratings) in order to use the comparator.  The internet has a million examples of sorting HashMaps by values using comparator, but the fact that the value is a list is really screwing up my day(s).  Thanks in advance for help/suggestions.
1 year ago
Well that was embarrassingly easy.  Thanks.
1 year ago
My first was a Commodore VIC-20 - 16K of Ram and a 6502(?) processor.  After I spent a couple of weeks writing my first program I got all bad-ass and bought a cassette drive for it.  But I learned BASIC and a little assembly language.
1 year ago
I need to create a HashMap which contains movie titles as the keys, and ratings for those movies, in the form of an ArrayList() of ratings for that movie, as the values.  Movies can have multiple ratings from multiple critics (one rating per movie per critic).  At this point I'm only trying to assign ratings to movies; assigning movies/ratings to critics will come later.

When I create the HashMap with the first key/value set, I am unable to figure out how to create the ArrayList for a second movie without altering the first movie's ArrayList.  Everything I've tried either destroys the first movie's ArrayList or alters it.  I was under the impression that once the ArrayList was added to the HashMap it would be "immune" from alteration unless the HashMap key was used to access the ArrayList, but that seems not to be the case.  It seems I need to create a new ArrayList for each HashMap, but, so far, every time I've tried it alters/destroys the existing ArrayList.  I can't figure out how to create a new ArrayList (with a new variable name) which can be repeated "infinitely" as new movies are rated.  Here's one of the many non-working attempts I have made to do this; I've included it to give an idea of what I'm trying to achieve.  

1 year ago
Obviously if the class needs the variable it should (must) get it.  My question is asking whether there is a "best" way to go about putting it there, assuming that there are multiple ways to do so..
1 year ago
Many multiclass programs make use of the same variables in multiple classes; e.g. length and height in various geometric structures.  In general, there are 3 ways to make "copies" of the variables - pass them as parameters, obtain them via getX() methods, and (re)declare them within each class.  I can think of examples where one way has a clear advantage, and examples where I see no clear advantage.  Is there a widely accepted rule of thumb about this?

I know subclasses can be a good option in some situations, but that's not what I'm considering at the moment.

I'm most interested in the efficiency of the various methods (the substance, if you will) rather than the style, but style does matter when it comes to code readability and/or maintenance.  Any advice?
1 year ago
Paul and Knute,

Excellent responses.  Thank you.  Interfaces are finally starting to make at least a little sense to me; the trick now is to see if I can work through a design and use them properly.  Thank you very much.

Al
1 year ago