• 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
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Tim Cooke
Sheriffs:
  • Rob Spoor
  • Liutauras Vilda
  • paul wheaton
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Mikalai Zaikin
  • Carey Brown
  • Piet Souris
Bartenders:
  • Stephan van Hulst

sorting fractions problem

 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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

 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
andrew cassato
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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"
 
Bartender
Posts: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Same thread? Same context- presented differently.
 
Ulf Dittmer
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Sheriff
Posts: 22809
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
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.
 
Marshal
Posts: 79786
382
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i modified it a bit but still nothing... anyone?

 
Rob Spoor
Sheriff
Posts: 22809
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

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
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 22809
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
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
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 22809
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
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.
 
A tiny monkey bit me and I got tiny ads:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic