I am working on an assignment and I am having trouble figuring out exactly how to do it. I have already done a good bit of it, but I keep getting stuck.
Here is the assignment:
Here is what I have for the two classes so far:Implement*the*following*classes*in*Java:
MusicCollection
This*class*represents*a*collection*of*music*albums *(see*below).*It*should*provide*the*following
public*methods:
● void addAlbum(Album album)**adds*the*supplied*album*to*the*collection
● void removeAlbum(Album album)**removes*the*supplied*album*from*the*colle ction
● Album[] albumsByArtist(String artist)**returns*an*array*of*Album*objects*from
the*collection*that*match*the*artist*supplied.
● Album[] albumsByYear(int year)**returns*an*array*of*Album*objects*from*the
collection*that*match*the*year*supplied.
In*addition,*MusicCollection*must*implement*the*It erable<Album>*interface.*This*requires
you*implement*the*following*method:
● Iterator<Album> iterator()**returns*an*Iterator<Album>*object*tha t*iterates
through*the*albums*in*the*collection*in*order*of*a rtist*and*year
Album
This*class*represents*a*music*album.*It*should*pro vide*the*following*public*methods:
● Album(String artist, String title, int year)**constructor*accepting*the*artist
name,*the*title*of*the*album,*and*the*year*it*was* released
● String getArtistName()**returns*the*name*of*the*artist
● String getTitle()**returns*the*title*of*the*album
● int getYear()**returns*the*release*year
● boolean equals(Album otherAlbum)**returns*true*if*other*album*is*the*s ame*album
from*the*same*artist*and*the*same*year
● String toString()**returns*a*string*representing*the*alb ums*information.*The*string
should*look*like*this:*Artist*Name***Album*Title* (Year)
In*addition,*Album*must*implement*the*Comparable<A lbum>*interface.*This*requires*you
implement*the*following*method:
● int compareTo(Album otherAlbum)**compares*the*album*with*the*supplied
otherAlbum*argument.*Returns**1*if*otherAlbum*shou ld*be*sorted*after*the*album,*0*if
they*are*same*album,*and*1*if*otherAlbum*should*be *sorted*before.*The*sorting*should*be
based*on*artist,*then*year,*then*album.
Album Class:
import java.io.*; import java.util.*; /** * * @author Jared */ public class Album { //Variables String Artist; String Title; int Year = 0; public Album(String artist, String title, int year){ Artist = artist; Title = title; Year = year; } public String getArtistName() { return Artist; } public String getTitle() { return Title; } public int getYear() { return Year; } public boolean equals(Album otherAlbum) { String otherArtist = otherAlbum.getArtistName(); String otherTitle = otherAlbum.getTitle(); int otherYear = otherAlbum.getYear(); if (Artist.equals(otherArtist) && Title.equals(otherTitle) && Year == otherYear) { return true; } else{ return false; } } @Override public String toString() { String artistInfo = Artist + " - " + Title +"(" + Year + ")"; return artistInfo; } public int compareTo(Album otherAlbum) { String otherArtist = otherAlbum.getArtistName(); String otherTitle = otherAlbum.getTitle(); int otherYear = otherAlbum.getYear(); if(Album.equals(otherAlbum)){ return 0; } else if (Artist.equals(otherArtist) ) { if (Year > otherYear){ return -1; } else if (Year < otherYear){ return 1; } } /* * else if (Artist.equals(otherArtist) && Year == otherYear && ! Album.equals(otherAlbum)) { if (){ } } return 0; * */ } }
The place that I cannot figure out how to do is the last method, the compareTo method.
MusicCollection class:
import java.io.*; import java.util.*; /** * * @author Jared */ public class MusicCollection impliments Iterable<Album> { public ArrayList<Album> musicCollection = new ArrayList<> (); public void addAlbum(Album album){ musicCollection.add(album); } public void removeAlbum(Album album) { } public Album[] albumsByArtist(String artist){ } public Album[] albumsByYear(int year) { } public Iterator<Album> iterator(){ } }
I can't figure out how to do the last four methods. My teacher did not really cover Iterator.
If someone could help me understand what to do, I would very much appreciate it. Thank you.