Question: can you please convert the following c++ code to java and run it and give output screenhsot Card.h #include using namespace std; /* Create a
can you please convert the following c++ code to java and run it
and give output screenhsot
Card.h
#include using namespace std; /* Create a class Card With a rank and suit as attributes And their card representation getter */ class Card { private: string rank; string suit; //Methods public: Card() {} Card(string rank, string suit); string getRank(); string getSuit(); string getCard(); };
Card.cpp
//Implementation of class Card #include "Card.h" //Constructor //Set rank and suit value Card::Card(string rank, string suit) { this->rank = rank; this->suit = suit; } //Return card representation string Card::getCard() { return rank + " of " + suit; } //Method to get rank of the card string Card::getRank() { return rank; } //Method to get suit of the card string Card::getSuit() { return suit; }
Deck.h
#include "Card.h" /* Create a class Deck Which has an array of 52 cards Shuffle them and draw them */ class Deck { //Attribute private: Card deck[52]; //Methods public: Deck(); int shuffle(); Card drawACard(); };
Deck.cpp
//Implementation of Deck class #include "Deck.h" //Constructor Deck::Deck() { //Two arrays for ranks and suits const string ranks[] = { "2","3","4","5","6","7","8","9","10","Jack","Queen","King","Ace" }; const string suits[] = { "Clubs","Diamonds","Hearts","Spades" }; int j = 0, k = 0; //Fill deck array for (int i = 0; i < 52; i++) { if (k < 4) { deck[i] = Card(ranks[j], suits[k++]); } else { deck[i] = Card(ranks[j++], suits[0]); k = 1; } } } //Method to randomly shuffle the cards //Return size of the array int Deck::shuffle() { int index = 0, i = 0; Card temp; srand(0); for (i = 0; i < 52; i++) { index = rand() % 52; temp = deck[i]; deck[i] = deck[index]; deck[index] = temp; } return 52; } //Take a card from deck randomly Card Deck::drawACard() { return deck[rand() % 52]; }
Main.cpp
/* Program to play Black jack game Here 1 player only game implemented */ #include #include; #include "Deck.h" using namespace std; //Function to return points of eack card int getPoints(Card card) { if (card.getRank() == "2"){ return 2; } else if (card.getRank() == "3") { return 3; } else if (card.getRank() == "4") { return 4; } else if (card.getRank() == "5") { return 5; } else if (card.getRank() == "6") { return 6; } else if (card.getRank() == "7") { return 7; } else if (card.getRank() == "8") { return 8; } else if (card.getRank() == "9") { return 9; } else if (card.getRank() == "10") { return 10; } else if (card.getRank() == "Jack") { return 11; } else if (card.getRank() == "Queen") { return 12; } else if (card.getRank() == "King") { return 13; } else if (card.getRank() == "Ace") { return 14; } } //Main function int main() { char ch; //Welcome message cout << "Black J Card Game: Good Luck! "; //Create a deck of cards Deck deck; //Shuffle int cnt = deck.shuffle(); //Start play cout << "Dealer has shuffled a deck of " << cnt << " cards. "; //Loop until user ready to quit while (true) { //Player hand cards storage vector playerHand; //Player points int total = 0; //Prompt for card draw cout << "Do you want to get one more card (y/n)? "; cin >> ch; //If yes while (ch == 'y' || ch == 'Y') { //Take a card from deck Card temp = deck.drawACard(); //Add into hand playerHand.push_back(temp); //Calculate total total += getPoints(temp); //Display cards and total cout << "Here are your cards : "; for (int i = 0; i < playerHand.size(); i++) { cout << playerHand[i].getCard() << endl; } cout << "Total: " << total << endl; //Check ponts and display appropriate message if (total > 21) { cout << "Sorry! Your total points exceed 21, therefore you have lost, Dealer wins "; break; } //Repetiton cout << "Do you want to get one more card (y/n)? "; cin >> ch; } //Otherwise if (ch == 'n' || ch == 'N' && total<=21) { cout << "It does not exceed 21, since we dont know Deelers card, we cannot tell if you win or lose "; } cout << "Continue ? (y / n) :"; cin >> ch; //Check user wish to quit if (ch == 'n' || ch == 'N') { cout << "Good bye! "; break; } } return 0; }