...

/

Solution Review: Design a PlayStation

Solution Review: Design a PlayStation

Have a look at the solution to the 'Design a PlayStation' challenge.

The Game class

Rubric criteria

Press + to interact
public class Game // Header of the class
{
// Private instances
private String name;
private int levels;
private int scorePerLevel;
// Parameterized constructors
public Game(String name, int scorePerLevel, int levels)
{
this.name = name;
this.scorePerLevel = scorePerLevel;
this.levels = levels;
}
// Return total number of levels
public int getTotalLevels()
{
return levels;
}
// Updating the game information - overloaded methods
public void updateGame(String name)
{
this.name = name;
}
public void updateGame(int scorePerLevel)
{
this.scorePerLevel = scorePerLevel;
}
public void updateGame(String name, int scorePerLevel)
{
this.name = name;
this.scorePerLevel = scorePerLevel;
}
// Getting maximum score per level
public int getScorePerLevel()
{
return scorePerLevel;
}
}

Rubric-wise explanation


Point 1:

  • At line 1, we declare the header of the Game class. The class is public. We can’t declare a top level class as protected or private. It will always give an error.

  • According to the problem statement, we only need three private data members:

    1. name: Name of the game
    2. levels: Total levels in a game
    3. scorePerLevel: Score assigned upon level completion

Point 2:

  • Then, we make a constructor. Look at line 9. We declare a 33-parameter constructor that accepts three values for name, scorePerLevel, and levels.

Now we create some methods. It may not make sense right now as to why we make them, but it will later on.

Point 3:

  • Look at line 17. We create an accessor method: getTotalLevels(). It returns level, a private instance of the Game class.
...