View the step-by-step solution to:

Just need any "JAVA expert" check my code and make some correction whatever is need according the instruction:

Just need any "JAVA expert" check my code and make some correction whatever is need according the instruction:

Hi, I try to make my assignment JAVA code and my instructor helped me thats I upload below. Now I can no make according the instruction how to make separate 3 or more extends class like "inheritance from another class" and abstract class.


I need to make JAVA program that recreates a game of Blackjack (aka 21). The object of the game is to accumulate cards that add up to 21 without exceeding this total (BUST). Your game should include 2 players and a dealer. The players bet money that the total points in their hand is greater than that of the Dealer (the players should not compete against each other)

It should required that we use these things within the code:

- if-else/else- if statement

- for loop, while loop, and/or do-while loop (must use two or more)

- read/write/or read & write to/from a file

- use 5 methods

- use 1 overloaded methods

-use 3 classes

-inheritance from another class

-abstract class

-include two arrays/arrayLists

-import 3 or more libraries

-All exceptions should be handled gracefully.

-include at least 1 programmer-created exception

- do not allow invalid inputs


## Here is my code:

// Name: Aktaruzzaman

// Date: November 29, 2018

// Purpose: To make a Blackjack project (class and demo).


import java.util.ArrayList;

import java.util.Random;

import java.util.Scanner;

import java.util.*;


class Deck {


public static final int HEARTS = 0;

public static final int DIAMONDS = 1;

public static final int SPADES = 2;

public static final int CLUBS = 3;

public static final int JACK = 11;

public static final int QUEEN = 12;

public static final int KING = 13;

public static final int ACE = 14;

private ArrayList<Card> deck;


public Deck() {

deck = new ArrayList<Card>();


for(int rank = 2; rank <= ACE; rank++){

for(int suit = HEARTS; suit <= CLUBS; suit++){

Card card = new Card(rank, suit);

deck.add(card);}

}

shuffle();

}


public ArrayList getCards(){

return deck;

}


public Card deal(){

return deck.remove(0);


}


public void shuffle(){

for(int i = 0; i < deck.size(); i++){

Random r = new Random();

int randomIndex = r.nextInt(52);

Card one = deck.get(i);

Card two = deck.get(randomIndex);

deck.set(i,two);

deck.set(randomIndex,one);

}


}


}


class Hand {

public String toString() {

return "Hand [cards=" + cards + "]";


}

public static final int HEARTS = 0;

public static final int DIAMONDS = 1;

public static final int SPADES = 2;

public static final int CLUBS = 3;

public static final int JACK = 11;

public static final int QUEEN = 12;

public static final int KING = 13;

public static final int ACE = 14;

private ArrayList <Card> cards;


public Hand() {

cards = new ArrayList<Card>();

}


public void addCard(Card c) {

cards.add(c);


}


public int getValue() {

int sum = 0;

int aceCount = 0;

for(Card c: cards)

{


sum += c.getValue();

if(c.getRank() == ACE) {

aceCount++;

}


}


while(sum > 21 && aceCount > 0) {

sum -= 10;

aceCount--;

}


return sum;


}


public boolean hasBlackJack() {

return getValue() == 21 && cards.size() == 2;

}


public boolean busted() {

return getValue() > 21;


}


public ArrayList<Card> getCards(){

return cards;

}


}


class Card {


public static final int HEARTS = 0;

public static final int DIAMONDS = 1;

public static final int SPADES = 2;

public static final int CLUBS = 3;

public static final int JACK = 11;

public static final int QUEEN = 12;

public static final int KING = 13;

public static final int ACE = 14;


private int rank;

private int suit;

private int value;

private String[]ranks = {"X","X","Two of ","Three of ","Four of ",

"Five of ","Six of ","Seven of ","Eight of ","Eight of ",

"Nine of ","Ten of ","Jack of ","Queen of ", "King of ","Ace of "};

private String[]suits = {"Hearts","Diamonds","Spades","Clubs"};


public Card(int r, int s) {

rank = r;

suit = s;

}


public void setRank(int rank) {

this.rank = rank;

}


public void setValue(int value) {

this.value = value;

}


public int getRank() {

return rank;

}


public int getSuit() {

return suit;

}


public int getValue() {

int value = rank;

if(rank > 10){

value = 10;

}


if(rank == ACE) {

value = 11;

}

return value;

}


public String rankToString(int r) {

return ranks[r];

}


public String suitToString(int s) {

return suits[s];

}


public String getSuitAsString() {

return suitToString(suit);

}


public String getRankAsString() {

return rankToString(rank);

}


public String toString() {

String rankString = ranks[rank];

String suitString = suits[suit];

return rankString + suitString;

}


}


class Randomizer {

public static Random theInstance = null;


public Randomizer() {


}


public static Random getInstance() {

if(theInstance == null) {

theInstance = new Random();

}


return theInstance;


}


public static boolean nextBoolean() {

return Randomizer.getInstance().nextBoolean();


}


public static boolean nextBoolean(double probability) {

return Randomizer.nextDouble() < probability;


}


public static int nextInt() {

return Randomizer.getInstance().nextInt();


}


public static int nextInt(int n) {

return Randomizer.getInstance().nextInt(n);


}


public static int nextInt(int min, int max) {

return min + Randomizer.nextInt(max - min + 1);


}


public static double nextDouble() {

return Randomizer.getInstance().nextDouble();


}


public static double nextDouble(double min, double max) {

return min + (max - min) * Randomizer.nextDouble();

}


}



//

public class AktauzzamanBlackjack {

private static final int HEARTS = 0;

private static final int DIAMONDS = 1;

private static final int SPADES = 2;

private static final int CLUBS = 3;

private static final int JACK = 11;

private static final int QUEEN = 12;

private static final int KING = 13;

private static final int ACE = 14;

private static final int STARTING_BANKROLL = 100;


public static String readLine(String str){


System.out.println(str);

Scanner sc = new Scanner(System.in);

return sc.nextLine();

}


public static int readInt(String str){

System.out.println(str);

Scanner sc = new Scanner(System.in);

return sc.nextInt();

}


private String getPlayerMove() {


while(true) {

String move = readLine("Enter move (hit/stand): ");

move = move.toLowerCase();


if(move.equals("hit") || move.equals("stand")) {

return move;

}


System.out.println("Please try again.");

}


}



private void dealerTurn(Hand dealer, Deck deck) {


while(true) {

System.out.println("Dealer's hand");

System.out.println(dealer);


int value = dealer.getValue();

System.out.println("Dealer's hand has value " + value);

readLine("Enter to continue...");


if(value < 17) {

System.out.println("Dealer hits");

Card c = deck.deal();

dealer.addCard(c);


System.out.println("Dealer card was " + c);


if(dealer.busted()) {


System.out.println("Dealer busted!");


break;

}


}


else {


System.out.println("Dealer stands.");


break;

}


}


}



private boolean playerTurn(Hand player, Deck deck) {


while(true) {

String move = getPlayerMove();


if(move.equals("hit")) {

Card c = deck.deal();

System.out.println("Your card was: " + c);

player.addCard(c);

System.out.println("Player's hand");

System.out.println(player.toString());


if(player.busted()) {

return true;

}


}


else


{


return false;


}


}


}



private boolean playerWins(Hand player, Hand dealer) {

if(player.busted()) {

return false;

}


if(dealer.busted()) {

return true;

}


return player.getValue() > dealer.getValue();

}



private boolean push(Hand player, Hand dealer) {

return player.getValue() == dealer.getValue();

}



private double findWinner(Hand dealer, Hand player, int bet) {

if(playerWins(player, dealer)) {

System.out.println("Player wins!");


if(player.hasBlackJack()) {

return 1.5 * bet;

}


return bet;

}


else if(push(player, dealer)) {

System.out.println("You push");

return 0;

}


else {

System.out.println("Dealer wins");

return -bet;

}


}



private double playRound(double bankroll) {

int bet = readInt("What is your bet? ");

Deck deck = new Deck();

deck.shuffle();


Hand player = new Hand();

Hand dealer = new Hand();


player.addCard(deck.deal());

dealer.addCard(deck.deal());


player.addCard(deck.deal());

dealer.addCard(deck.deal());


System.out.println("Player's Hand");

System.out.println(player);

System.out.println("Dealer's hand");


boolean playerBusted = playerTurn(player, deck);

if(playerBusted) {

System.out.println("You busted :(");

}


readLine("Enter for dealer turn...");

dealerTurn(dealer, deck);


double bankrollChange = findWinner(dealer, player, bet);

bankroll += bankrollChange;

System.out.println("New bankroll: " + bankroll);


return bankroll;

}



public void run() {

double bankroll = STARTING_BANKROLL;

System.out.println("Starting bankroll: " + bankroll);

while(true) {

bankroll = playRound(bankroll);


String playAgain = readLine("Would you like to play again? (Y/N)");

if(playAgain.equalsIgnoreCase("N")) {

break;

}


}


System.out.println("Thanks for playing!");

}


public static void main(String args[]){


AktauzzamanBlackjack b = new AktauzzamanBlackjack();

b.run();

}


}

Recently Asked Questions

Why Join Course Hero?

Course Hero has all the homework and study help you need to succeed! We’ve got course-specific notes, study guides, and practice tests along with expert tutors.

-

Educational Resources
  • -

    Study Documents

    Find the best study resources around, tagged to your specific courses. Share your own to gain free Course Hero access.

    Browse Documents
  • -

    Question & Answers

    Get one-on-one homework help from our expert tutors—available online 24/7. Ask your own questions or browse existing Q&A threads. Satisfaction guaranteed!

    Ask a Question