troubleshooting Question

How do I throw a exception while validating numbers?

Avatar of ryanbecker24
ryanbecker24 asked on
Java
16 Comments1 Solution241 ViewsLast Modified:
Hi,

I am very new to exceptions.

import java.util.ArrayList;
/**
 * Abstract class Participant - write a description of the class here
 * 
 * @author Ryan Becker
 * @version (version number or date here)
 */
public abstract class Participant extends Deck
{
    // instance variables - replace the example below with your own
    protected ArrayList<Participant> participantName;
    protected static double participantNumber;
    protected static double availableFunds;
    protected static double moneyWon;
    protected static double moneyLost;
  
    /**
     * Sets the participant’s name to “Participant-< Participant Number>”
     * Instantiates object of class Hand 
     * Sets Available Funds to $10,000
     * Sets Money Won Statistic  and Money Lost Statistic  to 0
     * Increments instance variable Participant Number  by 1 
    */
    public Participant()
    {
        availableFunds = 10000;
        moneyWon = 0;
        moneyLost = 0;
        participantNumber = 0;
        deck = new ArrayList<Card>();
        participantNumber++;
        
    }
    
    /**
     * Class Constructor which takes participant’s name and the initial amount of money available for betting 
     * Sets the player’s name to provided value
     * Instantiates object of class Hand 
     * Validates and then sets Available Funds to provided value. If the value is invalid (i.e., smaller than the minimum amount allowed or larger than the maximum amount
     * allowed) then the method throws an appropriate exception
     * Sets Money Won Statistic  and Money Lost Statistic  to 0
     * Increment instance variable Player Number  by 1 
     */
    public Participant(double availableFunds, ArrayList<Participant> participantNumber)
    {
        if(availableFunds < 10000 && availableFunds > 0){
        this.availableFunds = availableFunds;
        else{
            throw new NumberFormatException();
        }
        moneyWon = 0;
        moneyLost = 0;
        participantNumber++;
    }
}
    /**
     * Validates and then sets Available Funds to provided value. If the value is invalid 
     * (i.e., smaller than the minimum amount allowed or larger than the maximum amount allowed) then the method throws an appropriate exception
     */
    
    /**
     * Get the value of the money you have lost
     */
    public double getMoneyLost()
    {
        return moneyLost;
    }
    
    /**
     * Set the amount of money you have lost
     */
    public void setMoneyLost(double moneyL)
    {
        moneyLost = moneyL;
    }
    
    /**
     * Get the amount of money you have won
     */
    public double getMoneyWon()
    {
        return moneyWon;
    }
    
    /**
     * Set the amount of money you have lost
     */
    public void setMoneyWon(double moneyW)
    {
        moneyWon = moneyW;
    }
    
    /**
     * Set the amount of the available funds
     */
    public void setAvailableFunds(double funds)
    {
        availableFunds = funds;
    }
    
    /**
     * Get the amount of available funds
     */
    public double getAvailableFunds()
    {
        return availableFunds;
    }
    
    /**
     * Get the player name
     */
    public ArrayList<Participant> getPlayerName()
    {
        return participantName;
    }
    
    /**
     * Set the players name
     */
    public void setPlayerName(ArrayList<Participant> name)
    {
        participantName = name;
    }
    
    /**
     * This is a toString() method that returns the 
     */
//     public String toString()
//     {
//         
//     }
    
    /**
     * abstract method that will get the minimum amount of funds that you can have
     */
    public abstract double getMinFundsLimit();
    
    /**
     * abstract method that will get the maximum amount of funds that you can have
     */
    public abstract double getMaxFundsLimit();
}

import java.util.*;

/*
 * Contains information about the BlackJack hand of cards
 */
/**
 *
 * @author: Dr. Hnatyshin
 * @note:   Does not complete a full javadoc spec of comments
 */
public class Hand extends Deck {

    // player's hand of cards
   // private ArrayList<Card> hand;
    private static final Random r = new Random();
    public static final int MAX_HAND_SIZE = 5;
    public static final int MIN_HAND_SIZE = 2;

    // Constructor
    public Hand() {
        //hand = new ArrayList<Card>();
        deck = new ArrayList<Card>();
    }

    // SUPPORTING METHODS
    // Get the number of cards in the hand
    /**public int getSize() {
        return deck.size();
    }

    // Determine if the hand is empty
    

    // Dump all the cards from the hand
    /**public void reset() {
        hand.clear();
    }

    // Determines if the current hand contains given card
    public boolean contains(Card c) {
        return hand.contains(c);
    }

    // Add a card to the hand
    public void addCard(Card c) {
        hand.add(c);
    }

    // ACCESSORS/MUTATORS
    /**
     * Retrieves and removes a random card from the hand.
     * @return a Card object that will be removed from the hand or
     * null if hand is empty.
     */
    public Card retrieveCard() {
        if (!this.isEmpty()) {
            return deck.get(0);
        }
        return null;
    }

    /**
     * Retrieves and removes a card from specified position in the hand.
     * @return a Card object that will be removed from the hand or
     *  null if hand is empty.
     */
    public Card retrieveCard(int i) {

        // Assume that the user counts cards in a hand starting from 1
        if (i > 0 && i <= this.getSize()) {
            return deck.get(i-1);
        }
        return null;
    }

    /**
     * Retrieves but not removes a card from specified position in the hand.
     * The user counts card in the hand from position 1 and not 0
     * @return a Card object from the deck array.If no object is available in
     *  specified position then returns null
     */
    public Card getCard(int i) {

        // Check if i is valid
        // if not return null
        if (i < 1 || i > this.getSize()) {
            return null;
        }

        // Not enough cards in a deck
        if (deck.size() < i) {
            return null;
        }

        // Remove and return a random card from a specified position in a hand
        // Assume that player counts from 1 instead of 0
        return deck.get(i - 1);
    }

    /**
     * Retrieves but not removes a random card from the hand.
     * @return a Card object from the hand or null if the and is empty
     *
    public Card getCard() {
        // Create new deck if current deck is empty
        if (this.getSize() < 1) {
            reset();
        }

        // Remove and return a random card from a hansd
        return hand.get(r.nextInt(hand.size()));
    }


    // BLACK JACK GAME SUPPORTING METHODS

    /**
     * Return an integer value representing the number of points in the hand
     * given to the method.
     * 
     * @param hand An arrayList containing the value of the hand.
     * @return an integer value representing the value of the hand.
     */
    public int computeBlackJackValue() {
        int numberOfAces = 0;
        int valueOfHand = 0;
        Card c;

        for (int i = 0; i < deck.size(); i++) // Increment through the hand
        {
            c = deck.get(i); //Get the card value
            if (c.getRank().equals("Ace")) {
                numberOfAces += 1; // Increment the number of aces in the hand by 1.
            }
            valueOfHand += Card.getCardValue(c); // Get the value of the Hand.
        }

        if ((numberOfAces >= 1) && (valueOfHand < 12)) {
            valueOfHand += 10; // Make an Ace 11 if the hand value is under 12.
        }

        return valueOfHand;
    }

    /*
     * Compare two hands and see who won. Returns a: 1 for first hand won the
     * game, 0 for nobody winning the game (its a push), or -1 the first
     * hand lost the game.
     *
     * @param hand01 The first hand to be compared.
     * @param hand02 The second hand to be compared.
     * @return A 1, 0, or -1 depending on who won the game.
     */
    public int blackJackCompare(Hand h) {
        //Compute hand values
        int thisHandValue = this.computeBlackJackValue();
        int givenHandValue = h.computeBlackJackValue();

        // Draw
        if (thisHandValue == givenHandValue) {
            return 0;
        }

        // First player wins if
        // both hands are <= 21 and first hand greater than second
        // or
        // second hand > 21 and first hand smaller than second
        if ((thisHandValue <= 21 && givenHandValue <= 21 && thisHandValue > givenHandValue)
                || (givenHandValue > 21 && thisHandValue < givenHandValue)) {
            return 1;
        }

        // Otherwise second player won
        return -1;
    }

    // OTHER SUPPORTING METHODS

    /**
     * Display the value of each Card followed by the hand value.
     * 
     * @param hand The hand that will be evaluated and printed.
     */
    public void printHand() {

        System.out.println(this);
    }

    // Return string representation of a string
    @Override
    public String toString() {
        //String result = "[";

        //for (int i = 0; i < hand.size(); i++) {
            //result += hand.get(i).toStringShort();

            /* no space (" ") added after last card
            //if (i != hand.size() - 1) {
                //System.out.print(" ");
           //}
        //}
        */
        return super.toString() + "] => " + this.computeBlackJackValue();
    }

    // Determines if this given object is the same
    // as the current object
    @Override
    public boolean equals(Object o) {
        if (o == this) {
            return true;
        }

        if (!(o instanceof Hand)) {
            return false;
        }

        Hand h = (Hand) o;
        if (h.getSize() != this.getSize()) {
            return false;
        }

        if (h.getSize() == 0) {
            return true;
        }

        // Check if all the cards in the hands are the same
        // Rememebr that getCard methods counts from 1
        for (int i = 0; i < h.getSize(); i++) {
            if (!this.contains(h.getCard(i+1))) {
                return false;
            }
        }

        return true;
    }

    // Netbeans addded hashCode method
    @Override
    public int hashCode() {
        int hash = 5;
        hash = 97 * hash + (this.deck != null ? this.deck.hashCode() : 0);
        return hash;
    }
}
mport java.util.*;

/*
 * Contains information about the BlackJack hand of cards
 */
/**
 *
 * @author: Dr. Hnatyshin
 * @note:   Does not complete a full javadoc spec of comments
 */
public class Hand extends Deck {

    // player's hand of cards
   // private ArrayList<Card> hand;
    private static final Random r = new Random();
    public static final int MAX_HAND_SIZE = 5;
    public static final int MIN_HAND_SIZE = 2;

    // Constructor
    public Hand() {
        //hand = new ArrayList<Card>();
        deck = new ArrayList<Card>();
    }

    // SUPPORTING METHODS
    // Get the number of cards in the hand
    /**public int getSize() {
        return deck.size();
    }

    // Determine if the hand is empty
    

    // Dump all the cards from the hand
    /**public void reset() {
        hand.clear();
    }

    // Determines if the current hand contains given card
    public boolean contains(Card c) {
        return hand.contains(c);
    }

    // Add a card to the hand
    public void addCard(Card c) {
        hand.add(c);
    }

    // ACCESSORS/MUTATORS
    /**
     * Retrieves and removes a random card from the hand.
     * @return a Card object that will be removed from the hand or
     * null if hand is empty.
     */
    public Card retrieveCard() {
        if (!this.isEmpty()) {
            return deck.get(0);
        }
        return null;
    }

    /**
     * Retrieves and removes a card from specified position in the hand.
     * @return a Card object that will be removed from the hand or
     *  null if hand is empty.
     */
    public Card retrieveCard(int i) {

        // Assume that the user counts cards in a hand starting from 1
        if (i > 0 && i <= this.getSize()) {
            return deck.get(i-1);
        }
        return null;
    }

    /**
     * Retrieves but not removes a card from specified position in the hand.
     * The user counts card in the hand from position 1 and not 0
     * @return a Card object from the deck array.If no object is available in
     *  specified position then returns null
     */
    public Card getCard(int i) {

        // Check if i is valid
        // if not return null
        if (i < 1 || i > this.getSize()) {
            return null;
        }

        // Not enough cards in a deck
        if (deck.size() < i) {
            return null;
        }

        // Remove and return a random card from a specified position in a hand
        // Assume that player counts from 1 instead of 0
        return deck.get(i - 1);
    }

    /**
     * Retrieves but not removes a random card from the hand.
     * @return a Card object from the hand or null if the and is empty
     *
    public Card getCard() {
        // Create new deck if current deck is empty
        if (this.getSize() < 1) {
            reset();
        }

        // Remove and return a random card from a hansd
        return hand.get(r.nextInt(hand.size()));
    }


    // BLACK JACK GAME SUPPORTING METHODS

    /**
     * Return an integer value representing the number of points in the hand
     * given to the method.
     * 
     * @param hand An arrayList containing the value of the hand.
     * @return an integer value representing the value of the hand.
     */
    public int computeBlackJackValue() {
        int numberOfAces = 0;
        int valueOfHand = 0;
        Card c;

        for (int i = 0; i < deck.size(); i++) // Increment through the hand
        {
            c = deck.get(i); //Get the card value
            if (c.getRank().equals("Ace")) {
                numberOfAces += 1; // Increment the number of aces in the hand by 1.
            }
            valueOfHand += Card.getCardValue(c); // Get the value of the Hand.
        }

        if ((numberOfAces >= 1) && (valueOfHand < 12)) {
            valueOfHand += 10; // Make an Ace 11 if the hand value is under 12.
        }

        return valueOfHand;
    }

    /*
     * Compare two hands and see who won. Returns a: 1 for first hand won the
     * game, 0 for nobody winning the game (its a push), or -1 the first
     * hand lost the game.
     *
     * @param hand01 The first hand to be compared.
     * @param hand02 The second hand to be compared.
     * @return A 1, 0, or -1 depending on who won the game.
     */
    public int blackJackCompare(Hand h) {
        //Compute hand values
        int thisHandValue = this.computeBlackJackValue();
        int givenHandValue = h.computeBlackJackValue();

        // Draw
        if (thisHandValue == givenHandValue) {
            return 0;
        }

        // First player wins if
        // both hands are <= 21 and first hand greater than second
        // or
        // second hand > 21 and first hand smaller than second
        if ((thisHandValue <= 21 && givenHandValue <= 21 && thisHandValue > givenHandValue)
                || (givenHandValue > 21 && thisHandValue < givenHandValue)) {
            return 1;
        }

        // Otherwise second player won
        return -1;
    }

    // OTHER SUPPORTING METHODS

    /**
     * Display the value of each Card followed by the hand value.
     * 
     * @param hand The hand that will be evaluated and printed.
     */
    public void printHand() {

        System.out.println(this);
    }

    // Return string representation of a string
    @Override
    public String toString() {
        //String result = "[";

        //for (int i = 0; i < hand.size(); i++) {
            //result += hand.get(i).toStringShort();

            /* no space (" ") added after last card
            //if (i != hand.size() - 1) {
                //System.out.print(" ");
           //}
        //}
        */
        return super.toString() + "] => " + this.computeBlackJackValue();
    }

    // Determines if this given object is the same
    // as the current object
    @Override
    public boolean equals(Object o) {
        if (o == this) {
            return true;
        }

        if (!(o instanceof Hand)) {
            return false;
        }

        Hand h = (Hand) o;
        if (h.getSize() != this.getSize()) {
            return false;
        }

        if (h.getSize() == 0) {
            return true;
        }

        // Check if all the cards in the hands are the same
        // Rememebr that getCard methods counts from 1
        for (int i = 0; i < h.getSize(); i++) {
            if (!this.contains(h.getCard(i+1))) {
                return false;
            }
        }

        return true;
    }

    // Netbeans addded hashCode method
    @Override
    public int hashCode() {
        int hash = 5;
        hash = 97 * hash + (this.deck != null ? this.deck.hashCode() : 0);
        return hash;
    }
}
ASKER CERTIFIED SOLUTION
Join our community to see this answer!
Unlock 1 Answer and 16 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 16 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros