Today you are going to use a LinkedList to build a deck of cards. The linked lis
ID: 3915615 • Letter: T
Question
Today you are going to use a LinkedList to build a deck of cards. The linked list cardDeck has been declared for you. Now you must fill the card deck with cards as described below Details Download the ZIP files named FilesNeeded.zip for use in IntelliJ. When you are happy with your solution, upload the files into the src folder and run. You are going to finish off PoD.java to finish building the card deck. Input Input consists first of an integer number of moves needed to build the card deck (read in for you). Following that, moves will be of the form below I. If input is of the form TOP,9 Spades. insert Spades on the top of the deck 2. If input is of the form: BOTTOM,5 Diamonds. insert "5 Diamonds at the bottom of the deck 3. If input is of the form TAKE, top or TAKE,bottom, then remove a card from the top or bottom, respectively, of the deck. Processing For each line of input, you should add cards to the card deck as described Output Output will be handled for you and will display the contents of the card deckExplanation / Answer
Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts. Thanks
// PoD.java
import java.util.*;
public class PoD {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
LinkedList<String> cardDeck = new LinkedList<String>();
// START WORK HERE ****************************************************
/*
* Build a card deck by placing cards based on NUMBER_OF_MOVES lines of
* input: 1. If input is of the form: TOP,9 Spades insert "9 Spades" on
* the top of the deck 2. If input is of the form: BOTTOM,5 Diamonds
* insert "5 Diamonds" at the bottom of the deck 3. If input is of the
* form "TAKE,top" or "TAKE,bottom" then remove a card from the top or
* bottom, respectively, of the deck.
*/
final int NUMBER_OF_MOVES = Integer.parseInt(in.nextLine());
/**
* looping for NUMBER_OF_MOVES number of times
*/
for (int i = 0; i < NUMBER_OF_MOVES; i++) {
/**
* getting input
*/
String command = in.nextLine();
/**
* splitting input by comma, assuming input is valid
*/
String fields[] = command.split(",");
if (fields[0].equalsIgnoreCase("TOP")) {
/**
* Adding to the first position
*/
cardDeck.add(0,fields[1]);
} else if (fields[0].equalsIgnoreCase("BOTTOM")) {
/**
* Adding to the last position
*/
cardDeck.add(fields[1]);
} else if (fields[0].equalsIgnoreCase("TAKE")) {
if (fields[1].equalsIgnoreCase("top") && !cardDeck.isEmpty()) {
/**
* removing top element / element at first index
*/
cardDeck.remove(0);
} else if (fields[1].equalsIgnoreCase("bottom")
&& !cardDeck.isEmpty()) {
/**
* removing bottom element / element at last index
*/
cardDeck.remove(cardDeck.size() - 1);
}
}
}
// END WORK HERE ****************************************************
System.out.println(cardDeck);
in.close();
System.out.print("END OF OUTPUT");
}
}
/*OUTPUT*/
10
TOP,5 Spades
BOTTOM,9 Clubs
TOP,4 Spades
BOTTOM,10 Clubs
TOP,3 Spades
TAKE,bottom
TAKE,top
TOP,8 Diamonds
TAKE,bottom
BOTTOM,3 Diamonds
[8 Diamonds, 4 Spades, 5 Spades, 3 Diamonds]
END OF OUTPUT