Hello,
I think it's safe to say that I am totally off here. I need some help.

The program should include two classes Book and Author.
A Book should contain a title, an ISBN and publication year. An Author should contain a name and a sorted linked list of Books by publication year. In addition to a constructor that can be used to initialize a Book, the class should define a displayValues() method that outputs the values of the Book objects sorted by publication year. Also it should define a method findBook(string) to search for book by title.
The Author class should contain a constructor used to initialize an Author, a displayValues() method, and a method addBook(Book); this method must add books sorted by publication year. The main() function of the program should create two Author objects and several Book objects assigned to each Author. The displayValues() method of the Author objects should be used to output the values for each Author.

        class LinkedList1
        {



        private class Node
        {
            String value;   
            Node next;      




            Node(String val, Node n)
            {
                value = val;
                next = n;
            } 



            Node(String val)
            {

            }
        }   

        private Node first;  
        private Node last;   



        public LinkedList1()
        {
            first = null;
            last = null;        
        }



        public boolean isEmpty()
        {        
            return first == null;       
        }



        public int size()
        {
           int count = 0;
           Node p = first;     
             while (p != null)
           {
               // There is an element at p
               count ++;
               p = p.next;
           }
           return count;
        }



        public void add(String e)
        {
          if (isEmpty()) 
          {
              first = new Node(e);
              last = first;
          }
          else
          {

              last.next = new Node(e);
              last = last.next;
          }      
        }



        public void add(int index, String e)
        {
             if (index < 0  || index > size()) 
             {
                 String message = String.valueOf(index);
                 throw new IndexOutOfBoundsException(message);
             }


             if (index == 0)
             {

                 first = new Node(e, first);
                 if (last == null)
                     last = first;
                 return;
             }


             Node pred = first;        
             for (int k = 1; k <= index - 1; k++)        
             {
                pred = pred.next;           
             }


             pred.next = new Node(e, pred.next);  


             if (pred.next.next == null)
                 last = pred.next;         
        }



        public String toString()
        {
          StringBuilder strBuilder = new StringBuilder();


          Node p = first;
          while (p != null)
          {
             strBuilder.append(p.value + "\n"); 
             p = p.next;
          }      
          return strBuilder.toString(); 
        }




        public String remove(int index)
        {
           if (index < 0 || index >= size())
           {  
               String message = String.valueOf(index);
               throw new IndexOutOfBoundsException(message);
           }

           String element;  // The element to return     
           if (index == 0)
           {

              element = first.value;    
              first = first.next;
              if (first == null)
                  last = null;             
           }
           else
           {

              Node pred = first;


              for (int k = 1; k <= index -1; k++)
                  pred = pred.next;


              element = pred.next.value;


              pred.next = pred.next.next;  


              if (pred.next == null)
                  last = pred;              
           }
           return element;        
        }  





        public boolean remove(String element)
        {
           if (isEmpty()) 
               return false;      

           if (element.equals(first.value))
           {

              first = first.next;
              if (first == null)
                  last = null;       
              return true;
           }


          Node pred = first;
          while (pred.next != null && 
                 !pred.next.value.equals(element))
          {
              pred = pred.next;
          }


          if (pred.next == null)
              return false;


          pred.next = pred.next.next;    


          if (pred.next == null)
              last = pred;

          return true;       
        }

        public static void main(String [] args)
        {
            LinkedList1 ll = new LinkedList1();
            ll.add("");
            ll.add("");
            ll.add(0, "");
            ll.add(2, "");
            ll.add(4, "");
            System.out.println("The members of the list are:");
            System.out.print(ll);
        }
    } 

I have no idea how to do this. But this one adds/removes and displays the content, and it starts with an empty list that can be filled.
So you basically just have the name, author, and ISBN and it sorts it and then displays it. The question is if this is ok like this and you just need to add something that will sort it?

Edit: Can't get the formatting right. Sorry. I don't like this new design.

Recommended Answers

All 9 Replies

It's time to go back to the requirenments statement. You have a linked list (but why do I suspect that you just copied that from somewhere without really understanding it???), which you can use for the list of Books for each Author, but now you need the Book and Author classes. Just follow the instructions that you quoted in your first post.
(I too an having difficulty getting used to the new design. What happened to ther spell checking? I hope they know what they are doing!)

I did read it already, of course. I'm not sure where I can add the author and book class.

Well, to start off, create 2 new files, 1 called Author, 1 called Book. Just make sure they are created in the same package(folder) to avoid any unecessary confusion.

Then do exactly what your brief said. Create the variables needed in each class.

Java already has it's own LinkedList class and many other data structures. I am sure you are not expected to create your own data structure from scratch like what you have tried(or copied) to do.

I don't like doing this separately, though. What's wrong with putting it into one? It's a practice problem and I've been reading the book, but it's not been very helpful. :( How will it be sorted, though? Just by an appropriate sorting mechanism ? I want to say I read that the list can also sort it itself somehow.

You can put more than one class definition into a java source file. Only one can be public at the root level. Also you can define classes inside of other classes.

There are utility classes: Arrays and Collections that have a sort method you can use if you properly define the class to be sorted.

I understand, but I still don't know how to approach this. I don't get it. I do know I have to create 2 classes here. That's not the issue. The issue is building/implementing the linked list.

First you need to make a list of the features you want the LL to have. Then you design and code, compile and test each method. Do you have a list of the features?

That's another thing. I don't even know what is required. Obviously the add mehtod, though. I find the book lousy. It tells you about clear method etc. , but does not specify what/when etc. You will also need the get method, and isempty and size method. I don't see where something would be removed. I hope that is it. So, yes, then you use Stringlist interface to specify the supported methods.

So start with the add method.

Look at Java SE LinkedList class to get ideas for other methods you might want to include, but don't include them all.
Do one at a time: compile, execute verify the results before adding the next method.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.