Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 14 of 14

Thread: Could someone give me some insight as to how to do this?

  1. #1
    Member
    Join Date
    Aug 2013
    Posts
    39
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Could someone give me some insight as to how to do this?

    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:

    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.
    Here is what I have for the two classes so far:

    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.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,198
    Thanks
    49
    Thanked 2,172 Times in 2,145 Posts

    Default Re: Could someone give me some insight as to how to do this?

    Could you list the methods you are having problems with?
    This is one of the last four and it looks too easy to be a problem.
    ● int getYear()*—*returns*the*release*year
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Aug 2013
    Posts
    39
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Could someone give me some insight as to how to do this?

    That is in the Album class, the only part of that class that I needed help with is the last one. Its the MusicCollection class that I need help with the last four methods. Sorry for the confusion.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,198
    Thanks
    49
    Thanked 2,172 Times in 2,145 Posts

    Default Re: Could someone give me some insight as to how to do this?

    Please list the methods you are having problems with and show what you have tried for them.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Member
    Join Date
    Aug 2013
    Posts
    39
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Could someone give me some insight as to how to do this?

    Ok, for starters, I am having trouble figuring out how to do this compareTo method. I have tried to logically work through it with if statements but I keep getting stuck. This is what I have so far:

    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;
            * 
            */
        }

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,198
    Thanks
    49
    Thanked 2,172 Times in 2,145 Posts

    Default Re: Could someone give me some insight as to how to do this?

    I keep getting stuck.
    Could you be more specific and describe what you are stuck on?

    What works and what doesn't work?

    What are the rules for sorting albums? There are three fields. Which fields control the order?

    The variables don't follow standard naming conventions making the code confusing to read. Variable names and method should start with lowercase letters.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Member
    Join Date
    Aug 2013
    Posts
    39
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Could someone give me some insight as to how to do this?

    Okay, here is what I have gotten so far. I only have one more thing that needs to be implemented.

    Album Class:

    import java.io.*;
    import java.util.*;
     
    /**
     *
     * @author Jared
     */
    public class Album {
        //Variables
        public String Artist;
        public String Title;
        public 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 (Artist.equals(otherArtist) && Title.equals(otherTitle) && Year == otherYear){
                return 0;
            }
            else if (! Artist.equals(otherArtist)) {
                int compare = Artist.compareToIgnoreCase(otherArtist);
     
                if (compare < 0){
                    return -1;
                }
                else if (compare > 0){
                    return 1;
                }
            }
     
            else if (Artist.equals(otherArtist) ) {
                if (Year > otherYear){
                    return -1;
                }
                else if (Year < otherYear){
                    return 1;
                }
            }
     
            else if (Artist.equals(otherArtist) && Year == otherYear && ! Title.equals(otherTitle)) {
                int compare = Title.compareToIgnoreCase(otherTitle);
     
                if (compare < 0){
                    return -1;
                }
                else if (compare > 0){
                    return 1;
                }           
            }
            return 0;
        }
    }

    MusicCollection Class:
     
    import java.io.*;
    import java.util.*;
     
    /**
     *
     * @author Jared
     */
    public class MusicCollection implements Iterable<Album> {
        public ArrayList<Album> musicCollection = new ArrayList<> ();
     
     
        public  void addAlbum(Album album){
            musicCollection.add(album);
        }
     
        public void removeAlbum(Album album) {
            musicCollection.remove(album);
        }
     
        public Album[] albumsByArtist(String artist){
            String Artist = artist;
            Album[] albumsByArtist;
            ArrayList<Album> tempArrayList = new ArrayList<> ();
            for (int i = 0; i < musicCollection.size(); i++ ){
                if (Artist.equals(musicCollection.get(i).getArtistName())) {
                    tempArrayList.add(musicCollection.get(i));
                }
            }
            albumsByArtist = (Album[]) tempArrayList.toArray();
            return albumsByArtist;
        }
     
        public Album[] albumsByYear(int year) {
            int Year = year;
            Album[] albumsByYear;
            ArrayList<Album> tempArrayList = new ArrayList<> ();
            for (int i = 0; i < musicCollection.size(); i++){
                if (Year == musicCollection.get(i).getYear()){
                    tempArrayList.add(musicCollection.get(i));
                }
            }
            albumsByYear = (Album[]) tempArrayList.toArray();
            return albumsByYear;
        }
     
        /**
         *
         * @return
         */
        @Override
        public Iterator<Album> iterator(){
     
        }
    }

    I don't understand how to implement the iterator for the MusicCollection class. My teacher said that since I was using an ArrayList, that it would generate it for me, but I still do not understand how to write it.

    Here is the directions for it:

    In addition, MusicCollection must implement the Iterable<Album> interface. This requires
    you implement the following method:
    ● Iterator<Album> iterator() — returns an Iterator<Album> object that iterates
    through the albums in the collection in order of artist and year

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,198
    Thanks
    49
    Thanked 2,172 Times in 2,145 Posts

    Default Re: Could someone give me some insight as to how to do this?

    Writing an Iterator involves understanding how to write a class that implements an interface and how to write an inner class. Do you know about both those things?

    ArrayList, that it would generate it for me
    Have you read the API doc for the ArrayList class? If it has an iterator() method, you could have your method return what it returns.
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Member
    Join Date
    Aug 2013
    Posts
    39
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Could someone give me some insight as to how to do this?

    Hey, I apologize that my understanding of what a iterator is or does is poor. We have not really covered it.

    Is this what you are talking about?

        /**
         *
         * @return
         */
        @Override
        public Iterator<Album> iterator(){
            Iterator<Album> it = musicCollection.iterator();
            while (it.hasNext()){
                Album obj = it.next();
     
            }
            return null;
        }

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,198
    Thanks
    49
    Thanked 2,172 Times in 2,145 Posts

    Default Re: Could someone give me some insight as to how to do this?

    Did you read the API doc for the Iterator interface? Did you have any specific questions about what it says?

    The code in post#9 looks like it uses an Iterator.
    If you don't understand my answer, don't ignore it, ask a question.

  11. #11
    Member
    Join Date
    Aug 2013
    Posts
    39
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Could someone give me some insight as to how to do this?

    I am just confused as how to implement it. I feel like that is doing what I am suppose to do, Im just not sure. Thanks.

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,198
    Thanks
    49
    Thanked 2,172 Times in 2,145 Posts

    Default Re: Could someone give me some insight as to how to do this?

    What is the Iterator supposed to return? Will the Iterator for the musicCollection ArrayList return that value?
    If so, have Album iterator() method return what the musicCollection's iterator() returns.
    If you don't understand my answer, don't ignore it, ask a question.

  13. #13
    Member
    Join Date
    Aug 2013
    Posts
    39
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Could someone give me some insight as to how to do this?

    The Album class doesn't contain an iterator. Only the MusicCollection class contains an iterator. Since the ArrayList aready has a built in iterator, then do I even need to define an iterator for the ArrayList?

  14. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,198
    Thanks
    49
    Thanked 2,172 Times in 2,145 Posts

    Default Re: Could someone give me some insight as to how to do this?

    I saw this in the requirements the MusicCollection class:
    ● Iterator<Album> iterator()*—*returns*an*Iterator<Album>*object*tha t*iterates
    through*the*albums*in*the*collection*in*order*of*a rtist*and*year
    Note the order requirement. Is the arraylist sorted in that order?

    do I even need to define an iterator for the ArrayList?
    Are the requirements now satisfied?

    Do you have a program for testing that adds some albums, calls iterator() and prints out the results to show its working?
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Please give the solution
    By Abdul Matheen in forum Android Development
    Replies: 0
    Last Post: November 29th, 2012, 12:38 AM
  2. Replies: 4
    Last Post: February 23rd, 2011, 08:20 AM
  3. Trying to Give Coins For Change
    By bengregg in forum What's Wrong With My Code?
    Replies: 6
    Last Post: January 27th, 2011, 03:21 PM
  4. New give thanks feature
    By JavaPF in forum The Cafe
    Replies: 0
    Last Post: April 11th, 2009, 11:14 AM