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
Gameclass. The class ispublic. We can’t declare a top level class asprotectedorprivate. It will always give an error. -
According to the problem statement, we only need three
privatedata 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 theGameclass.
Point 4:
-
Now we have three mutator methods, all with the same name:
updateGame. This is allowed as long as they are overloading each other.
Ask