This week's book giveaway is in the Java in General forum.We're giving away four copies of Think Java: How to Think Like a Computer Scientist and have Allen B. Downey & Chris Mayfield on-line!See this thread for details.
Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!

# problem solving using Linked List

Mona Alsh
Ranch Hand
Posts: 32
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 !!

Tony Docherty
Bartender
Posts: 2957
59
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.

Greg Charles
Sheriff
Posts: 2985
12
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: 2957
59
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
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
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
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: 2985
12
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?

Carey Brown
Ranch Hand
Posts: 1458
16
• 1
If you have

Mona Alsh
Ranch Hand
Posts: 32
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
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