Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

The shell of the Singleton Pattern code is provided. 2 of the below 4 classes need to be finished to work correctly according to the

image text in transcribed

The shell of the Singleton Pattern code is provided. 2 of the below 4 classes need to be finished to work correctly according to the instructions in the image above.

import java.util.*;

public class AssembleParty implements Runnable { //This is a necessary component of testing the code. //For simplicity, it has been provided to you in a complete state. public void run() { System.out.println("Beginning this thread... "); //Creates the only instance of SingletonDM, and prints the identity. SingletonDM singleton = SingletonDM.getInstance(); System.out.println("ID of current instance: " + System.identityHashCode(singleton));

//Gets and prints all of the character sheets. LinkedList gameSheets = singleton.getSheetList(); System.out.println("All sheets: " + gameSheets + " "); //Get and prints only the level 1 character sheets. LinkedList levelOneGameSheets = singleton.getSheetsOfLevel(1); System.out.println("Only level 1 sheets: " + levelOneGameSheets + " "); //Get and prints only the wizard character sheets. LinkedList wizardGameSheets = singleton.getSheetsOfType("Wizard"); System.out.println("Only wizards: " + wizardGameSheets + " "); //Confirms that the process is done. System.out.println("Character sheets retrieved! "); } }

import java.util.*;

public class PlayerCharacter { //This class shall be used to represent Player Characters as data. //D&D aficionados will note the bare nature of this class and the paucity of given stats. //However, for our purposes, the given information will work just fine. //For consistency and ease of coding, this class was completed for you. //In the immortal words of the Bluth family, that was a freebie. //For our purposes, these values will represent all possible characters. private int[] baseStatScores; private int level; private String type; //I was gonna call these by their proper name, "classes", but that word is special in java. static String[] types = { "Barbarian", "Bard", "Cleric", "Druid", "Fighter", "Monk", "Paladin", "Ranger", "Rogue", "Sorcerer", "Warlock", "Wizard" }; //Constructs a PlayerCharacter of specific level and type. public PlayerCharacter(int lev, int ty) { baseStatScores = rollBaseStats(); level = lev; type = types[ty%12]; } //Constructs a PlayerCharacter of specific level and type. public PlayerCharacter(int lev, String ty) { baseStatScores = rollBaseStats(); level = lev; type = ty; } //Constructs an entirely random character. public PlayerCharacter() { baseStatScores = rollBaseStats(); level = (int)Math.ceil(Math.random() * 20); type = types[(int)Math.floor(Math.random() * 12)]; } //Gets this character's level. public int getLevel() { return level; } //Sets this character's level. public void setLevel(int lev) { level = lev; } //Gets this character's type. public String gettype() { return type; } //Sets this character's type. public void settype(int ty) { type = types[ty]; } //Sets this character's type. public void settype(String ty) { type = ty; }

//Gets this character's stats. public int[] getBaseStats() { return baseStatScores; } //Sets this character's stats. //To prevent creating broken characters, you cannot directly set the stats. public void setBaseStats() { baseStatScores = rollBaseStats(); } //Randomizes the character's level. public void randomizeLevel() { level = (int)Math.ceil(Math.random() * 20); } //Randomizes the character's type. public void randomizeType() { type = types[(int)Math.floor(Math.random() * 12)]; } //Rolls character stats. private int[] rollBaseStats() { int stats[] = new int[6]; for(int i = 0; i

for(int j = 0; j

sum -= min; stats[i] = sum; } return stats; } //Gives an easy way to print the character. //This way, you can just call System.out.println(x), where x is a PlayerCharacter object. public String toString() { return "Level " + level + " " + type; } }

import java.util.*;

public class SingletonDM { //Here, put the specific private static variable that makes this class follow the singleton pattern. //The array of randomized character sheets. //Feel free to hardcode a few of these for your testing. private PlayerCharacter sheets[] = { new PlayerCharacter(), new PlayerCharacter(), new PlayerCharacter(), new PlayerCharacter(), new PlayerCharacter(), new PlayerCharacter(), new PlayerCharacter(), new PlayerCharacter(), new PlayerCharacter(), new PlayerCharacter(), new PlayerCharacter(), new PlayerCharacter(), new PlayerCharacter(), new PlayerCharacter(), new PlayerCharacter(), new PlayerCharacter(), new PlayerCharacter(), new PlayerCharacter(), new PlayerCharacter(), new PlayerCharacter(), new PlayerCharacter() }; //The sheets array remade as a list for convenience. private LinkedList sheetList = new LinkedList(Arrays.asList(sheets)); //Here, put the specific kind of constructor that makes this class follow the singleton pattern. public static SingletonDM getInstance() { //This will act as the "true" constructor for this class. //Its details should include but not be limited to the following: // - Check if this is the first thread. // - Check the private static variable at the top of the class. // - Have the "synchronized" key word in there somewhere. // - Return some kind of SingletonDM } public String GetNameAndPID() { return( "Last, First, PID"); //Please replace Last, First, and PID with your relevant details. //This function will be called in main. } public LinkedList getSheetList() { //Pretty self-explanatory. } public LinkedList getSheetsOfLevel(int level) { //This should find all characters of a certain level in the list, and return them in a separate list. //Note: do not remove these characters from the list itself! //Just find them and put them in their own list, then return that list. } public LinkedList getSheetsOfType(String type) { //This should find all characters of a certain type in the list, and return them in a separate list. //Note: do not remove these characters from the list itself! //Just find them and put them in their own list, then return that list. } }

import java.util.*;

public class TestSingletonDM { public static void main(String args[]) { //This main function will be used to test your program. //Feel free to use it to test your code, but leave this blank when you turn your program in. //Some things you might want to test: //Creating two or more instances of AssembleParty, and starting their threads one after the other //Creating two or more instances of SingletonDM and comparing their IDs with System.identityHashCode() //Creating two or more instances of SingletonDM and comparing their sheet lists //Creating two or more instances of SingletonDM and getting sheets of only a certain type or level }

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Administration The Complete Guide To Dba Practices And Procedures

Authors: Craig S. Mullins

2nd Edition

0321822943, 978-0321822949

More Books

Students also viewed these Databases questions

Question

Explain the concept of shear force and bending moment in beams.

Answered: 1 week ago

Question

What do Dimensions represent in OLAP Cubes?

Answered: 1 week ago