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.
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
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:-
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...
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 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: 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 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 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 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
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...
Write a java program to convert and print an infix expression to postfix expression. You can use Java stack methods. (Must read input from System.in) Your main method should be as follow: public static void main(String args[]) { intopost p = new intopost (); String iexp, pexp; //infix postfix expression try{ Scanner inf = new Scanner (System.in); // Read input from KB/ File while(inf.hasNext()){ // read next infix expression iexp = inf.next(); // Assume method name to convert infix...
Hi. This is a prototype of Java. The following Java program was developed for prototyping a mini calculator. Run the program and provide your feedback as the user/project sponsor. (Save the code as MiniCalculator.java; compile the file: javac MiniCalculator.java; and then run the program: java MiniCalculator). HINTs: May give feedback to the data type and operations handled by the program, the way the program to get numbers and operators, the way the calculation results are output, the termination of the...