Question

In: Computer Science

JAVA - Programming Exercise 10-5. The developers of a free online game named Sugar Smash have...

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.

Solutions

Expert Solution

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!!!!!!!!----------


Related Solutions

The developers of a new online game have determined from preliminary testing that the scores of...
The developers of a new online game have determined from preliminary testing that the scores of players on the first level of the game can be modelled satisfactorily by a Normal distribution with a mean of 185 points and a standard deviation of 28 points. They would like to vary the difficulty of the second level in this game, depending on the player’s score in the first level. (a) The developers have decided to provide different versions of the second...
The developers of a new online game have determined from preliminary testing that the scores of...
The developers of a new online game have determined from preliminary testing that the scores of players on the first level of the game can be modelled satisfactorily by a Normal distribution with a mean of 185 points and a standard deviation of 28 points. They would like to vary the difficulty of the second level in this game, depending on the player’s score in the first level. (a) The developers have decided to provide different versions of the second...
Nim Game Java, PegClass One variation of the game of Nim, described in Chapter 5 Exercise...
Nim Game Java, PegClass One variation of the game of Nim, described in Chapter 5 Exercise 12, is played with four piles of stones. Initially, the piles contain 1, 2, 3, and 4 stones. On each turn, a player may take 1, 2, or 3 stones from a single pile. The player who takes the last stone loses. a) Write a program that allows two players to play Nim. Be sure to allow only legal moves. The program should be...
JAVA PROGRAMMING. In this assignment, you are to create a class named Payroll. In the class,...
JAVA PROGRAMMING. In this assignment, you are to create a class named Payroll. In the class, you are to have the following data members: name: String (5 pts) id: String   (5 pts) hours: int   (5 pts) rate: double (5 pts) private members (5 pts) You are to create no-arg and parameterized constructors and the appropriate setters(accessors) and getters (mutators). (20 pts) The class definition should also handle the following exceptions: An employee name should not be empty, otherwise an exception...
THIS IS JAVA PROGRAMMING 1. Create a class named Name that contains the following: • A...
THIS IS JAVA PROGRAMMING 1. Create a class named Name that contains the following: • A private String to represent the first name. • A private String to represent the last name. • A public constructor that accepts two values and assigns them to the above properties. • Public methods named getProperty (e.g. getFirstName) to return the value of the property. • Public methods named setProperty ( e.g. setFirstName)to assign values to each property by using a single argument passed...
THIS QUESTION IS BASED UPON JAVA PROGRAMMING. Exercise 1 In this exercise, you will add a...
THIS QUESTION IS BASED UPON JAVA PROGRAMMING. Exercise 1 In this exercise, you will add a method swapNodes to SinglyLinkedList class. This method should swap two nodes node1 and node2 (and not just their contents) given references only to node1 and node2. The new method should check if node1 and node2 are the same nodes, etc. Write the main method to test the swapNodes method. Hint: You may need to traverse the list. Exercise 2 In this exercise, you will...
Put In Java Programming The TicketMachine class: Design a class named TicketMachine that contains: • A...
Put In Java Programming The TicketMachine class: Design a class named TicketMachine that contains: • A double data field named price (for the price of a ticket from this machine). • A double data field named balance (for the amount of money entered by a customer). • A double data field named total (for total amount of money collected by the machine). • A constructor that creates a TicketMachine with all the three fields initialized to some values. • A...
PUT IN JAVA PROGRAMMING The Stock class: Design a class named Stock that contains: • A...
PUT IN JAVA PROGRAMMING The Stock class: Design a class named Stock that contains: • A string data field named symbol1 for the stock’s symbol. • A string data field named name for the stock’s name. • A double data field named previousClosingPrice that stores the stock price for the previous day. • A double data field named currentPrice that stores the stock price for the current time. • A constructor that creates a stock with the specified symbol and...
write on eclipse java Write a program named lab5 that will play a game of Blackjack...
write on eclipse java Write a program named lab5 that will play a game of Blackjack between the user and the computer. Create a second class named Card with the following: Define the following private instance variables: cardValue (int) & cardSuite (String) Write a constructor with no parameters that will Set cardValue to a random number between 1 and 13 Generate a second random number between 0 and 3 and assign cardSuite a string based on its value (0 –...
Exercise 1. Write a function named printBox to print a 5 x 10 box of asterisks...
Exercise 1. Write a function named printBox to print a 5 x 10 box of asterisks using nested loops. When printBox() is called your function should display the pattern shown below. Write a function named printBox to print a 5 x 10 box of asterisks using nested loops. When printBox() is called your function should display the following information. ********** ********** ********** ********** ********** """ # place the code for the printBox function here printBox( numRows, numCols ) Exercise 2....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT