Hey all, thank you for taking time to help me out, I am in need of some help.
This is what is required from me:
Develop 3 ordered collection classes each storing a collection of Album objects in a sorted order but implemented in a different way as follows:
1.
2. 3.
The first class saves the data in an array. This class is similar to exercise 2 but here the elements have to maintain their sorted order after every addition operation. The second class saves data in a singly linked list. The third class saves the data in a doubly linked list.
Each of the above classes must implement the Iterable interface. You will need to define Iterator classes that implement the Iterator interface. Consult the Java documentations for more on these two interfaces and the relation between them. In addition to the methods add and remove, implement the following methods in each class:
E first() Returns the first (lowest) element currently in this sorted set.
E last() Returns the last (highest) element currently in this sorted set.
int size() Returns the number of elements in this set (its cardinality).
boolean isEmpty() Returns true if this set contains no elements.
boolean contains(E e) Returns true if the list contains the specified element.
void clear() Removes all of the elements from this list.
boolean equals(Object o) Returns true if the specified object is also a set, the two sets have the same size, and every member of the specified list is contained in this list.
Note: the remove method specified in the Iterator interface class should generate an exception when invoked. It is a design flaw for an Iterator object to modify the collection object that it is iterating on.
Driver Class
Write a driver class that tests the implemented classes and all their methods reporting the state of the collection (by printing it) every time it is modified. You may hard code the Album objects to be created and added to the collections.
----------------------------------------------------------------
Until now, I haven't managed to understand at all what is needed however I managed to get this far
CLASS ALBUM
public class Album implements Comparable<Album>
{
private String title;
private String artist;
private String[] categories;
private String[] categories2;
private int year;
private long reference;
public Album ( String a, String t, int y, long r, String[] c)
{
title = t;
artist = a;
categories = c.clone(); //change to for loop
year = y;
reference = r;
}
public String getTitle()
{
return title;
}
public String getArtist()
{
return artist;
}
public String[] getCategories()
{
return categories;
}
public int getYear()
{
return year;
}
public long getReference()
{
return reference;
}
public String toString()
{
String str = categories[0];
for (int i = 1; i < categories.length ; i++)
str = str + ", " + categories[i];
String out;
out = artist + "; " + title + "; " +"(c)" + year + "; " + str + "; " + reference;
return out;
}
public boolean equals(Album x)
{
boolean equal;
if (x.getArtist().equals(artist) && x.getTitle().equals(title) && x.getReference()==reference)
equal = true;
else equal = false;
return equal;
}
public int compareTo(Album x)
{
int j = 0;
if (x.getArtist().compareTo(artist) > 0 || x.getTitle().compareTo(title) >0 || x.getReference() > (reference))
j = 1;
else if (x.getArtist().compareTo(artist) < 0 || x.getTitle().compareTo(title) <0 || x.getReference() < (reference))
j=-1;
return j;
}
}
CLASS ALBUMCOLLECTION
import java.util.*;
public class AlbumCollection implements Iterable<Album>
{
private String owner;
private Album[] albumList;
private int arrayCount = 0;
public AlbumCollection (String ownerName)
{
owner = ownerName;
albumList = new Album[0];
}
public Iterator<Album> iterator()
{
}
public void add (Album a)
{
int arraySize = albumList.length;
Album temp;
if (arrayCount == albumList.length)
{
Album [] oldAlbumList = albumList;
albumList = new Album [arraySize+1];
for ( int i = 0 ; i < oldAlbumList.length; i++)
albumList[i] = oldAlbumList[i];
}
albumList[arrayCount] = a;
arrayCount ++;
for (int i = 0 ; i < albumList.length ; i++)
{
if (a.compareTo(albumList[i]) < 0 )
{
temp = albumList[i];
albumList[i] = a;
a = temp;
}
else if (a.compareTo(albumList[i]) > 0 )
{
temp = a;
a = albumList[i];
albumList[i] = temp;
}
}
}
public String toString()
{
String out = "Music Collection of " + owner + "\n" + "\n";
for (int i = 0; i < albumList.length ; i++)
out = out + " " + albumList[i] + "\n";
return out;
}
public class innerClass implements Iterator<Album>
{
private Album nextElement;
public Album next()
{
return nextElement;
}
public boolean hasNext()
{
if (nextElement != null)
return true;
else
return false;
}
public void remove()
{
}
}
}
CLASS ALBUMCOLLECTIONDRIVER
import java.util.*;
import java.io.*;
public class AlbumCollectionDriver
{
public static void main(String[] args) throws FileNotFoundException
{
FileReader fileReader = new FileReader("Albums.txt");
Scanner scan = new Scanner (fileReader);
String albumOwner = scan.nextLine();
AlbumCollection coll = new AlbumCollection(albumOwner);
scan.nextLine();
while (scan.hasNextLine())
{
String Artist = scan.nextLine();
String name = scan.nextLine();
int Year = Integer.parseInt(scan.nextLine());
long reference = Long.parseLong(scan.nextLine());
String newLine;
ArrayList<String> categoriesArrayL = new ArrayList<String>();
while(!(newLine = scan.nextLine()).equals(""))
{
categoriesArrayL.add(newLine);
}
String [] categories = new String [categoriesArrayL.size()];
for (int i = 0; i < categoriesArrayL.size() ; i++)
categories[i] = categoriesArrayL.get(i);
Album album = new Album (Artist, name, Year, reference ,categories);
coll.add(album);
}
System.out.println(coll);
}
}
can anyone give me a push towards what i should do.. explain the exercise in a more detailed way?
Thank you