In: Computer Science
JAVA - Programming Exercise 10-5.
The developers of a free online game named Sugar Smash have asked you to develop a class named SugarSmashPlayer that holds data about a single player. The class contains the following fields: idNumber - the player’s ID number (of type int) name - the player's screen name (of type String) scores - an array of integers that stores the highest score achieved in each of 10 game levels Include get and set methods for each field. The get method for scores should require the game level to retrieve the score for. The set method for scores should require two parameters—one that represents the score achieved and one that represents the game level to be retrieved or assigned. Display an error message if the user attempts to assign or retrieve a score from a level that is out of range for the array of scores. Additionally, no level except the first one should be set unless the user has earned at least 100 points at each previous level. If a user tries to set a score for a level that is not yet available, issue an error message. Create a class named PremiumSugarSmashPlayer that descends from SugarSmashPlayer. This class is instantiated when a user pays $2.99 to have access to 40 additional levels of play. As in the free version of the game, a user cannot set a score for a level unless the user has earned at least 100 points at all previous levels.
SOLUTION-
I have solve the problem in python code with comments and
screenshot for easy understanding :)
CODE-
PremiumSugarSmashPlayer.java
//Create a class PremiumSugarSmashPlayer which extends
//the parent class SugarSmashPlayer
public class PremiumSugarSmashPlayer extends SugarSmashPlayer
{
//instance variables
private int[] scores=new int[40];
private boolean accessLevel;
//Constructor that sets pay value
public PremiumSugarSmashPlayer(double pay)
{
//Check if pay is pay $2.99 to
access all levels
if(pay>=2.99)
{
accessLevel=true;
}
else
{
//Otherwise set accesLevel to false
accessLevel=false;
}
}
//implement the mutator method to set the
score
public void setScore(int score, int gameLevel)
{
//Check if user has
accessLevel
if(accessLevel)
{
if(gameLevel<0 ||gameLevel>scores.length)
{
System.out.println("Invalid Game level");
}
else
{
scores[gameLevel]=score;
}
}
else
{
System.out.println("Pay 2.99 to access levels:\n");
}
}
//Returns score at given game level
public int getScore(int gameLevel)
{
if(gameLevel>=0 &&
gameLevel<scores.length )
{
return
scores[gameLevel];
}
else
{
System.out.println("Invalid Game Level");
return
-1;
}
}
}
SugarSmashPlayer.java
//SugarSmashPlayer.java
public class SugarSmashPlayer
{
//instance variables ID, screenName
private int idNumber;
private String name;
//Create an array of size 10 for 10 levels
private int[] scores=new int[10];
//implement the method to set the score at
gameLevel
public void setScore(int score, int gameLevel)
{
//Define the boolean
variable
boolean
validScores=false;
//Set score at index=0 if
gameLevel is 0
if(gameLevel==0)
{
scores[0]=score;
}
else
{
//check the userscore
for
(int index = 0; index < gameLevel
&& !validScores;
index++)
{
if(scores[index]>100)
{
validScores=true;
}
}
//Set
score to gameLevel
if(validScores)
{
scores[gameLevel]=score;
}
else
{
/*Otherwise set score at index=0 and print
a message invalid score */
scores[0]=score;
System.out.println("Invalid score ");
}
}
}//end of setScore
//Implement method to get the Score
public int getScore(int gameLevel)
{
if(gameLevel>=0 &&
gameLevel<scores.length)
{
return
scores[gameLevel];
}
else
{
System.out.println("Invalid game level");
return
-1;
}
}
//Method to set ID number
public void setID(int ID)
{
this.idNumber=ID;
}
//Method to get ID number
public int getID()
{
return idNumber;
}
//Set screen name
public void setScreenName(String screenName)
{
this.name=screenName;
}
//Returns screen Name
public String getScreenName()
{
return name;
}
}//end of the SugarSmashPlayer
-----------------------------------------------------------------------------------------
DemoSugarSmash.java
//DemoSugarSmash.java
public class DemoSugarSmash
{
public static void main(String[] args)
{
System.out.println("SugarSmashPlayer Class Object1: ");
//Create an instance of
SugarSmashPlayer
SugarSmashPlayer
sugarPlayer=new SugarSmashPlayer();
//set the screen name
sugarPlayer.setScreenName("Black");
//set the
ID
sugarPlayer.setID(1111);
//Set score less than 100 at
game level=0
//Print an error message since
score <100
sugarPlayer.setScore(50,
0);
//Set score less than 100 at
game level=1
//Print an error message since
score <100
sugarPlayer.setScore(70,
1);
//Set score less than 100 at
game level=2
sugarPlayer.setScore(120,
2);
System.out.println("SugarPlayer Name: "
+sugarPlayer.getScreenName());
System.out.println("SugarPlayer ID: "
+sugarPlayer.getID());
System.out.println("Score :
"
+sugarPlayer.getScore(0));
//Other scores are set to
zero
System.out.println("Score :
"
+sugarPlayer.getScore(1));
System.out.println("Score :
"
+sugarPlayer.getScore(2));
System.out.println();
System.out.println("PremiumSugarSmashPlayer Class Object1:
");
PremiumSugarSmashPlayer
premiumPlayer1=
new PremiumSugarSmashPlayer(2.00);
//Set score to level 0
given an error message
premiumPlayer1.setScore(15,
0);
premiumPlayer1.setID(1234);
//set the screen Name
premiumPlayer1.setScreenName("Blue");
System.out.println("PremiumSugarSmashPlayer ID: "
+premiumPlayer1.getID());
System.out.println("PremiumSugarSmashPlayer Name: "
+premiumPlayer1.getScreenName());
System.out.println("PremiumSugarSmashPlayer Score: "
+premiumPlayer1.getScore(0));
System.out.println("\nPremiumSugarSmashPlayer Class
Object2:");
//Create an instance of
PremiumSugarSmashPlayer
//with pay $3 dollars
PremiumSugarSmashPlayer
premiumPlayer=
new PremiumSugarSmashPlayer(3.00);
//Set id for the Premium
player
premiumPlayer.setID(321);
premiumPlayer.setScreenName("Thrones");
//Set scores to any
level
premiumPlayer.setScore(15,
0);
premiumPlayer.setScore(50,
1);
premiumPlayer.setScore(20,
2);
System.out.println("PremiumSugarSmashPlayer ID: "
+premiumPlayer.getID());
System.out.println("PremiumSugarSmashPlayer Name: "
+premiumPlayer.getScreenName());
System.out.println("Premium
User score who pay $3");
System.out.println("Score :
"
+premiumPlayer.getScore(0));
System.out.println("Score :
"
+premiumPlayer.getScore(1));
System.out.println("Score :
"
+premiumPlayer.getScore(2));
}//end of main method
}//end of the class
SCREENSHOT-
IF YOU HAVE ANY DOUBT PLEASE COMMENT DOWN BELOW I WILL
SOLVE IT FOR YOU:)
----------------PLEASE RATE THE ANSWER-----------THANK
YOU!!!!!!!!----------