Question: Today we will practice using List objects. 1 Returning a List Let us write a function that takes a simple array of integer as parameter,
Today we will practice using List objects. 1 Returning a List Let us write a function that takes a simple array of integer as parameter, and returns all the numbers that are negative. public static void main(String[] args) { // should print -5 and -3 for(int x : getNegatives(-5, 3, 7, -3, 78)) { System.out.println(x); } } public static List getNegatives(int... numbers) { List negatives = new LinkedList<>(); // TODO return negatives; } 1.1 Reminder Remember that the parameter "int... numbers" means "int [] numbers" except that it can be called with a variable number of parameter. Im using the enhanced for loop in the main to print the negative numbers. 2 Dealing Cards Lets write a main() that builds a deck of cards, shuffles it, and then deals each of the 2 player 3 cards. Use the code in LabList.java and write the loop that builds the deck. public static void main(String[] args) { List deck = new ArrayList<>(); List hand1 = new ArrayList<>(); List hand2 = new ArrayList<>(); // Text Version String[] suits = new String[] { " of Spades", " of Hearts", " of Diamonds", " of Clubs" }; String[] numbers = new String[] { "Ace", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King" }; // TODO initialize the deck (use a nested loop on suits and numbers to add the 52 cards to the deck list) // shuffle Collections.shuffle(deck); System.out.println("Cards in deck: " + deck.size()); // deal for (int i = 0; i < 3; i++) { hand1.add(deck.remove(0)); hand2.add(deck.remove(0)); } // print sizes System.out.println("Cards in deck: " + deck.size()); 1 System.out.println("Cards in hand 1: " + hand1.size()); System.out.println("Cards in hand 2: " + hand2.size()); // print hands System.out.println("Hand 1"); for (String card : hand1) { System.out.println(card); } System.out.println("Hand 2"); for (String card : hand2) { System.out.println(card); } } import java.util.ArrayList; import java.util.Collections; import java.util.LinkedList; import java.util.List; public class LabList { public static void main(String[] args) { List deck = new ArrayList<>(); List hand1 = new ArrayList<>(); List hand2 = new ArrayList<>(); // Text Version //String[] suits = new String[] { " of Spades", " of Hearts", " of Diamonds", " of Clubs" }; //String[] numbers = new String[] { "Ace", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King" }; // Symbol Version String[] suits = new String[] { " ", "", "", "" }; String[] numbers = new String[] { "A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K" }; // TODO initialize the deck (use a nested loop on suits and numbers to add the 52 cards to the deck list) // shuffle Collections.shuffle(deck); System.out.println("Cards in deck: " + deck.size()); // deal for (int i = 0; i < 3; i++) { hand1.add(deck.remove(0)); hand2.add(deck.remove(0)); } // print sizes System.out.println("Cards in deck: " + deck.size()); System.out.println("Cards in hand 1: " + hand1.size()); System.out.println("Cards in hand 2: " + hand2.size()); // print hands System.out.println("Hand 1"); for (String card : hand1) { System.out.println(card); } System.out.println("Hand 2"); for (String card : hand2) { System.out.println(card); } } } Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
