aspose file tools*
The moose likes Beginning Java and the fly likes sorting fractions problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "sorting fractions problem" Watch "sorting fractions problem" New topic
Author

sorting fractions problem

andrew cassato
Ranch Hand

Joined: Nov 14, 2010
Posts: 37
I am trying to write this program which should sort a bunch of fractions which are pulled from a file and assigned to an array then print the sorted array. Using the debugger i can see the data is being read in properly but nothing prints. There are three classes, one for fractions, one to sort them and a test class to run everything. The fractions class was given so i know it is all done correctly but i started to get a null pointer exception error on line 155 and i cant figure out what to do.

Sorry its a lot of code and thanks in advance for taking a look.

FRACTIONS CLASS

SORT FRACTIONS CLASS

TEST CLASS

Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41508
    
  53
The selectionSort method replaces the existing array "A" with a new (and empty) array in line 77, and then proceeds to access the non-existing elements of that array.


Ping & DNS - my free Android networking tools app
andrew cassato
Ranch Hand

Joined: Nov 14, 2010
Posts: 37
Ulf Dittmer wrote:The selectionSort method replaces the existing array "A" with a new (and empty) array in line 77, and then proceeds to access the non-existing elements of that array.


I tried to take that line out and got the same error, im not sure how i can keep the data in the array "A"
Mohamed Sanaulla
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3068
    
  33

Same thread? Same context- presented differently.


Mohamed Sanaulla | My Blog
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41508
    
  53
Does the readArray method properly initialize all array elements?

This also looks odd:

A is assigned an array of null elements, and then replaced immediately with a reference to the "A" array in the SoftFractions class. I think "Fraction[] A = test.getSortedArray()" may be sufficient.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19670
    
  18

Three comments on your Fraction class in general:
1) don't call System.exit(1) if someone tries to create a fraction with a 0 denominator. That will end your entire application. Throw an exception instead:

2) Don't implement Comparator<Fraction>, implement Comparable<Fraction> instead. You want each fraction to be comparable to any other fraction, you don't want to use one fraction to compare two other fractions.
3) Your return value of compare has a potential for returning a wrong value if the number overflows. Correcting 2 and 3:
If you find that ternary operator difficult to understand, it's really quite easy:
- if l is negative, return a negative value. For compareTo it doesn't matter what negative value you return, as long as it's negative. -1 will do.
- if l is not negative, use another ternary operator:
--- if l is positive, return a positive value. Again, the value doesn't matter, so 1 will do.
--- if l is not positive it must be 0, so return 0.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38353
    
  23
I would add to the bit about throwing an exception: also throw an Exception if the absolute value |i| of the numerator or demoninator is ≥ √(Integer.MaX_VALUE); that way you can never suffer an overflow. Then you can use the subtraction technique for your compareTo() method.
andrew cassato
Ranch Hand

Joined: Nov 14, 2010
Posts: 37
Ulf Dittmer wrote:Does the readArray method properly initialize all array elements?


This method seems to get all the data except the last set of integers, making up the last fraction.

The selection sort method seems to be wrong, it prints out the data but not sorted. Any pointers to clean up this method?
andrew cassato
Ranch Hand

Joined: Nov 14, 2010
Posts: 37
i modified it a bit but still nothing... anyone?

Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19670
    
  18

andrew cassato wrote:

If you would have listened to me, you wouldn't need that extra object "f". You could just have written Also, if you want to sort, you should consider using Arrays.sort unless writing the sorting is homework.
andrew cassato
Ranch Hand

Joined: Nov 14, 2010
Posts: 37
Rob Spoor wrote:
andrew cassato wrote:

If you would have listened to me, you wouldn't need that extra object "f". You could just have written Also, if you want to sort, you should consider using Arrays.sort unless writing the sorting is homework.


The fractions class was given to us so we are not supposed to make any changes to it... writing the sort is the assignment. i have tried everything with no luck. I get the data into the array but it wont sort them. is there a way to make it work without changing the fractions class?
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19670
    
  18

Sure. The only problem the Fraction class itself has is the Comparator / Comparable issue. And you can tell you professor that implementing Comparator<Fraction> instead of Comparable<Fraction> is bad design. Tell him I said so

SortFractions is where the real problem lies, and that's where you need to work on.
andrew cassato
Ranch Hand

Joined: Nov 14, 2010
Posts: 37
Rob Spoor wrote:Sure. The only problem the Fraction class itself has is the Comparator / Comparable issue. And you can tell you professor that implementing Comparator<Fraction> instead of Comparable<Fraction> is bad design. Tell him I said so

SortFractions is where the real problem lies, and that's where you need to work on.


i have been working on the sort fractions method for days now, thats why im here. i cant figure it out at all, but its pretty much too late at this point.

do you know why the sortFractions method isnt working?
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19670
    
  18

I think you'll need to do the swapping inside the loop. That way, A[minIndex] will always be smaller than any element after it; if it isn't then you swap the two.
Also, don't use an existing array index to do the swapping, use a temporary variable instead:
You'll now find that smallestIndex is in fact not necessary. I've just tested this code (using an int[] for sorting, but the principle is the same) and it produced the same results as Arrays.sort.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: sorting fractions problem