Question

Write in Java! Do NOT write two different programs for Deck and Card, it should be...

Write in Java! Do NOT write two different programs for Deck and Card, it should be only one program not 2 separate ones!!!!!!

The Learning Goal for this exercise is to use and understand and know the difference between arrays and array lists.

!!!!Use at least one array defined in your code and two array lists defined by the operation of your code!!!!

The array should be 52 elements and contain a representation of a standard deck of cards, in new deck order. (This is the order of a deck of cards new from the box.)

The 2 Array lists can be looked at as a draw deck and a hand deck. The original array is used to populate the draw deck before dealing.

  • Write and use a method to build a deck of cards where the array of cards is available in Main.
  • Use the clear screen method given in class, clear the screen when your program runs.(I am providing you an example code for clear screen method at the end of this page)
  • Use heading or titles and make the output look nice.
  • Back in Main,  print out the cards, 4 per line, 1 suit per column. (using a loop might help)
  • Use the deck array to create an array list of cards and be able to shuffle those cards.
  • Print out the shuffled deck.
  • Deal out 5 cards to another array list and the remaining cards should be the draw pile. (you will be taking cards from one ArrayList and adding it to another.)
  • Print both the hand (5 cards) and the draw deck (47 cards).
  • Go Beyond modify the code to do something fresh. For example, deal 4 hands.

Example Code for clear screen method:

class myControl  {
  public static void main(String[] args) {
    cls();
    System.out.println("Howdy!");
    sleep(5);
    cls();
    System.out.println("Oh, I thought you were someone else.");
    beep();
    sleep(2);
    System.out.println("GoodBye!");
  }// end main

  public static void sleep(int s) {
    try {
      Thread.sleep(s*1000);
    } catch(Exception e) {
      System.out.println("not sleepy");
    }//end try
  }//end sleep

  public static void beep() {
    System.out.println("\007");
  }//end beep

  public static void cls() {
    if (System.getProperty("os.name").contains("Mac")) {
      System.out.println("\033[H\033[2J");
    }else{
      try {
        new ProcessBuilder("cmd","/c","cls").inheritIO().start().waitFor();
      } catch(Exception e) {
        System.out.println("Error?");
      }//end try
    }//end if
  }// cls
}// end class

please run the program and make sure your program runs without a problem.
0 0
Add a comment Improve this question Transcribed image text
Answer #1


ANSWER:-

CODE:-

import java.util.*;
class Card{
   String rank;
   String suit;
   Card(String rank, String suit){
       this.rank = rank;
       this.suit = suit;
   }
   public String toString(){
       return this.rank+" of "+this.suit;
   }
}
class Deck{
   private String[] suits = {"Spades","Hearts","Diamonds","Clubs"};
   private String[] ranks = {"Ace","2","3","4","5","6","7","8","9","10","Jack","Queen","King"};
   Card[] cards;
   Deck(){
       this.cards = new Card[52];
   }
   public Card[] createDeck(){
       int k = 0;
       for(int i=0;i<suits.length;i++){
           for(int j=0;j<ranks.length;j++){
               this.cards[k++] = new Card(ranks[j],suits[i]);
           }
       }
       return this.cards;
   }
   public ArrayList<Card> shuffle(Card[] cards){
       ArrayList<Card> list = new ArrayList<Card>();
       for(int i=0;i<cards.length;i++){
           list.add(cards[i]);
       }

       Random random = new Random();
       for(int i=list.size()-1;i>0;i--){
           int rand = random.nextInt(i+1);
           Card temp = list.get(rand);
           list.set(rand,list.get(i));
           list.set(i,temp);
       }
       return list;
   }
   static class MyControl{
      public static void clsScreen() {
        cls();
        System.out.println("\n*********Welcome***********");
      }// end main

      public static void sleep(int s) {
        try {
          Thread.sleep(s*1000);
        } catch(Exception e) {
          System.out.println("not sleepy");
        }//end try
      }//end sleep

      public static void beep() {
        System.out.println("\007");
      }//end beep

      public static void cls() {
        if (System.getProperty("os.name").contains("Mac")) {
          System.out.println("\033[H\033[2J");
        }else{
          try {
            new ProcessBuilder("cmd","/c","cls").inheritIO().start().waitFor();
          } catch(Exception e) {
            System.out.println("Error?");
          }//end try
      }//end if
    }// cls
   }// end class
   public void dealCard(ArrayList<Card> deck,ArrayList<Card> hand){
       // deal one card from deck
       // add it hand
       hand.add(deck.get(0));
       deck.remove(0);
   }
   public String printDeck(ArrayList<Card> cards){
       String str = "[";
       for(int i=0;i<cards.size();i++){
           String delimiter = (i==cards.size()-1)?"":", ";
           str += cards.get(i).toString()+delimiter;
       }
       str += "]";
       return str;
   }
   public void printCards(){
       // printing cards
       System.out.println("\nHere is your sorted Deck!\n");
       for(int i=0;i<13;i++){
           int j=13;
           System.out.print(cards[i]+", ");
           System.out.print(cards[i+j]+", ");
           j += 13;
           System.out.print(cards[i+j]+", ");
           j += 13;
           System.out.println(cards[i+j]);
       }
       System.out.println();
   }
   public static void main(String[] args) {
       // Clearing screen
       MyControl.clsScreen();
       // Create Deck
       Deck deck = new Deck();
       deck.createDeck();
       // printing cards
       deck.printCards();
       // suffling deck
       ArrayList<Card> shuffledDeck = deck.shuffle(deck.cards);
       System.out.print("The suffled deck is: ");
       System.out.println(deck.printDeck(shuffledDeck));
       // taking arralists for 5 hands
       ArrayList<Card> hand1 = new ArrayList<Card>();
       ArrayList<Card> hand2 = new ArrayList<Card>();
       ArrayList<Card> hand3 = new ArrayList<Card>();
       ArrayList<Card> hand4 = new ArrayList<Card>();
       ArrayList<Card> hand5 = new ArrayList<Card>();
       for(int i=0;i<5;i++){
           // dealing cards
           deck.dealCard(shuffledDeck,hand1);
           deck.dealCard(shuffledDeck,hand2);
           deck.dealCard(shuffledDeck,hand3);
           deck.dealCard(shuffledDeck,hand4);
           deck.dealCard(shuffledDeck,hand5);
       }
       // print cards in each hand
       System.out.print("\nThe cards for the first hand is: ");
       System.out.println(deck.printDeck(hand1));
       System.out.println();
       System.out.print("\nThe cards for the second hand is: ");
       System.out.println(deck.printDeck(hand2));
       System.out.println();
       System.out.print("\nThe cards for the third hand is: ");
       System.out.println(deck.printDeck(hand3));
       System.out.println();
       System.out.print("\nThe cards for the fourth hand is: ");
       System.out.println(deck.printDeck(hand4));
       System.out.println();
       System.out.print("\nThe cards for the fifth hand is: ");
       System.out.println(deck.printDeck(hand5));
       System.out.println();
       System.out.print("The draw pile is: ");
       System.out.println(deck.printDeck(shuffledDeck));
   }
}

NOTE:- If you need any modifications in the code,please comment below.Please give positive rating.THUMBS UP.

                    THANK YOU!!!!

OUTPUT:-

Add a comment
Know the answer?
Add Answer to:
Write in Java! Do NOT write two different programs for Deck and Card, it should be...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write in Java! Do NOT write two different programs for Deck and Card, it should be...

    Write in Java! Do NOT write two different programs for Deck and Card, it should be only one program not 2 separate ones!!!!!! !!!!!!!!!!!!!!!Use at least one array defined in your code and two array lists defined by the operation of your code!!!!!!!!!!!!!!!!!!!!! The array should be 52 elements and contain a representation of a standard deck of cards, in new deck order. (This is the order of a deck of cards new from the box.) The 2 Array lists...

  • write a program to handle an exception that is generated when a program attempts to write...

    write a program to handle an exception that is generated when a program attempts to write beyond the end of an array in a lower scope such that that the exception is handled at a higher scope. java please write simple code so I can understand. use the code below public class Main { public static void main(String [] args) { int [] values = {1,2,3,4}; try { int theSum= calculate_sum(values); } catch (expection arrayindexoutofBoundsexpection) { //System.out.println(“tired to access array...

  • JAVA CODE This program causes an error and crashes. Compile and give it a test run,...

    JAVA CODE This program causes an error and crashes. Compile and give it a test run, note the exception that is thrown. Then do the following to fix the problem 1. Write code to handle this exception 2. Use try, catch and finally blocks. 3. Display, the name of the exception 4. Display a message from the user about what happened. Here is the starting file BadArray.java: public class BadArray { public static void main(String[] args) { // Create an...

  • Java Write a complete program that implements the functionality of a deck of cards. In writing...

    Java Write a complete program that implements the functionality of a deck of cards. In writing your program, use the provided DeckDriver and Card classes shown below. Write your own Deck class so that it works in conjunction with the two given classes. Use anonymous objects where appropriate. Deck class details: Use an ArrayList to store Card objects. Deck constructor: The Deck constructor should initialize your ArrayList with the 52 cards found in a standard deck. Each card is a...

  • In java---- The DeckTester.java file, provides a basic set of Deck tests. Add additional code at...

    In java---- The DeckTester.java file, provides a basic set of Deck tests. Add additional code at the bottom of the main method to create a standard deck of 52 cards and test the shuffle method ONLY in the Deck class. After testing the shuffle method, use the Deck toString method to “see” the cards after every shuffle. Deck: import java.util.List; import java.util.ArrayList; /** * The Deck class represents a shuffled deck of cards. * It provides several operations including *...

  • Question 1 8 pts Write all the different possible outputs of the following Java multi-threaded program:...

    Question 1 8 pts Write all the different possible outputs of the following Java multi-threaded program: class MultithreadingDemo extends Thread public void runot try{ System.out.println (--Multithread.countDown): catch (Exception e) w public class Multithread{ public static int countDown = 10; public static void main(String[] args) { for (int i = 0; i < 3; i++) { MultithreadingDemo object = new MultithreadingDemol); object.start(); } میه

  • JAVA HELP: Directions Write a program that will create an array of random numbers and output...

    JAVA HELP: Directions Write a program that will create an array of random numbers and output the values. Then output the values in the array backwards. Here is my code, I am having a problem with the second method. import java.util.Scanner; import java.util.Random; public class ArrayBackwards { public static void main(String[] args) { genrate(); print(); } public static void generate() { Scanner scanner = new Scanner(System.in);    System.out.println("Seed:"); int seed = scanner.nextInt();    System.out.println("Length"); int length = scanner.nextInt(); Random random...

  • Looking for some simple descriptive pseudocode for this short Java program (example italicized in bold directly...

    Looking for some simple descriptive pseudocode for this short Java program (example italicized in bold directly below): //Create public class count public class Count {     public static void main(String args[])     {         int n = getInt("Please enter an integer value greater than or equal to 0");                System.out.println("Should count down to 1");         countDown(n);                System.out.println();         System.out.println("Should count up from 1");         countUp(n);     }            private static void countUp(int n)     {...

  • Question 1 (5 points) Question 1 Unsaved What is displayed on the console when running the...

    Question 1 (5 points) Question 1 Unsaved What is displayed on the console when running the following program? public class Quiz2B { public static void main(String[] args) { try { System.out.println("Welcome to Java"); int i = 0; int y = 2 / i; System.out.println("Welcome to Java"); } catch (RuntimeException ex) { System.out.println("Welcome to Java"); } finally { System.out.println("End of the block"); } } } Question 1 options: The program displays Welcome to Java two times. The program displays Welcome to...

  • Computer Science 182 Data Structures and Program Design Programming Project #3 – Link List Card Games...

    Computer Science 182 Data Structures and Program Design Programming Project #3 – Link List Card Games One of the nice things about Java is it is very easy to do fun things with graphics. The start code provided here will display a deck of cards on the screen and shuffle them. Your mission, is to start with this code and build a card game. Blackjack, poker solitaire, what ever your heart desires. Blackjack is the easiest. Obviously any program you...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT