CS 0445 Summer 2023 Assignment 1

Online: Wednesday, May 17, 2023 Due: All source files plus a completed Assignment Information Sheet zipped into a single .zip file and submitted properly to the submission site by 11:59PM on Saturday, May 27, 2023 (Note: see the submission information page for submission details) Late Due Date: 11:59PM on Tuesday, May 30, 2023 Purpose: To refresh your Java programming skills and to emphasize the object-oriented programming approach used in Java. Specifically, you will work with control structures, class-building, interfaces and generics to create and utilize a simple array-based data structure. Goal 1: To design and implement a simple class MultiDS that will act as a simple data structure for accessing Java Objects. Your MultiDS class will primarily implement 2 interfaces – MyQ and Shufflable. The details of these interfaces are explained in the files MyQ.java and Shufflable.java. Read these files over very carefully before implementing your MultiDS class. Goal 2: To utilize your MultiDS class by implementing a simple version of the card game "War". In this case your program will be a client using MultiDS and the details of the MultiDS implementation will be abstracted out. Details 1: For the details on the functionality of your MultiDS class, carefully read over the files MyQ.java, QueueInterface.java, Shufflable.java and Assig1A.java provided on the Web site. You must use these files as specified and cannot remove/alter any of the code that is already written in them. In Lecture 3 we discussed the author's QueueInterface, which is an ADT that allows for adding at the logical back (enqueue) and removing from the logical front (dequeue) of the data structure. The methods in this interface are specified in file QueueInterface.java. See the Lecture 3 Powerpoint presentation for some background and ideas about the QueueInterface. In Recitation Exercise 2 you implemented (or will implement) two simple classes called PrimQ1 and PrimQ2 that satisfy QueueInterface but in an inefficient way. Specifically, they use an array that maintains one logical end or the other of the queue at index 0 and thus requires shifting for one of the enqueue() or dequeue() operations. We will discuss specific run-time analysis of these implementations a bit later in the course, but it is intuitive that there is a lot of overhead in both of these implementations. A queue can be implemented in a more efficient way with an array if we allow both logical sides of the queue to move along the array in a circular fashion. For example, consider the array below: 0 1 2 3 4 5 6 7 10 20 30 40 50 front back In this queue, both front and back will move forward within the array as we enqueue() or dequeue() in the queue. For example, if we enqueue(55) in this queue, it will look as follows: 0 1 2 3 4 5 6 7 10 20 30 40 50 55 front back If we then dequeue(), the queue will look as follows: 0 1 2 3 4 5 6 7 20 30 40 50 55 front back Note that for this approach to work effectively, both back and front will need to "wrap" around the end of the array when necessary. This enables the beginning indices in the array to be reused. For example, if we now enqueue(66), the array will appear as follows: 0 1 2 3 4 5 6 7 66 20 30 40 50 55 back front Your MultiDS class must implement the queue using this circular approach. Note that when the queue is implemented in this way, we do not have to shift data in the array and either of the enqueue() or dequeue() methods can be implemented with just a few statements. However, there are some special cases to consider (ex: detecting a full array, handing an empty queue) so think carefully about how you would implement your class. The MyQ interface extends QueueInterface, adding 2 new, simple methods (size() and capacity()). Your MultiDS must implement MyQ as well as the Shufflable interface. Shufflable has just one method, shuffle(), which will permute the data within the collection in a pseudo-random way. Thus, your class header should be: public class MultiDS implements MyQ, Shufflable Important Note: The primary data within your MultiDS class must be an array. You may not use any predefined Java collection class for your MultiDS data. In addition to the interface methods, you will also need a constructor and a toString() method for your MultiDS class. See file Assig1A.java for details on these methods. Your MultiDS class must also be able to dynamically resize when necessary. As we discussed (or will discuss) in Lecture 4, array-based data structures can be logically resized by allocating new, larger arrays and copying the data from the old array into the new array. This can be done in your MultiDS class but, due to its circular nature, you must be careful when doing this to preserve the queue order. Think about how you would resize for this class. Note that after resizing (but before doing anything else) the capacity() of your MultiDS should double but the size() should be unchanged. After you have finished your coding of MultiDS, the Assig1A.java file provided for you should compile and run correctly, and should give output identical to the output shown in the sample executions (except for the segments where the data is shuffled, since it will be pseudo-random in that case). Details 2: War is a card game played often by children that has many variations. You will implement the simple version as described below: • Initially shuffle a 52-card standard deck of cards • Deal the cards out completely to two players, alternating cards to each player • Do the following until one player is out of cards or until a set number of rounds have completed: • Each player plays the top card in their hand • If one player's card beats the other (has a higher rank – suits don't matter), that player puts both cards into their discard pile • If the players tie, it is a WAR, so do the following until the WAR is resolved: • Each player plays a card without comparing (in the physical game this would be placing a card “face down” on the table). • Each player plays one more card and compares in the same way as above • The winner of the WAR takes all played cards (initially compared cards, uncompared cards, second compared cards) For solution whatsapp +923325198690

No comments: