Solution Review: Design a PlayStation
Have a look at the solution to the 'Design a PlayStation' challenge.
We'll cover the following...
The Game
class
Rubric criteria
Press + to interact
public class Game // Header of the class{// Private instancesprivate String name;private int levels;private int scorePerLevel;// Parameterized constructorspublic Game(String name, int scorePerLevel, int levels){this.name = name;this.scorePerLevel = scorePerLevel;this.levels = levels;}// Return total number of levelspublic int getTotalLevels(){return levels;}// Updating the game information - overloaded methodspublic 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 levelpublic int getScorePerLevel(){return scorePerLevel;}}
Rubric-wise explanation
Point 1:
-
At line 1, we declare the header of the
Game
class. The class ispublic
. We can’t declare a top level class asprotected
orprivate
. It will always give an error. -
According to the problem statement, we only need three
private
data members:name
: Name of the gamelevels
: Total levels in a gamescorePerLevel
: Score assigned upon level completion
Point 2:
- Then, we make a constructor. Look at line 9. We declare a -parameter constructor that accepts three values for
name
,scorePerLevel
, andlevels
.
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 returnslevel
, a private instance of theGame
class.