Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

CS145 - PROGRAMMING ASSIGNMENT CARD ARRAY LIST OVERVIEW This program primarily focuses on the implementation of a ArrayList type interface and the necessary methods to

image text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribed

CS145 - PROGRAMMING ASSIGNMENT CARD ARRAY LIST OVERVIEW This program primarily focuses on the implementation of a ArrayList type interface and the necessary methods to implement the ArrayList. It also includes polymorphism and class comparison. INSTRUCTIONS Your deliverable will be to turn in three files. The files will be named Card.java, PremiumCard.java and the last file will be called CardArrayList.java. For this assignment, any use of a data control structure other than a simple Arrary or String will result in no credit. I am aware that this assignment could be done quite simply by the Collections implementation of an ArrayList > but the point of the assignment is to do it without the standard implementation to get a feel for how they work "under the hood". COLLECTABLE CARD While the primary goal of this assignment will be the implementation and use of a custom ArrayList. The particular implementation we will use will focus on the idea of a simple collectable card game. For our card game, we will reuse the card class that we created in the very first assignment using the exact same details we did when we wrote it. So refer back to that assignment for details on the base Card class. In addition, there is a secondary type of card that are considered Premium cards. If we were using a GUI interface, these would be drawn graphically different on the screen with some sort of special treatment, but for our purposes there will only be a slight difference in the output of the card information. But this will give you some practice in polymorphism. In this assignment you will construct an ArrayList that can manage a set of Cards which includes both normal and premium cards. You will implement the various methods that are required in order to make the ArrayList function. A sample program will be provided for you to test with, but you may want to alter it to check different functionality of your ArrayList. YOUR INSTRUCTIONS You vill vrite the following classes, implementing the necessary methods as appropriate. NECESSARY METHODS - CARD CLASS The Card class will be your primary class to contain one particular card. Look back at the instructions for the first assignment for details. When comparing two cards, the one with the higher cost comes after the other unless they have the same cost. If the two cards have the same cost, then the one with the SMALLER sum of R,P, S values comes first. If the cost is the same, and R+P+S is the same, then they are equal. {1,1,1000:2} " where the first number is the R, the second number is the P, the third number is the S, and the number after the "::" is the calculated cost. Note the different look using the "|" and the ">" values. So instead of: Card \#23987 [2,5,7:129] A PremiumCard should look like : Card \#23987 257=129 NECESSARY METHODS - CARDARRAYLIST CLASS The CardArrayList is the primary focus of this assignment. In it you will maintain a list of cards, allowing for all the methods listed below to manage a list of cards. The basic idea of the CardArrayList will be to keep track of an internal array of cards and a list of how many of the array slots are currently being used. As values are added/removed to the internal array, you will update the "size" field to keep track of how many are being used. Should the user try to add a value to the array that would cause it to overflow, then you will need to implement routines to create an array that is double the size of the current array, then copy the current array into the new array, then replace the old array with the new array. When that is done, then you may complete the original operation that caused the resize. However, we want to be able to change the type of list in the future, so your CardArrayList will need to implement the CardList interface in order to work. So while the CardList interface is provided for you, you will need to make sure that your program properly implements it. In the CardArraylist() constructor should create an initial array of size 10 , and set the internal size counter to zero to represent that none of the spaces are currently being used. PUBLIC CARDARRAYLIST (INT X) In the CardArrayList() constructor should create an initial array of size x, and set the internal size counter to zero to represent that none of the spaces are currently being used. If x is less than one, throw an appropriate exception. PUBLIC STRING TOSTRING () The tostring() method should print the current values of the arraylist being stored. It should surround the entire cardArraylist inside [0: :size] with commas between the values. A sample output might look like. [0:[2,3,4:55],[8,9,10:90],>,[4,5,6:82]:4] Note the 4 to show the current size. If the array is empty it should print out: [0::0] PUBLIC INT SIZE () In the size () method, you should return the current number of elements being stored in the array. Not the size of the array, but how many elements it is currently holding. PUBLIC VOID ADD (CARD X) In the add () method, you should add the card provided to the array list in the last empty location and increment the size counter. Note that this may require a resize before running. PUBLIC CARD REMOVE [] In the remove () method, you should return the last element from the array list. You should then decrement the size counter by one to show that we don't care about that element anymore. You don't actually have to delete it, it effectively gets removed by being ignored. Make sure to return the Card as you exit however so it can be used by the internal program. PUBLIC CARD GET (INT X) In the get (int x ) method, you should return the card located in the array at the x location. This method does not delete the element; it just returns the current value. If x is outside the bounds of the array, throw an appropriate exception. PUBLIC INT INDEXOF (CARD X) In the indexOf () method, you should return the location of the first card that is "equal" to the card that is provided. If it isn't found, return -1. Note that the card found may not match the card given precisely due to the unusual comparison of cards. PUBLIC VOID ADD (INT L, CARD X) In the add (location, x ) method, you should add the card( x) provided to the array list in location x. However because the location is inside the array, you will need to move everything after the location over one spot. So you will need to move everything to make room, then add x into location. Note that this may require a resize before running. Make sure that x is inside the current array OR at the end, do not extend the array more than one value. Make sure to alter the size counter as appropriate. If x is outside the bounds of the array plus one, throw an appropriate exception. PUBLIC CARD REMOVE (INT J) In the remove (int j ) method, you should remove the element from the array list in location j and then return it. However in this method, the item may be in the middle of the array, so you will need to store the item, then move everything after the item to the left one spot, then adjust the size counter. If j is outside the bounds of the array in use, throw an appropriate exception. Make sure to return the Card as you exit however so it can be used by the internal program. Hint: you will need a temp holder to hold the return card while you are readjusting the array. PUBLIC VOID SORT () The sort () method should simply sort the array from smallest to largest. However I want you to implement your own version of the merge sort. Do not use Arrays.sort 0 . Also, keep in mind that you will not be sorting the entire array. Your array might be size 100 but you are only using 60 elements currently. Your merge sort should take into account that you are only sorting a section of an array. This is a hard method, and only worth a couple of points, so spend your time wisely here if you are having problems. Think about making a copy and soring the copy and putting it back.... The shuffle () method should shuffle the array into a non-ordered arrangement. One way of doing this is picking two random numbers within the size of the array, and then swapping those two values. Then repeat this process a bunch of times. (For example five times the number of elements in the arraylist). Also make sure that you are only shuffling the part of the array that you are using, and not the "empty" array elements. Note that this isn't mathematically a good shufle, but it will work for ow pupposes. PRIVATE BOOLEAN ISROOM () A good idea for your class is to create a private isRoom () method that will check to see if adding one more element will require the array to grow. This way you can use this method before accidently adding a value that will cause and overflow. PRIVATE VOID EXPAND() A good idea for your class is to create a private expand () method that will double the size of your array and then copy the old array into the new array for storage. This should not change the value of the size counter, just make it big enough for added storage. PRIVATE VOID SWAP(INT A, INT B) A good idea for your class is to create a private swap(a,b) method that will swap two cards around as necessary. Helpful for methods above. PUBLIC VOID CLEAR() clear 0 method should reset your size and array back to an initial size (10), basically deleting the entire arrayList

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

Advances In Spatial And Temporal Databases 10th International Symposium Sstd 2007 Boston Ma Usa July 2007 Proceedings Lncs 4605

Authors: Dimitris Papadias ,Donghui Zhang ,George Kollios

2007th Edition

3540735399, 978-3540735397

More Books

Students also viewed these Databases questions