×
Reviews 4.9/5 Order Now

Create A Program to Find Cards in A Deck in Java Assignment Solution

June 26, 2024
Ellie Icely
Ellie Icely
🇬🇧 United Kingdom
Java
PhD in Computer Science from the University of Hertfordshire, with 8 years of experience in Java assignments. Expert in delivering high-quality, efficient solutions for complex programming problems. Passionate about teaching and mentoring students.
Key Topics
  • Instructions
    • Objective
  • Requirements and Specifications
Tip of the day
Understand the problem first, then choose the most suitable data structure—whether it's arrays, linked lists, stacks, queues, or trees. Practice drawing diagrams to visualize how data flows and test edge cases thoroughly to ensure your implementation handles all scenarios efficiently.
News
In early 2025, Intel’s Cryptography Primitives Library 2025.1.0 launched with optimized AES, RSA, and post-quantum algorithms (XMSS/LMS), making it ideal for students tackling assignments on secure communications

Instructions

Objective

If you require assistance with a Java assignment, here's a program that you might find helpful. The task is to write a Java program that finds specific cards in a deck. To accomplish this, you can create a class representing a card with attributes like suit and value. Then, create a deck as an array or a collection of card objects. Implement a method to search for cards based on given criteria, such as suit or value.

Requirements and Specifications

program-to-find-cards-in-a-deck-in-java
program-to-find-cards-in-a-deck-in-java 1

Source Code

import java.util.ArrayList; public class CardDeckApp { public static void main(String[] args) { System.out.println("DECK"); //String[] deck = getDeck(); ArrayList deck = getDeck(); displayCards(deck); System.out.println("SHUFFLED DECK"); shuffleDeck(deck); displayCards(deck); int count = 2; System.out.println("Dealing 4 hands of " + count + " card each..."); for(int i = 0; i < 4; i++) { System.out.println("Hand No. " + (i+1) + ": HAND OF " + count + " CARDS"); //String[] hand = dealCards(deck, count); ArrayList hand = dealCards(deck, count); displayCards(hand); System.out.println(""); } } private static ArrayList getDeck() { String[] suits = {"Spades", "Hearts", "Diamonds", "Clubs"}; String[] ranks = {"Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King"}; // Create ArrayList of String ArrayList deck = new ArrayList (52); //String[] deck = new String[52]; for (String suit : suits) { for (String rank : ranks) { deck.add(rank + " of " + suit); //deck[i] = rank + " of " + suit; } } return deck; } private static void displayCards(ArrayList cards) { System.out.print("|"); for (String card : cards) { System.out.print(card + "|"); } System.out.println(); } private static void shuffleDeck(ArrayList deck) { for (int i = 0; i < deck.size(); i++) { String savedCard = deck.get(i); int randomIndex = (int) (Math.random() * deck.size()-1); deck.set(i, deck.get(randomIndex)); //deck[i] = deck[randomIndex]; //deck[randomIndex] = savedCard; deck.set(randomIndex, savedCard); } } private static ArrayList dealCards(ArrayList deck, int count) { //String[] hand = Arrays.copyOfRange(deck, 0, count); ArrayList hand = new ArrayList (count); // create empty // Now, add the first 'count' elements to hand and remove them from deck int i = 0; while(i < count) { String card = deck.remove(0); hand.add(card); i++; } // Display number of cards left in deck System.out.println(deck.size() + " cards left in deck after dealing hand of " + count + " cards."); return hand; } }

Similar Samples

Explore our diverse range of programming samples at ProgrammingHomeworkHelp.com. Our carefully curated examples cover various languages and topics, showcasing our proficiency in solving complex programming challenges. These samples serve as educational resources and demonstrate our commitment to delivering high-quality solutions tailored to your academic or professional needs.