• Ensure that your code compiles with NO syntax errors • Test code in ALL classe
ID: 3740872 • Letter: #
Question
• Ensure that your code compiles with NO syntax errors
• Test code in ALL classes to ensure NO logic or runtime errors
• Use Javadoc documentation standards (comments & tags) and good programming style (10) • Zip the folder containing the code and submit on Blackboard.
Suppose each baseball card collection has a collector with a group of cards. Each card is for a certain year of a particular baseball player. A new project consists of the following:
• a class named CardCollection containing: – a String field named collectorName
– a field named cards with an ArrayList of Card elements • another class named Card containing:
– a String field named player – an int field named year
Create a class named Card to store the details of a card
Declare 2 fields for the details of the card:
• Field named player of type String • Field named year of type int
Write a constructor with 2 parameters for inputs player and year
Use internal method call to setDetails to initialize the fields
Add accessor method getPlayer to return the player field
Add accessor method getYear to return the year field
AddaccessormethodgetDetailstoreturnaStringintheformat“()”where & are actual values: e.g., “Mickey Mantle (1952)”.
7. Add mutator method setDetails (called from constructor) that takes 2 parameters named player & year and initializes both player and year fields to these parameters of the same name. HINT: Use this. to help di erentiate the name overloading.
Create a class named CardCollection to store the cards
Import required Java package for ONLY the ArrayList library class
Declare 2 fields for the details of the card:
• Field named collectorName of type String
• Field named cards of type ArrayList of Card
Write a constructor with 1 input parameter for the collectorName • Initialize the collectorName field to the input parameter
• Create a new object to instantiate the cards field
AddmethodgetNumberOfCardswithnoparametersandreturnsthenumberofcardsinthecards
ArrayList (requires ONLY 1 line of code)
Add method indexValid that takes an index parameter and returns a boolean value depending on
if index is valid in the cards ArrayList. Also:
if negative index value, prints “Index cannot be negative: ”
else if index is more than the biggest index value of the collection, prints “Index is too large: ”
otherwise the index is valid, so no printing is necessary
such that should be replaced by the index parameter value.
Add method addCard with Card card parameter to add a card object to the cards ArrayList collection:
if card is not null, then .add the card to the cards ArrayList collection and print “Added card: ” (use .getDetails to get a String to replace )
else, print “Invalid card can not be added”
Add method removeCard with int index parameter to remove the card at index from the cards
ArrayList collection:
if indexValid(index) returns a true value, then .remove card at index from cards ArrayList and print “Removed card: ”. HINT: The return of .remove is the actual element that was removed and may be used with a call to .getDetails to print the card details
else print “NO card to remove at index: ”
Add method listCard with int index parameter to print the details of the card at index in cards
ArrayList in format “Card : ”:
• indexValid(index) returns a true value, print details of card at index (use .getDetails) • else print “Invalid index: ”
AddmethodlistAllCardswithnoparameterstoprintALLthecardsfromthecardsArrayList(in sequence):
if there is at least 1 card in cards, then print the heading “Card listing:” on its own line followed by ALL the cards (each one per line). HINT: Use a for-each loop to access each card in cards and then use an external method call to getDetails to print each card’s details
else there are NO cards, so print “There are NO cards to print”.
AddmethodlistByPlayerwithStringplayerparametertosearchentirecardsArrayListandprint
ALL cards containing the player String:
Use a local variable named found initialized to false
Use for-each loop to access each card in cards one at a time
– Use external method call to getPlayer to get the player info for the card HINT: You may not need a local variable to store this
– iftheplayercardinfo.containstheplayersearchingfor,thenprintthecarddetailsusing an external method call to getDetails and set found to true (indicating at least 1 card was found)
(Only after ENTIRE search loop is completed - thus, outside of loop) if no match is found, print “NO cards found for player: ”
Add method listByYear with int year parameter to search entire cards ArrayList and print ALL cards matching the year searching for:
Use a local variable named found initialized to false
Use for-each loop to access each card in cards one at a time
– Use external method call to getYear to get the year for the card. HINT: You may not need a local variable to store this.
– if the year on the card is the same year searching for, then print the card details using an external method call to getDetails and set found to true (indicating at least 1 card was found)
(Only after ENTIRE search loop is completed - thus, outside of loop) if no match is found using the year, print “NO cards found for year: ”
AddmethodfindFirstOfPlayerwithStringsearchStringparametertosearchthecardsArrayList and return the index of ONLY the FIRST card found containing the given searchString parameter:
Use a local variable named index initialized to 0
Use a local variable named searching initialized to true
LoopwhilestillsearchingANDindex<.size(searchplayerfieldofeachcardincardsuntil a match is found or all cards searched):
– May (or may not) use a local variable to .get each card and then .getPlayer String for that card
– if the player String for that card .contains the searchString, set searching to false (in- dicating card found at location index)
– else the player has not been found yet, so increment index by 1 (thus, continuing with another loop iteration onto the next card)
• (Only after ENTIRE search loop is completed - thus, outside of loop)
– if still searching (is true) then no card matching searchString was found, so print “NO player card for: ”
– else there was a card found, print the formatted String: “Player card for: at index: ” HINT: If found, then looping should end with the looping variable index holding the value of the index location in cards of where the actual card matching the searchString was found
• Make sure that you return the index of the first card found, or an out-of-bounds index value (i.e. –1) if there is no card found at all
Explanation / Answer
Code is exactly according to the question so comments inline were not necessary. Also there is no sample output since there is no main method specified. However the code compiles aand runs fine.
File Card.java
Thanks
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.