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.