Program Design - Hazeman99/ConcurrrentProgramming GitHub Wiki

Flowchart

Activity Diagram

Class Diagram

In the main class

1) Pseudocode

FUNCTION main 
	Print welcoming message
	Prompt the user to enter n, t, m.
	WHILE t > n
		Prompt user to renter t which is smaller than n
	END WHILE
	Create Game object 
	Call Game.startGame and pass”n” to it
	Create thread pool and pass number of threads “t” to it
	Create hashMap “threadsStore” to store threads
	Submit t number of tasks to ExecutorService
END FUNCTION

FUNCTION printStatus (Game game, HashMap threadStore)
	FOR EACH thread in the threadStore
		Print the thread current status
	END FOREACH
END FUNCTION
FUNCTION readInt (String msg)
	Initialise boolean variable “error” and assign it to false
	Initialise int variable “x” and assign it to 0
	REPEAT 
		TRY
			Print msg
			Get user input and store it in “x”
			IF 0 >= x
"Please enter a positive number only"
				Error = true
			ELSE
				Error = false

			END IF
		END TRY
		CATCH exception 
			Print "Please enter a positive number only"
		END CATCH
	UNTIL error = false
	Return x

2) Code Description The program starts with importing the util and the concurrent packages. Then in the main method: we printed a welcoming message "Welcome to the connection game!". After that, we initialized the variables n,m and t with the user input by invoking the readInt method which will take the input from the user and will validate it if it’s a positive integer or not.”n” variable corresponds to number of random points, “m” to number of seconds while “t” to number of threads. We then initialized a game object “game” and call the startGame method on it by passing “n” to it. The next step was to create a thread pool using the ExecutorService service class by creating an object “executor” and pass “t” to it. We then created a hashSet to store the threads where the value of the hash set is the current thread status. A for loop is created to Submit t number of tasks to ExecutorService.

In the printStatus method: We created a foreach loop to print the status of each thread in the hashSet.

In the readInt method: We get a String “msg” as an argument. WE initialize a boolean variable error to false and int variable to 0. In a do while loop we print the msg, take the user input and check whether the number entered by the user is greater than zero. If it is not, then error is set to true and a message "Please enter a positive number only" will be printed. If it is lower than or equal to 0, then error is set to true. The loop will stop when error is false. Finally, the user input “x” is returned.

In the edges class

1) Pseudocode

Initialize firstPoint
Initialize secondPoint

FUNCTION Edge (Point first, Point second)
	firstPoint = first
	secondPoint = second
END FUNCTION

FUNCTION toString
Print firstPoint and secondPoint
END FUNCTION

2) Code Description The class main goal is to store form an edge by passing the first and second point of the edge to its constructor. Then a toString method is used to present the edge with the 2 points.

In the threadStatus class

1) Pseudocode

In the threadStatus
Declare threadName
Declare statusCode
Declare completedTasksCount and assign it to 0
Declare failuresCount and assign it to 0

FUNCTION getThreadName
	Return threadName
END FUNCTION

FUNCTION setThreadName (String threadName)
	this.threadName = threadName
END FUNCTION


FUNCTION getFailuresCount
	return failuresCount
END FUNCTION

FUNCTION increaseFailuresCountByOne()
	this.failuresCount++
END FUNCTION

FUNCTION getStatusCode
	return statusCode
END FUNCTION

FUNCTION setStatusCode (String statusCode)
	this.statusCode = statusCode;
END FUNCTION

FUNCTION getCompletedTasksCount
	return completedTasksCount
END FUNCTION

FUNCTION increaseCompletedTaskCountByOne
	this.completedTasksCount++;
END FUNCTION

FUNCTION toString
	return a string containing threadName, statusCode, completedTasksCount and failuresCount
END FUNCTION

2) Code Description We declared 2 string variables "threadName" and "statusCode". Another 2 integer variables "completedTasksCount" and "failuresCount" are declared and assigned value of 0. Then we created getters methods for all variables, setters methods for "threadName" and "statusCode" along with "increaseFailuresCountByOne" and "increaseCompletedTaskCountByOne" methods. The last method in this class is toString which returns a string representation of all variables.

In the Game class

1) Pseudocode

FUNCTION startGame
Declare double variables called x,y
WHILE number of points < n
	Generate the first random float number and assign it to x
	Generate the second random float number and assign it to y
	Form a point with x and y coordinates
	IF point doesn’t exist in the ArrayList
		Add the point to the ArrayList
	END IF
END WHILE
END FUNCTION

FUNCTION getRandomPoint
	return random point from points ArrayList
	Remove point from ArrayList
END FUNCTION

FUNCTION  pushNewEdges
 	Add edge to edges ArrayList
END FUNCTION

FUNCTION  getEdgesList
 	Return edges ArrayList
END FUNCTION

FUNCTION  getPointstList
 	Return points ArrayList
END FUNCTION

2) Code Description The game class starts with importing the arraylist and random packages In the game class, two arraylists are created. One to store all the generated points and the other to store the created edges. Then we initialized the random class. After that we declared the two arraylist in the constructor class. The start game method will take the number of points as its argument, then the coordinates x and y are declared as double variables.

In the while loop, while the size of the points arraylist is less than the number of points entered by the user, meaning that we did not finish generating the number of points required. Two random float numbers in the (1000 x 1000) region will be generated and assigned to the x and y coordinates. We will then initialize a point object with the x and y coordinates and check if the points arraylist contains this point, if not the point will be added to the points arraylist.

The getRandomPoint method returns a random point from the points arraylist then remove it from the array. This method will be used by the connecting edges class to create an edge by getting two random points from the points arraylist.

The push pushNewEdges method will add edges to the arraylist. The getEdgesList method will return the edges arraylist, while the getPointstList method will return the points arraylist.

In the Point class

1) Pseudocode

Declare x_coordinate
Declare y_coordinate 
FUNCTION Point (double x, double y)
x_coordinate = x;        
y_coordinate = y;
END FUNCTION

FUNCTION toString
	Print x_coordinate and y_coordinate
END FUNCTION

2) Code Description This class is used to form the point object that represents the coordinates by passing the x and y coordinates to the point constructor. The toString method prints the x and y coordinates.