• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Descending order and associating Array Elements in sort method to another's elements

 
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear Programmers,

From the program I wrote, class RaterTwo (as written below), I obtained the following console output from the input I put to its GUI:-

C:\JPF Bk2>javac RaterTwo.java

C:\JPF Bk2>java RaterTwo
Phillipa Carruthers came 2 gets 3points
Phillipa Carruthers came 4 gets 1points
Norbert Postlethwaite came 5 gets 0points
Norbert Postlethwaite came 1gets 5points
Zacharias Smythe came 3 gets 2points
Zacharias Smythe came 2 gets 3points
4
5
5

Overall points are:-14.0
Phillipa Carruthers's TotalPoints are:- 4.0
Norbert Postlethwaite's TotalPoints are:- 5.0
Zacharias Smythe's TotalPoints are:- 5.0
Phillipa Carruthershas a point percentage of 28.57142857142857
Norbert Postlethwaitehas a point percentage of 35.714285714285715
Zacharias Smythehas a point percentage of 35.714285714285715
Runner's percentages in ascending order are:
28.57142857142857
35.714285714285715
35.714285714285715

C:\JPF Bk2>

However I did want to elaborate on the code to improve the output.
What I wanted to do was firstly to also output in descending order, so's Zacharias' percentage would come at the top, and Phillipa's at the bottom .So output to the console woud be in ascending and descending order.
Another somewhat harder objective was to be able to get the numbers as output by the last lines of code:-

java.util.Arrays.sort(Percentage);

System.out.println("Runner's percentages in ascending order are: ");

for (i = 0;i<Runners;i++)
{
System.out.println(Percentage[i]);
}

to output their respective Names, so that instead of it reading : -

28.57142857142857
35.714285714285715
35.714285714285715

output would read, say:-

Phillipa Carruthers has a point percentage of 28.57142857142857
Norbert Postlethwaite has a point percentage of 35.714285714285715
Zacharias Smythe has a point percentage of 35.714285714285715

Of course that was the output obtained previously by the code:-

for (i = 0; i< Runners;i++)
{
System.out.println(RunnersNames[i] +"'s TotalPoints are:- " + TotalPoints[i]);
Percentage[i] = (TotalPoints[i]/Overallpoints)*100;
}

but the fact that that output came out in ascending order was only mere coincidence.

What I wanted is for the code to sort the Percentage array to return the elements in ascending /descending order, with the
names thus associated with those scores or values output to their respective values, thus sorted.

If I wrote for instance :-


java.util.Arrays.sort(Percentage);

System.out.println("Runner's percentages in ascending order are: ");

for (i = 0;i<Runners;i++)
{
System.out.println(RunnersNames[i] + " got a percentage of " + Percentage[i]);
}

I would end up with the unsorted order of input for the Runners[names] array with the sorted order for the input of Percentage[i] array.
In other words, how does one sort an array whose elements are associated with another array of elements, so that associated array will output in the same sequence as the sorted array.
import javax.swing.*;
Anyway, I hope you can give me some notion how to proceed with this.
This is the program RaterTwo, as referred to:-






Thanks in advance for your help.

Yours

Simon.


[RP] Removed the first duplicate part of the post, and removed the smileys from the code.

[ August 04, 2008: Message edited by: Rob Prime ]
[edit]Add some new lines to reduce horizontal scrolling. CR[/edit]
[ August 12, 2008: Message edited by: Campbell Ritchie ]
 
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Take a look at
Also instead of having multiple arrays I would suggest you wrap the values inside an object and maintain the object array.
That way you can tweak your custom Comparator to sort not only in ascending/descending order but also to sort on different criteria (name, points etc)
[ August 04, 2008: Message edited by: Maneesh Godbole ]
 
Sheriff
Posts: 22815
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is not really Swing related, so I'm moving this to the beginner's forum.
 
Simon Evans
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear Maneesh Godbole, (and all other programmers out there)

Thank you for your recommendation that I use an Arraysort and a comparator.

I have looked at some of the Tutorials on the net under these titles.

I saw one by Baldwin Tutorials, called Comparator05, which deals with filling an array and then sorting it, then sorting it with a Comparator.

There is a problem with this, which I can see recurring with other such Tutorials, which is that the content of the Array that is sorted is of the same data type, ie: String objects.

In the Array I want to sort both doubles and String objects, so that whatever order a descending or ascending sort gives to percentages thus calculated, the Names allocation to those percentages will be preserved.

F'rinstance if a program returns, say: Ceri = 5, Andy = 3, Bob = 2, and upon running an ascending sort we get 2,3,5, and upon putting a Names[i] into the return, we then get (but would not want):- Ceri = 2, Andy = 3, Bob = 5, we'd want to reprogram code to return the order with the names allocated to the sorted values, ie get: Bob = 2, Andy = 3, Ceri = 5.

However using a comparator will only take in one data type - or rather object, being a String, but as hopefully above example illustrates - we need it to cope with two data types - a string for the name and an integer (actually a double for my program).

I seem to remember coming across this problem in the distant past, and the solution involved converting the char within an element to an int - but as the program I am trying to adopt, doesn't deal with either of these data types, I don't think it can apply.

Would you, or anyone out there know of a means of achieving not only sorted, but allocated output to an array? If the comparator can handle this, are there any tutorials out there that illustrate this?

Anyhow, thanks for reading,
I hope you can help.

Yours
Simon.
 
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If the two pieces of data belong together, then I would put them both as properties of an Object and make the Object comparable. For example, if you have a Person class, where the Person has a String name and a double score, you could make an array of People, then compare them:


You could take the comparable code out of the Person object and move it to a Comparator to get equivalent code that could more easily change how a collection of People would be ordered (for example, order by name, by reverse score, etc...). Here is an example of how the Comparator might look:


[ August 07, 2008: Message edited by: Steve Luke ]
-- Edited On 8/11/2008 --
Reformatted to try and make it appear better on screen
[ August 11, 2008: Message edited by: Steve Luke ]
 
Simon Evans
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Compiler returns on programs Person and ComparePerson

Dear Steve Luke

I downloaded and installed (successfully - I think as the javac file was in the bin directory)- the Java 6.0 earlier to my computer.
Anyway, I thought this would fix any error returns on programs Person and PersonComparer.
Could you give me some advice as to how to get these programs running?
I enclose the code and the console output in response (though I suppose you still have the code for these programs).
I have also experienced difficulty regarding compiler returns with programs copied direct from the SunJava site, which I am asking for as a general request,

Thank you for your help.



Microsoft Windows XP [Version 5.1.2600]
(C) Copyright 1985-2001 Microsoft Corp.

C:\Documents and Settings\Administrator>cd C:\Java\Java Six

C:\Java\Java Six>set PATH=C:\Program Files\Java\jdk1.6.0_05\bin;%PATH%

C:\Java\Java Six>javac Person.java
Person.java:3: luke.steven.compare.Person is not abstract and does not override
abstract method compareTo(luke.steven.compare.Person) in java.lang.Comparable
public class Person implements Comparable<Person>
^
Person.java:25: cannot find symbol
symbol : variable p
location: class luke.steven.compare.Person
if (p == null) throw new IllegalArgumentException("Person being compared can't b
e null"); return Double.valueOf(this.getScore()).compareTo(p.getScore());

^
Person.java:25: return outside method
if (p == null) throw new IllegalArgumentException("Person being compared can't b
e null"); return Double.valueOf(this.getScore()).compareTo(p.getScore());


^
3 errors

C:\Java\Java Six>javac PersonComparer.java
PersonComparer.java:13: <identifier> expected
Override public int compare(Person first, Person second)
^
1 error


C:\Java\Java Six>

[edit]Add newlines and remove whitespace to reduce horizontal scrolling. CR[/edit]
[ August 12, 2008: Message edited by: Campbell Ritchie ]
 
Steve Luke
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It looks like the code got mangled between the web page and your editor. Things like braces and hole words were lost. I re-formatted the code so that it will hopefully be easier to read (less scrolling). Try copy and pasting the code again. If you still get those errors, then look through your code and the source on this page and fix any mangling that occurred.

The code is just example code anyway. You should be able to look at it, get an idea of what it means, then implement your own version in your code (feel free to copy it, but you will learn more by re-implementing it).

Also note that my classes are in packages. If you haven't worked with packages before See the Java Tutorial on the subject then begin to use them all the time :-)
 
Simon Evans
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear Steve Luke,
Thank you for your advice.
I have recopied the text of your code for Person and PersonComparator, so they replicate what you sent. All the same I get error returns, so can't progress with this code. Just taking out what the compiler rejects, or putting in what it requests is no way, as you probably know, to get a program to work. I haven't any familiarity with Comparators, so wouldn't know how to recode it to work. I hope you can give me some suggestions.
I enclose my copy of your code, plus the console output (using jdk 1.6):-



Compiler gives:-

Microsoft Windows XP [Version 5.1.2600]
(C) Copyright 1985-2001 Microsoft Corp.

C:\Documents and Settings\Administrator>cd c:\Java\Java Six

C:\Java\Java Six>set PATH=C:\Sun\AppServer\jdk1.6.0_05\bin;%PATH%


C:\Java\Java Six>javac Person.java
Person.java:7: '{' expected
public class Person implements Comparable<Person> {
^
Person.java:28: illegal character: \64
@Override
^
Person.java:51: illegal character: \64
@Override
^
Person.java:59: illegal character: \64
@Override
^
Person.java:64: illegal character: \64
@Override
^
Person.java:82: '}' expected
}
^
6 errors




C:\Java\Java Six>javac PersonComparer.java
PersonComparer.java:6: '{' expected
public class PersonComparer implements Comparator<Person> {
^
PersonComparer.java:18: illegal character: \64
@Override
^
PersonComparer.java:102: '}' expected
^
3 errors
***************************************************************************

Anyhow I suppose I might take out the @Overide line, which the compiler seems to make an issue of, but tampering with the code even further only seems to push things further askew.
Could these programs be rewritten to run, is there any other code out there which would exemplify array element sorting that can preserve elements initial sequence ? If you know of such code's location, ie a tutorial, or example, on the net, would you be able to direct me to it?
Thanks for reading.
Yours, hoping to hear,
Simon.

[edit]Delete extraneous whitespace to improve formatting, and disable smilies. CR[/edit]
[ August 14, 2008: Message edited by: Campbell Ritchie ]
 
Marshal
Posts: 79962
396
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Have't got the time to look at the whole of it, but there are two likely reasons why @Override causes problems
  • You are using Java1.4.2 or older; the annotations were only added in Java5.
  • If you are using Java5 or Java6, you have made a mistake in overriding.
  • The error message you have suggests you are using an older version of Java; you might do well to download a more recent version.
     
    Campbell Ritchie
    Marshal
    Posts: 79962
    396
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    If you are using Java6, then I was mistaken about the error to do with @Override. Sorry. It is very peculiar that it is complaining about \64; if you look up 64 in ASCII it is @ which is what it supposed to be.

    What are you using as a text editor? The odd formatting is not what one expects from a text editor; I have been through your posts on this thread with the delete key at least twice.

    You're not using a word processor, are you? They put all sorts of control characters in which the compiler can't cope with.

    And I am very surprised you haven't had errors about the Double when you are trying to return an int in the compareTo methods.
     
    Campbell Ritchie
    Marshal
    Posts: 79962
    396
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    I now see what the Double cast is doing in the compareTo methods; now I have read it properly I can see it won't return a Double.

    Actually I copied and pasted the text you quoted; once the strange control characters had vanished, and a few extraneous ;s caused by the smilies in the website were deleted, the whole thing worked and gave this output:

    Person: Sam (1.40)
    Person: Mary (3.29)
    Person: Harry (3.30)
    Person: Beth (16.40)

    You seem to have it working.
     
    Simon Evans
    Ranch Hand
    Posts: 93
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Dear Campbell Ritchie,
    Thank you for your diagnosis.
    I did copy the code above I believe you were referring to, above, and tried running class Person through my compiler again (I use Word pad, and am running Java 1.6 (see the compiler output I posted for Steve Luke, or output below)). All the same I got the following:-


    Mind, its not the end of the world, as I found some similar code on the web, which compiles and runs okay re:-




    Giving the output : -


    I not too sure about the "int emp1Age = ( (Employee) emp1).getAge();" bit, but hey - it works.
    Again, when I've finished coding this, I will post it on this thread, and I wager that you (& everyone else) will be amazed by it.

    Until then,
    Yours
    Simon.
     
    Simon Evans
    Ranch Hand
    Posts: 93
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Addendum

    Sorry, I meant to say that I use Notepad - for writing and copying code, ( - not Wordpad)

    Yours
    Simon.
     
    Campbell Ritchie
    Marshal
    Posts: 79962
    396
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    I don't think Notepad is any good for programming; try Notepad++ or Notepad2 which support syntax highlighting, bracket matching and (I think) automatic indentation, which makes finding errors in the code much easier. Both those editors are available free of charge.

    There is something very peculiar going on; as I said yesterday your compiler is complaining about a \64 character but as far as I can tell (assuming the 64 is decimal) \64 is @ which is what it is supposed to be. Go to the Unicode website and look for "basic Latin" and open the file which is called U0000.pdf or similar. "Basic Latin" in Unicode is identical to extended ASCII as far as I know.
    Notepad is very fussy about line ends; most applications aren't that fussy. DOS/Windows uses \r\n as line end, *nix uses \n and older Mac versions used \r. It is possible that you have code which has been transferred from one OS to another hence the line ends not working properly. You do sometimes find Notepad puts those rectangles in the code when it encounters an unfamiliar line end.

    Go back to your posting of yesterday afternoon, the last one before my posting saying "Haven't got the time . . ." Now I have deleted the extraneous ;s put in by the automatic smilies it should work. What I did yesterday was to highlight copy and paste the text from this thread, and run it. I actually used Eclipse, but it will probably work if you put it into a text editor and use "save as." As I said, it ran happily and I showed you the output. Good luck with your application.

    Please go into "general computing" and start a thread about whether anybody has ever seen Notepad insert strange characters into their code before. I have no idea why that is happening.
     
    Steve Luke
    Bartender
    Posts: 4179
    22
    IntelliJ IDE Python Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    I just went back and tried to copy then compile the code (using Notepad as the editor on a Windows machine).

    In Java 1.5 and 6 the copied code works fine.

    In Java 1.4 I get the same exact errors you get because the generics and annotations weren't introduced yet.

    You said you downloaded Java 6, if you did, then you did not set your path properly. Change your system's CLASSPATH and PATH folders to point to the newly downloaded Java 6 JDK you downloaded and that should fix your problems.
    [ August 15, 2008: Message edited by: Steve Luke ]
     
    Steve Luke
    Bartender
    Posts: 4179
    22
    IntelliJ IDE Python Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Originally posted by Simon Evans:
    (I ... am running Java 1.6 (see the compiler output I posted for Steve Luke, or output below)).
    ...
    C:\Java\Java Six>set PATH=C:\Sun\AppServer\jdk1.6.0_05\bin;%PATH%



    Do a javac -version as a sanity check. My guess - your %PATH% variable has the jdk 1.4 path in it, and because it comes after the 6 JDK you add to the path it gets used instead.




    I not too sure about the "int emp1Age = ( (Employee) emp1).getAge();" bit, but hey - it works.



    Life before Generics, how did we cope :-)
     
    Campbell Ritchie
    Marshal
    Posts: 79962
    396
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Originally posted by Steve Luke:
    Do a javac -version as a sanity check. My guess - your %PATH% variable has the jdk 1.4 path in it, and because it comes after the 6 JDK you add to the path it gets used instead.



    Are you sure you mean 1.4 after the JDK 6? Surely it would be the other way round?
     
    Simon Evans
    Ranch Hand
    Posts: 93
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Dear Ritchie Campbell and Steve Luke,

    Could you tell me where I can get Notepad +, please?
    I adjusted the Comparator program I found on the net: 'JavaComparator' and 'Employee' to fit in with my 'HorseTraderOrder' program.
    There seems to be one hitch, which doesn't occur in the JavaComparatorExample program.
    I don't know why the compiler will accept that and not accept my own 'HorseTraderOrder' program.
    I have also written 'Horse' program, which creates the array objects and its variable accessor and mutator methods, which parallels the 'Employer'
    program, and it compiles ok.
    I had thought the hitch was due to the jdk, but ran it on the jdk 1.6, but still it returns the following:-

    Microsoft Windows XP [Version 5.1.2600]
    (C) Copyright 1985-2001 Microsoft Corp.

    C:\Documents and Settings\Administrator>cd c:\Java\Java Six

    C:\Java\Java Six>set PATH=C:\Sun\AppServer\jdk1.6.0_05\bin;%PATH%

    C:\Java\Java Six>javac HorseTraderOrder.java
    HorseTraderOrder.java:229: cannot resolve symbol
    symbol : class PercentageComparator
    location: class HorseTraderOrder
    Arrays.sort(horse,new PercentageComparator());
    ^
    1 error

    C:\Java\Java Six>javac Horse.java

    C:\Java\Java Six>

    I have looked at your replies since starting this letter, and shall try what you suggest, as the above compiler output may be due
    to what you diagnose - about the obselete path. Though I know the javac file is in the 'bin' cause I checked it was there in 'Explorer' also I was
    told if it wasn't then any javac command wouldn't work, as ie when the 'set PATH etc' is omitted.

    Though it now occurs to me, that if 'official' program : 'JavaComparator' and 'Employee' run ok on my compiler - then why not my
    'HorseTraderOrder' and 'Horse' ? - not fair.

    If you lift the last two loops from 'HorseTraderOrder' it'll run okay on its own (without 'Horse'), but as it stands - it seems a bit shoddy.
    It is just a fun application to compare with any given exchange's results cards - so don't go losing any hard-earned on it.

    Hoping to hear, and thank you for your assistance.

    Yours

    SImon

    Code to Javac Comparator and Employee, and 'HorseTraderOrder' and 'Horse' :-




    [edit]Add line breaks and whitespace to improve formatting. Also disable smilies. CR[/edit]
    [ August 17, 2008: Message edited by: Campbell Ritchie ]
     
    Campbell Ritchie
    Marshal
    Posts: 79962
    396
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Try here. Or here. One of those sites may be specific to Britain, however.

    I am not convinced you have a PATH problem, but as somebody suggested earlier, invoking

    java -version

    and

    javac -version

    will sort that out. Make sure to do both, and check you get the same version (or nearly the same) on both; it should say something beginning with 1.6 and one of them usually says something about "hotspot" too. This is what happened when I tried it

    campbell@linux-pgix:~> java -version
    java version "1.6.0_10-beta"
    Java(TM) SE Runtime Environment (build 1.6.0_10-beta-b25)
    Java HotSpot(TM) Client VM (build 11.0-b12, mixed mode, sharing)
    campbell@linux-pgix:~> javac -version
    javac 1.6.0_10-beta
    campbell@linux-pgix:~>

    I have a suspicion you can get that sort of compiler error from a tiny spelling mistake; I shall look at your code soon and see what I can make of it.
     
    Campbell Ritchie
    Marshal
    Posts: 79962
    396
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    You know there is a way you can show yes/no buttons in JOptionPane? I can't remember what it is just at present, but you pass something like YES_NO_OPTION as one of the parameters. You can use YES_OPTION and NO_OPTION as results from that Dialog.

    The whole thing looks complicated; you have lots and lots of fields.

    Where is the PercentageComparator class? The compiler error means it can't find a class file by that name. Since you haven't imported it, you would have to create a PercentageComparator class in the same package/folder as the HorseTraderOrder class. Please check the spelling very carefully; if you have called it percentageComparator the compiler will miss the capital P at the beginning and throw an error.
     
    Simon Evans
    Ranch Hand
    Posts: 93
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Dear Ritchie Campbell
    The PercentageComaparator class is in the 'Horse' class, in the same way as the AgeComparator class is within the 'Employee' class, in the other set of programs ('Employee' and 'JavaComparatorExample').

    Those 'Smilies' seem to get everywhere - I for sure didn't put them there.

    That is a pertinent issue you raise, about how there should be a yes/no input to the JOption pane - as I realise 'HorseTraderOrder' would be more user friendly using a yes/no input - than numerical input - but couldn't get it to run somehow, and to have it simplified to just button pressing would minimise user input blunders (as it is, it crashes given the slightest provocation)Perhaps It looks complicated - but its practially all procedural type stuff, I agree it could do with trimming, but thats the way it grew somehow. As I suggested,if you take out the last two 'for -do' loops - and recompile, it'll run okay - but as said, the final output won't be in any order - ascending or descending - although that is what I wanted by using the Comparator.
    Thank you for your help.
    Hoping to hear,
    Yours
    Simon.
     
    Campbell Ritchie
    Marshal
    Posts: 79962
    396
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Not Ritchie Campbell, but the other way round

    Have you made the Comparators inner classes? Why? Without some sort of additional naming, they are only accessible inside their outer classes.

    It would have been easier to use nested classes (labelled static) than inner; then you could call "new Horse.PercentageComparator()". For an inner class you have to say something like

    myHoss.new PercentageComparator()

    . . . and you have to start from an instance of the Horse class. Please find a book (there is a copy of Thinking in Java by Bruce Eckel, 3rd edition, available free on the Internet, and look for the chapter about inner classes in that), and check the syntax for instantiating an inner class; I might have got it wrong.

    I can see the logic behind having the Comparators inside the classes they compare, but I think you would find it much easier to use nested (static) classes [or even a top-level class instead].
     
    Simon Evans
    Ranch Hand
    Posts: 93
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Dear Campbell Ritchie,

    I have a copy of Eckel's 'Thinking in Jave' book.
    Theres some code on page 377 - 378 ('The Link to the Outer Class') that I'll try exploring.

    All the same, as 'JavaComparatorExample' can access Employee's inner class with the line:-

    Arrays.sort(employee, new AgeComparator());,

    I can't see why 'HorseTraderOrder' can't access Horse's inner class with the line:-

    Arrays.sort(horse,new PercentageComparator());

    'JavaComparatorExample' runs okay - so how come 'HorseTraderOrder' doesn't?

    Thank you for your assistance.

    Yours

    Simon.
     
    Campbell Ritchie
    Marshal
    Posts: 79962
    396
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    AgeComparator and NameComparator are not inner classes; they are ordinary non-public classes which happen to be in the same file.
     
    Simon Evans
    Ranch Hand
    Posts: 93
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Dear Campbell Ritchie and Steve Luke,
    The program 'HorseTraderOrder' runs like the 'JavaComparatorExample' program did - it was a misplacement of the curly brackets (flukes (?)) in my 'Horse' program that rendered PercentageComparator inaccessible.
    I got it to compile and run - and output in order as below :-



    Giving the following output (to input to gui):-

    Microsoft Windows XP [Version 5.1.2600]
    (C) Copyright 1985-2001 Microsoft Corp.

    C:\Documents and Settings\Administrator>cd c:\Java\Java Six

    C:\Java\Java Six>set PATH=C:\Sun\AppServer\jdk1.6.0_05\bin;%PATH%

    C:\Java\Java Six>javac Horse.java

    C:\Java\Java Six>javac HorseTraderOrder.java

    C:\Java\Java Six>java HorseTraderOrder
    Spring Dream has: 3.0
    Rose Row has: 3.0
    Bassinet has: 3.0
    Adage has: 3.0
    Gamesters Lady has: 1.0
    Never Pink has: 1.0
    Queen Excalibur has: 1.0

    Total points = 15.0

    Spring Dream has: 20.0 % of points
    Rose Row has: 20.0 % of points
    Bassinet has: 20.0 % of points
    Adage has: 20.0 % of points
    Gamesters Lady has: 6.666666666666667 % of points
    Never Pink has: 6.666666666666667 % of points
    Queen Excalibur has: 6.666666666666667 % of points

    Spring Dream came 2 gets: 3 points
    Spring Dream came 0 gets: 0 points
    Rose Row came 1 gets: 5 points
    Rose Row came 5 gets: 0 points
    Bassinet came 3 gets: 2 points
    Bassinet came 3 gets: 2 points
    Adage came 4 gets: 1 points
    Adage came 6 gets: 0 points
    Gamesters Lady came 4 gets: 1 points
    Gamesters Lady came 0 gets: 0 points
    Never Pink came 6 gets: 0 points
    Never Pink came 0 gets: 0 points
    Queen Excalibur came 0 gets: 0 points
    Queen Excalibur came 9 gets: 0 points

    Spring Dream overall got place points of 3
    Rose Row overall got place points of 5
    Bassinet overall got place points of 4
    Adage overall got place points of 1
    Gamesters Lady overall got place points of 1
    Never Pink overall got place points of 0
    Queen Excalibur overall got place points of 0

    Overall points are:- 14.0

    Spring Dream's TotalPoints are: - 3.0
    Rose Row's TotalPoints are: - 5.0
    Bassinet's TotalPoints are: - 4.0
    Adage's TotalPoints are: - 1.0
    Gamesters Lady's TotalPoints are: - 1.0
    Never Pink's TotalPoints are: - 0.0
    Queen Excalibur's TotalPoints are: - 0.0

    Spring Dream has point percentage of 21.428571428571427
    Rose Row has point percentage of 35.714285714285715
    Bassinet has point percentage of 28.57142857142857
    Adage has point percentage of 7.142857142857142
    Gamesters Lady has point percentage of 7.142857142857142
    Never Pink has point percentage of 0.0
    Queen Excalibur has point percentage of 0.0
    Spring Dream's Course and Distance total is: 1.0
    Rose Row's Course and Distance total is: 3.0
    Bassinet's Course and Distance total is: 1.0
    Adage's Course and Distance total is: 1.0
    Gamesters Lady's Course and Distance total is: 1.0
    Never Pink's Course and Distance total is: 1.0
    Queen Excalibur's Course and Distance total is: 0.0
    Total for course and distance 8.0
    Course and Distance percentage for Spring Dream is 12.5
    Course and Distance percentage for Rose Row is 37.5
    Course and Distance percentage for Bassinet is 12.5
    Course and Distance percentage for Adage is 12.5
    Course and Distance percentage for Gamesters Lady is 12.5
    Course and Distance percentage for Never Pink is 12.5
    Course and Distance percentage for Queen Excalibur is 0.0

    Spring Dream gets 61.53846153846154 tips percentage
    Rose Row gets 15.384615384615385 tips percentage
    Bassinet gets 7.6923076923076925 tips percentage
    Adage gets 7.6923076923076925 tips percentage
    Gamesters Lady gets 0.0 tips percentage
    Never Pink gets 7.6923076923076925 tips percentage
    Queen Excalibur gets 0.0 tips percentage

    Spring Dream gets Overall percentage of 115.46703296703296
    Rose Row gets Overall percentage of 108.5989010989011
    Bassinet gets Overall percentage of 68.76373626373626
    Adage gets Overall percentage of 47.33516483516483
    Gamesters Lady gets Overall percentage of 26.30952380952381
    Never Pink gets Overall percentage of 26.858974358974358
    Queen Excalibur gets Overall percentage of 6.666666666666667


    Horses in ascending/descending order of percentages given
    HorseQueen Excaliburhas6.666666666666667 points
    HorseGamesters Ladyhas26.30952380952381 points
    HorseNever Pinkhas26.858974358974358 points
    HorseAdagehas47.33516483516483 points
    HorseBassinethas68.76373626373626 points
    HorseRose Rowhas108.5989010989011 points
    HorseSpring Dreamhas115.46703296703296 points

    C:\Java\Java Six>

    Not bad huh?
    Anyhow don't go losing your shirts on it.
    'Bye for now.
    Yours
    Simon.

    [edit]New lines to improve formattinv. CR[/edit]
    [ August 17, 2008: Message edited by: Campbell Ritchie ]
     
    Campbell Ritchie
    Marshal
    Posts: 79962
    396
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Well done

    And I think Notepad++ and Notepad2 will help by allowing brace matching; if you hover your mouse pointer on a { the corresponding } changes colour.
     
    Simon Evans
    Ranch Hand
    Posts: 93
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Dear Campbell Ritchie, & Steve Luke,
    It occurred to me, the output to the program HorseTraderOrder needed clearing up, the revised version is below.
    The criteria aren't mine - they belong to someone else who reckons they're 'in the know', there's so much of a chaos factor in racing - if you applied this program to any day's racing results you'd see what I mean.
    Thank you for your assistance, all the same.
    I was told by a company that it would be an idea to write a few programs of my own, as a sort of hobby, that they could look at, sort of a portfolio as a means to securing work, if you don't have industrial experience, hence this program and a few more, hopefully.
    Speak to you soon.
    Yours Simon.




    Output:-

    Microsoft Windows XP [Version 5.1.2600]
    (C) Copyright 1985-2001 Microsoft Corp.

    C:\WINDOWS\system32\spm>cd c:\Java \Java Six

    C:\Java\Java Six>javac HorseTraderOrder.java

    C:\Java\Java Six>javac Horse.java

    C:\Java\Java Six>java HorseTraderOrder
    Spring Dream has: 3.0
    Rose Row has: 3.0
    Bassinet has: 3.0
    Adage has: 3.0
    Gamesters Lady has: 1.0
    Never Pink has: 1.0
    Queen Excalibur has: 1.0

    Total points = 15.0

    Spring Dream has: 20.0 % of points
    Rose Row has: 20.0 % of points
    Bassinet has: 20.0 % of points
    Adage has: 20.0 % of points
    Gamesters Lady has: 6.666666666666667 % of points
    Never Pink has: 6.666666666666667 % of points
    Queen Excalibur has: 6.666666666666667 % of points

    Spring Dream came 2 gets: 3 points
    Spring Dream came 0 gets: 0 points
    Rose Row came 1 gets: 5 points
    Rose Row came 5 gets: 0 points
    Bassinet came 3 gets: 2 points
    Bassinet came 3 gets: 2 points
    Adage came 4 gets: 1 points
    Adage came 6 gets: 0 points
    Gamesters Lady came 4 gets: 1 points
    Gamesters Lady came 0 gets: 0 points
    Never Pink came 6 gets: 0 points
    Never Pink came 0 gets: 0 points
    Queen Excalibur came 0 gets: 0 points
    Queen Excalibur came 9 gets: 0 points

    Spring Dream overall got place points of 3
    Rose Row overall got place points of 5
    Bassinet overall got place points of 4
    Adage overall got place points of 1
    Gamesters Lady overall got place points of 1
    Never Pink overall got place points of 0
    Queen Excalibur overall got place points of 0

    Overall points are:- 14.0

    Spring Dream's TotalPoints are: - 3.0
    Rose Row's TotalPoints are: - 5.0
    Bassinet's TotalPoints are: - 4.0
    Adage's TotalPoints are: - 1.0
    Gamesters Lady's TotalPoints are: - 1.0
    Never Pink's TotalPoints are: - 0.0
    Queen Excalibur's TotalPoints are: - 0.0

    Spring Dream has point percentage of 21.428571428571427
    Rose Row has point percentage of 35.714285714285715
    Bassinet has point percentage of 28.57142857142857
    Adage has point percentage of 7.142857142857142
    Gamesters Lady has point percentage of 7.142857142857142
    Never Pink has point percentage of 0.0
    Queen Excalibur has point percentage of 0.0
    Spring Dream's Course and Distance total is: 3.0
    Rose Row's Course and Distance total is: 3.0
    Bassinet's Course and Distance total is: 1.0
    Adage's Course and Distance total is: 1.0
    Gamesters Lady's Course and Distance total is: 1.0
    Never Pink's Course and Distance total is: 1.0
    Queen Excalibur's Course and Distance total is: 0.0
    Total for course and distance 10.0
    Course and Distance percentage for Spring Dream is 30.0
    Course and Distance percentage for Rose Row is 30.0
    Course and Distance percentage for Bassinet is 10.0
    Course and Distance percentage for Adage is 10.0
    Course and Distance percentage for Gamesters Lady is 10.0
    Course and Distance percentage for Never Pink is 10.0
    Course and Distance percentage for Queen Excalibur is 0.0

    Spring Dream gets 61.53846153846154 tips percentage
    Rose Row gets 15.384615384615385 tips percentage
    Bassinet gets 7.6923076923076925 tips percentage
    Adage gets 7.6923076923076925 tips percentage
    Gamesters Lady gets 0.0 tips percentage
    Never Pink gets 7.6923076923076925 tips percentage
    Queen Excalibur gets 0.0 tips percentage

    Spring Dream gets Overall percentage of 132.96703296703296
    Rose Row gets Overall percentage of 101.0989010989011
    Bassinet gets Overall percentage of 66.26373626373626
    Adage gets Overall percentage of 44.83516483516483
    Gamesters Lady gets Overall percentage of 23.80952380952381
    Never Pink gets Overall percentage of 24.358974358974358
    Queen Excalibur gets Overall percentage of 6.666666666666667


    Horses in ascending order of percentages gained:-
    Horse Queen Excalibur has 6.666666666666667 points
    Horse Gamesters Lady has 23.80952380952381 points
    Horse Never Pink has 24.358974358974358 points
    Horse Adage has 44.83516483516483 points
    Horse Bassinet has 66.26373626373626 points
    Horse Rose Row has 101.0989010989011 points
    Horse Spring Dream has 132.96703296703296 points

    C:\Java\Java Six>

    [edit]Disable smilies. CR[/edit]
    [ August 17, 2008: Message edited by: Campbell Ritchie ]
     
    Steve Luke
    Bartender
    Posts: 4179
    22
    IntelliJ IDE Python Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Hi Simon,

    That is great you got it working, congratulations! I agree with the company who told you to get some experience by coding for a hobby. Doing is definitely the best way of learning, and coding something new that you are interested in helps keep you motivated.

    If you wanted to continue with this sample a little bit more, you might look into cleaning up all those decimal places in the output. How many of those decimal places are significant? You might want to look into DecimalFormat or using the System.out.printf (from PrintStream) which uses java.util.Formatter to display data.

    And you should definitely investigate why the Generics and Annotations didn't work. These two tools are fairly new to Java, and are great skills to show off.
     
    Campbell Ritchie
    Marshal
    Posts: 79962
    396
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Well done and I agree with Steve about practice.
     
    A magnificient life is loaded with tough challenges. En garde tiny ad:
    Gift giving made easy with the permaculture playing cards
    https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
    reply
      Bookmark Topic Watch Topic
    • New Topic