Search code examples
javaarraysarraylistbluej

Class cannot be converted to ArrayList (Java)


error message - Game cannot be converted to GameCollection

I want to apply binary search algorithm to my array named gameCollection. The array is using set of variables from Game class to define components of the array. I added set of records to the array but the algorithm does not want to find the middle value.

I think this is because the collection is not in alphabetical order and this could cause an error and here comes the other question is this how you sort the array using other class? as I did some research and I'm still unsure about it. Here is my code so far:

GameCollection class:

public class GameCollection  {
    // instance variables - replace the example below with your own
    private ArrayList<Game> gameCollection = new ArrayList<Game>();

    /**
     * Constructor for objects of class GameCollection
     */
    public GameCollection()
{
    setGameCollection();
}

public void setGameCollection()
{
    Game g1 = new Game("Little Big Planet", "PS3", 34, 7, 2014, "");
    Game g2 = new Game("Grand Theft Auto 4", "PS3", 23, 3, 2013, "");
    Game g3 = new Game("Grand Theft Auto 4", "X360", 23, 12, 2012, "");
    Game g4 = new Game("Last Of Us", "PS3", 23, 18, 2011, "");
    Game g5 = new Game("Metal Gear Rising: Revengeance", "Win", 76, 7, 2010, "");
    Game g6 = new Game("Don't Starve: Console Edition", "PS4", 5, 3, 2014, "");
    Game g7 = new Game("Grand Theft Auto 5", "PS4", 34, 12, 2008, "");
    Game g8 = new Game("KickBeat", "Win", 23, 18, 2007, "");
    Game g9 = new Game("Tomb Raider: Definitive Edition", "PS4", 87, 15, 2006, "");
    Game g10 = new Game("Tomb Raider: Definitive Edition", "XBO", 2, 18, 2005, "");
    Game g11 = new Game("NaissanceE", "Win", 34, 7, 2014, "");
    Game g12 = new Game("LocoCycle", "X360", 4, 3, 2013, "");
    Game g13 = new Game("LocoCycle", "Win", 56, 12, 2012, "");
    Game g14 = new Game("Assassin's Creed IV: Black Flag - Freedom Cry", "PS3", 10, 18, 2011, "");
    Game g15 = new Game("Assassin's Creed IV: Black Flag - Freedom Cry", "PS4", 11, 15, 2010, "");
    Game g16 = new Game("Basement Crawl", "PS4", 34, 3, 2009, "");
    Game g17 = new Game("Castlevania: Lords of Shadow 2", "PS3", 22, 12, 2008, "");
    Game g18 = new Game("Castlevania: Lords of Shadow 2", "X360", 46, 18, 2007, "");
    Game g19 = new Game("Castlevania: Lords of Shadow 2", "Win", 55, 15, 2006, "");
    Game g20 = new Game("Dead Nation: Apocalypse Edition", "PS4", 43, 3, 2005, "");
    Game g21 = new Game("South Park: The Stick of Truth", "PS3", 52, 12, 2004, "");

    gameCollection.add(g1);
    gameCollection.add(g2);
    gameCollection.add(g3);
    gameCollection.add(g4);
    gameCollection.add(g5);
    gameCollection.add(g6);
    gameCollection.add(g7);
    gameCollection.add(g8);
    gameCollection.add(g9);
    gameCollection.add(g10);
    gameCollection.add(g11);
    gameCollection.add(g12);
    gameCollection.add(g13);
    gameCollection.add(g14);
    gameCollection.add(g15);
    gameCollection.add(g16);
    gameCollection.add(g17);
    gameCollection.add(g18);
    gameCollection.add(g19);
    gameCollection.add(g20);
    gameCollection.add(g21);

    Collections.sort(gameCollection, new Comparator<Game>()
    {
        @Override
        public int compare(Game g1, Game g2)
        {
            return g1.title.compareTo(g2.title);            
        }
    }
    );

    //ArrayList.sort(gameCollection);
    //this collection must be in alfabetical order
}

public GameCollection searchGame(String title)
{
    if (gameCollection.size() == 0)
    {
        return null;
    }

    int low = 0;
    int high = gameCollection.size()-1;

    while(low <= high)
    {
        int middle = (low + high) / 2;
        if (title.compareTo(gameCollection.get(middle).getTitle()) > 0)
        {
            low = middle + 1;
        }
        else if (title.compareTo(gameCollection.get(middle).getTitle()) <0)
        {
            high = middle - 1;
        }
        else
        {
            return gameCollection.get(middle);
        }
    }
}
}

Here is my Game class:

public class Game
{
// instance variables - replace the example below with your own
public String title;
public String console;
public int quantity;
public int ageRating;
public int releaseDate;
public String pict; 

/**
 * Constructor for objects of class Game
 */
public Game(String title, String console, int quantity, int ageRating, int releaseDate, String pict)
{
    // initialise instance variables
    this.title = title;
    this.console = console;
    this.quantity = quantity;
    this.ageRating = ageRating;
    this.releaseDate = releaseDate;
    this.pict = pict;
}

public void setTitle(String title)
{
    this.title = title;
}

public void setConsole(String console)
{
    this.console = console;
}

public void setQuantity(int quantity)
{
    this.quantity = quantity;
}

public void setAgeRating(int ageRating)
{
    this.ageRating = ageRating;
}

public void setReleaseDate(int releaseDate)
{
    this.releaseDate = releaseDate;
}

public void setPict(String pict)
{
    this.pict = pict;
}

/**
 * An example of a method - replace this comment with your own
 * 
 * @param  y   a sample parameter for a method
 * @return     the sum of x and y 
 */
public String getTitle()
{
    return title;
}

public String getConsole()
{
    return console;
}

public int getQuantity()
{
    return quantity;
}

public int getAgeRating()
{
    return ageRating;
}

public int getReleaseDate()
{
    return releaseDate;
}

public String getPict()
{
    return pict;
}

public String toString()
{
    //return all the variables (contcatination)
    return "<html>Game Details:<br>"
    + "<br>Title:" + title
    + "<br>Console:" + console
    + "<br>Quantity:" + quantity
    + "<br>Age Rating:" + ageRating
    + "<br>Release Date:" + releaseDate
    + "<br>Picture:" + pict;
}

}

Solution

  • As I commented, searchGame signature returns GameCollection, but your return clause returns Game, because gameCollection is an array of Game.

    So, there are two possibilities, depending on your needs:

    • Change method's signature to: public Game searchGame(String title)

    • Change returns clause to return a sublist of gameCollection containing the element you want: gameCollection.subList(middle, middle+1);