0

Hello ive added two new functions to the implementation of an interface.

this is the implementation file...

import au.edu.uow.Collection.Album;
import java.util.ArrayList;

public class CDAlbum implements Album {
    private String Title;
    private String Genre;
    private String Artist;
    private String MediaType;
    private ArrayList<String> trackList;

    public CDAlbum(String TempTitle, String TempGenre, String TempArtist, ArrayList<String> TempTracklist, String TempMediaType){
        //Set initail variable values
        Title = TempTitle;
        Genre = TempGenre;
        Artist = TempArtist;
        trackList = TempTracklist;
        MediaType = TempMediaType;
    }
    //Accessor Functions
    public String getMediaType(){
        //Return Media Type
        return MediaType;
    }
    public String getTitle(){
        //Return Title
        return Title;
    }
    public String getGenre(){
        //Return Genre
        return Genre;
    }
    public String getArtist(){
        //Return Artist
        return Artist;
    }
    public ArrayList<String> getTrackList(){
        //Return Tracklist
        return trackList;
    }
}

The bottom two functions are the added functions( getArtist(), getTrackList()) the problem im having is that when i try to call these functions from a class file it give me the following errors.

./au/edu/uow/UserInterface/UserInterface.java:95: cannot find symbol
symbol  : method getArtist()
location: interface au.edu.uow.Collection.Album
            System.out.println(albumCollection.get(number).getArtist());
                                                          ^
./au/edu/uow/UserInterface/UserInterface.java:98: cannot find symbol
symbol  : method getTrackList()
location: interface au.edu.uow.Collection.Album
            ArrayList<String> trackList = albumCollection.get(number).getTrackList();

When i call the functions

import au.edu.uow.Collection.Album;

System.out.println(albumCollection.get(number).getArtist());

//Access the track titles
ArrayList<String> trackList = albumCollection.get(number).getTrackList();
//Output collection
int arrayListSize = trackList.size();
for(int i = 0; i < arrayListSize; i++)
{
    System.out.println( i + ": " + trackList.get(i));
}
7
  • could you show us your interface Album as well?! Seems these methods are missing in there. Aug 26, 2012 at 6:04
  • provide the code of Album class Aug 26, 2012 at 6:06
  • @DanielDC Album specifies neither getArtist nor getTrackList, only CDAlbum does. You can determine whether the Album is a CDAlbum or DVDAlbum by checking Album.getMediaType; then, if it is a CD, you can cast to CDAlbum and invoke getArtist and getTrackList then.
    – obataku
    Aug 26, 2012 at 6:10
  • I think Album is a super of CDAlbum or DVDAlbum interface. pls provide CDAlbum Interface Aug 26, 2012 at 6:17
  • @MohammodHossain Album is an interface which CDAlbum and DVDAlbum implement.
    – obataku
    Aug 26, 2012 at 6:18

4 Answers 4

0

Album specifies neither getArtist nor getTrackList:

public interface Album {
    /**
     * This method returns the media type of the album. 
     * @return the media type of the album, either CD or DVD
     * @see #getTitle()
     * @see #getGenre()
     */
     String getMediaType();

    /**
     * This method returns the title of the album. 
     * @return the title of the album
     * @see #getMediaType()
     * @see #getGenre()
     */
     String getTitle();

    /**
     * This method returns the genre of the album. 
     * @return the genre of the album
     * @see #getTitle()
     * @see #getMediaType()
     */
    String getGenre();

}

... only CDAlbum does.

You can determine whether the Album is a CDAlbum or DVDAlbum by checking Album.getMediaType; then, if it is a CD, you can cast to CDAlbum and invoke getArtist and getTrackList then.

for (final Album album : albumCollection) {
  final String type = album.getMediaType();
  System.out.print(type + " album: " + album.getTitle()
      + " (" + album.getGenre() + ") - ");
  if (type.equals("CD")) {
    final CDAlbum cd = (CDAlbum) album;
    System.out.println(cd.getArtist());
    int n = 0;
    for (final String track : cd.getTrackList()) {
      System.out.printf("#%2d - %s\n", ++n, track);
    }
  } else {
    final DVDAlbum dvd = (DVDAlbum) album;
    System.out.println(dvd.getDirector());
    System.out.println(dvd.getPlotOutline());
  }
}
0
0

Is Album the return type of albumCollection.get(number)? If so, then you need to add getArtist and getTrackList method into your interface as well.

3
  • then albumCollection.get(number) return type should be CDAlbum. However, that would kill the whole point of having an interface.
    – Infinity
    Aug 26, 2012 at 6:05
  • can i just make the variables public and get them to print like that Aug 26, 2012 at 6:14
  • Nope because Album doesn't know anything about CDAlbum. CDAlbum is an album but Album is not a CDAlbum.
    – Infinity
    Aug 26, 2012 at 6:31
0

When you add new method in your interface you must implement in your implementation class.

check the type of albumCollection.get(number) if albumCollection.get(number) return Album then you can call getArtist()

1
  • you mean i have to declare it in the interface ? is there another method so that i can just at it to the interface class? Aug 26, 2012 at 6:01
0

Create a super class say for example MasterAlbums and extend your CDAlbum and DVDAlbum from it.Place the methods getArtist() & getTrackList() in the super class.Do not override it in the subclasses.Now when you call System.out.println(albumCollection.get(number).getArtist()); , It will refer to the superclass method and your artist & tracklist will be printed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.