• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

problem solving using Linked List

 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi there!!
I'm trying to solve a Data Structure programming Exercise, here is the problem requirements:

Farey Fraction of level one is defined as sequence (0/1,1/1), at level two , it is (0/1,1/2,1/1), at level three is (0/1,1/3,1/2,2/3,1/1)
at level four (0/1,1/4,1/3,1/2,2/3,3/4,1/1). So that at each level n, a new fraction a+b/c+d is inserted between two neighbor fractions a/c and b/d only if c + d <= n. write a program which for a number n entered by the user extends the linked list(sequence of terms) at level n and then display them?

Here is my implementation, I think I'm following the right logic but I have some warnings regarding the Object type I'm passing to the add () method from the LinkedList class

Fraction Class:



FractionList class:





and the sequence class which contains the methods to add fraction objects to the sequence and allow the user to enter the number of level
and define the method that should insert the fraction in the sequence between two adjacent terms if they meet the condition c + d <= n




I would really appreciate if you can help me find the error in my code !!

Thanks in advance

 
Bartender
Posts: 3323
86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch

I've edited your post by changing the 'javadoc' tags you used to 'code' tags so it displays properly on this forum.
 
Sheriff
Posts: 3064
12
Mac IntelliJ IDE Python VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Mona, welcome to JavaRanch!

It would be helpful if you posted the warnings you are getting. They probably are related to a new(ish) concept in Java called generics. Try changing the class declaration to:



and see if that makes them go away.
 
Tony Docherty
Bartender
Posts: 3323
86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your code contains IndexOutOfBoundException which should probably be IndexOutOfBoundsException

The warnings are due to you not using generics, see http://docs.oracle.com/javase/tutorial/extra/generics/index.html.
Note using generics is not compulsory (but is definitely recommended) and not using generics will not stop your code from running.
 
Mona Alsh
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tony Docherty wrote:Welcome to the Ranch

I've edited your post by changing the 'javadoc' tags you used to 'code' tags so it displays properly on this forum.



Thanks a lot ,, it is my first post !!
 
Mona Alsh
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Greg Charles wrote:Hi Mona, welcome to JavaRanch!

It would be helpful if you posted the warnings you are getting. They probably are related to a new(ish) concept in Java called generics. Try changing the class declaration to:



and see if that makes them go away.



here is the warning :

FractionSequence.java:17: cannot find symbol
symbol : class IndexOutOfBoundException
location: class FractionSequence
}catch(IndexOutOfBoundException ex){}
^
Note: FractionSequence.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
1 error
 
Mona Alsh
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tony Docherty wrote:Your code contains IndexOutOfBoundException which should probably be IndexOutOfBoundsException

The warnings are due to you not using generics, see http://docs.oracle.com/javase/tutorial/extra/generics/index.html.
Note using generics is not compulsory (but is definitely recommended) and not using generics will not stop your code from running.



yes, it is something related to generics but I don't know how to fix it ,
I have tried casting the Fraction node to object and pass it to the add() method but didn't work either!!
 
Greg Charles
Sheriff
Posts: 3064
12
Mac IntelliJ IDE Python VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Actually, that first one is a compile error. The second one is a warning. The difference is that errors prevent you from getting .class files output from the compiler. Warnings mean the compiler isn't completely happy about it, but does compile your code into .class files.

In any case, Tony spotted the error and showed you how to fix it, and both of us guessed what the warning was, and I showed you how to fix that. You should be good to go, right?
 
Saloon Keeper
Posts: 11054
88
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you have

 
Mona Alsh
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:If you have



Thank you very much ,,
yah ,,, it didn't work , I'll use iterator instead !!

Thank you again for spotting this run-time error
 
Mona Alsh
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I know it is a bit late,,,but I just had my holidays.
I don't know how to thank you, but you really helped me.
and here is my way giving back to this awesome community


Fraction Class



FractionList class



This the FractionSequence class




Thank you a lot
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic