aspose file tools*
The moose likes Beginning Java and the fly likes problem solving using Linked List Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "problem solving using Linked List" Watch "problem solving using Linked List" New topic
Author

problem solving using Linked List

Mona Alsh
Ranch Hand

Joined: Dec 20, 2012
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 !!

Thanks in advance

Tony Docherty
Bartender

Joined: Aug 07, 2007
Posts: 2317
    
  49
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

Joined: Oct 01, 2001
Posts: 2854
    
  11

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

Joined: Aug 07, 2007
Posts: 2317
    
  49
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

Joined: Dec 20, 2012
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

Joined: Dec 20, 2012
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

Joined: Dec 20, 2012
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

Joined: Oct 01, 2001
Posts: 2854
    
  11

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

Joined: Nov 19, 2001
Posts: 189

If you have

Mona Alsh
Ranch Hand

Joined: Dec 20, 2012
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

Joined: Dec 20, 2012
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: problem solving using Linked List