Question: JAVA programming PART - II: Information Parsing with Stacks (35 points) In PART-II, you will decode and parse strings that contain compressed information. [7 points]
JAVA programming


![points] Write a Position class as a representation of a point (x,y)](https://dsd5zvtm8ll6.cloudfront.net/si.experts.images/questions/2024/09/66f45f469bd8e_20666f45f461ef5c.jpg)

PART - II: Information Parsing with Stacks (35 points) In PART-II, you will decode and parse strings that contain compressed information. [7 points] Write a Position class as a representation of a point (x,y) on a 2D map as the figure below. The origin (0,0) is located at the top-left corner of the map. The Position class has the following private fields: - An int indicating the x-coordinate on the map - An int indicating the y-coordinate on the map The Position class must also have the following public methods: - A constructor that takes two int as inputs indicating the x - and y-coordinates on a map, in this order. The constructor uses the inputs to initialize the corresponding fields. - A constructor that takes a Position as input and initializes the fields with the corresponding input. This type of constructors are called copy constructors. They are used to create duplicates of existing instances of the class. - A reset method that takes two int as inputs indicating the x - and y-coordinates on a map. The method uses the inputs to resets the corresponding fields. - A reset method that takes Position as input and resets the corresponding fields using the values of those of the input object. - A static method called getDistance that takes two Position objects as input and returns the absolute distance between them in terms of the x - and y-coordinates. For example, the distance between two Position objects (2,3) and (3,5) is 3 (i.e. 23 +35). - A get X and a getY method that return the values of the corresponding fields. - A moveWest method that decrements the x-coordinate by 1. - A moveEast method that increments the x-coordinate by 1 . - A moveNorth method that decrements the y-coordinate by 1. - A moveSouth method that increments the y-coordinate by 1. - An equals method which takes as input an Dbject and returns true if the input matches this in type, x-coordinate, and y-coordinate. Otherwise, the method returns false. [13 points] Write a TargetQueue class that extends from MyQueue . The TargetQueue class is designed to store a list of Position objects. The TargetQueue can parse a string and store the information as a queue. For example, given a string " (1,2),(3,4)(5,6). ", the input should be parsed into a queue of 3 positions shown in the figure below: Figure 1: A queue of positions after parsing " (1,2)(3,4)(5,6) " Note that, the input string has this strict format (x,y). The x - and y-coordinates are enclosed by a left and a right parenthesis separated by a comma, and two positions must be separated by a period. The TargetQueue class has the following private fields: - A MyStack to help with parsing a string containing information about the positions. The Targetqueue class must also have the following public methods: - A constructor that takes no input. The constructor should invoke the constructor of the superclass and initialize its fields. - A clear method that clears this queue and its stack field. - An addTargets method that takes input as a String and parses it into a queue of Positions. The method should throw an IllegalArgumentException if the syntax of the input is invalid. It can be useful for this method to have a local string variable to store the numbers contained in the input string. I will refer to this variable as num. To check the validity of the syntax and parse out the information regarding the positions, we need to iterate through every character in the input string. - if the character is a left parenthesis, the stack and num should both be empty. If so, push "(" onto the stack. - if the character is a digit, append the digit to num. - if the character is a comma, check if num is a valid integer. If num is empty, we must. have read a comma before the x-coordinate, which is a syntax error. Otherwise, push num and "," onto the stack, in exactly this order. Finally, reset num. - if the character is a right parenthesis * the stack should contain 3 elements with exactly the order a comma, an integer, and a left parenthesis from the top. If the order is not correct or any of the elements are missing, the input string has a syntax error. 7 * num should be an integer representing the y-coordinate. If num is empty, we must have reached the right parenthesis without finding a y-coordinate, which is a syntax error. * If the syntax is correct, add the xy coordinate as a Position object onto the quene and reset num. - if the character is a period, we must have reached the end of expression for a particular position. If the syntax is correct, the stack and num should both be empty. Otherwise, this is another syntax error. Note that, these are the minimum cases of syntax errors that you should take care of. There might be more edge cases that do not get detected by the instructions above. It would be your job to find them and throw exceptions when needed. [2 points] Write an enumeration Direction. An enumeration in java is a special data type that represents a fixed set of constants. Defining and using enumerations will make your code a bit more readable. The syntax is as follows: Inside a file called Direction. java you should define an enumeration called Direction that represents the directions. The enumeration Direction has four values NORTH, SDUTH, WEST, EAST. [13 points] Write a ActionQueue class that extends Myqueue. The Actionqueue class decodes a string that contains compressed information on directions into a queue of Direction objects. The input strings are encoded in the form K[D] which indicates that the direction(s) D is repeated K times. K should be a positive number. D should contain characters ' N ', ' S ', ' W ', ' E ' that represent directions. Specifically, ' N ' stands for NORTH, ' S ' stands for SDUTH, ' W ' stands for WEST, and 'E' stands for EAST. Table 1 contains some examples of encoded strings and their decoded values: Note that, implementing this class is meant to be a bit more challenging, and hence, the implementation details are omitted. You will need to understand and analyze the task at hand and decide how to go about implementing such class. Table 1: Examples of encoded and decoded directions It is up to you to decide how many, and what type of private fields you might need for this class. Different implementations are possible. Remeber to exploit the properties of stacks to help you parse strings. The Actionqueue class must have the following public methods: - A constructor that takes no input and takes care of initializing whichever field(s) you have added. - A clear method that removes all items from this queue (and possibly clears the field(s)) - A loadFromEncodedString method that takes input as a String of an encoded message and converts it into a queue of Direction. Similar to the TargetQueue, the method should throw an IllegalArgumentException if the syntax of the input is invalid. It is up to you to figure out when and where to throw exception, or otherwise to translate the string into the corresponding queue of Directions. I suggest you start by focusing on how to correctly parse very simple strings like 3[N]. Then move onto correctly parsing sequences of simple strings like 2[N]3[W] or more complicated strings such as 3[NE]. Finally, try to solve the problem of parsing nested strings such as 3[2[N]2[E]]. Please do not focus on getting this part to work to perfection before moving onto Part III of the assignment. You do not need this to be working to complete what comes next. In fact, you can still get a very high mark even if this part is only partially working. Make sure to use your time wisely
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
