Please note, this is a STATIC archive of website www.tutorialspoint.com from 11 May 2019, cach3.com does not collect or store any user information, there is no "phishing" involved.
Tutorialspoint

BlackJack

//(c) A+ Computer Science
//www.apluscompsci.com
//Name - Bryan Nieto

import static java.lang.System.*;

public class CardRunner
{
	public static void main( String args[] )
	{
		Card one = new Card("SPADES", 9);
		out.println(one.getSuit());
		out.println(one.getFace());

		Card two = new Card("DIAMONDS", 1);
		out.println(two);
		two.setFace(3);
		out.println(two);

		Card three = new Card("CLUBS", 4);
		out.println(three);

		Card four = new BlackJackCard("SPADES", 1);
		out.println(four);

		Card five = new BlackJackCard("HEARTS", 13);
		out.println(five);
		
		Card six = new BlackJackCard("HEARTS", 11);
		out.println(six);
		
		Card seven = new BlackJackCard("CLUBS", 12);
		out.println(seven);	
	}
}


class Card
{
	public static final String FACES[] = {"ZERO","ACE","TWO","THREE","FOUR",
			"FIVE","SIX","SEVEN","EIGHT","NINE","TEN","JACK","QUEEN","KING"};

	//instance variables
	private String suit;
	private int face;

  	//constructors
    public Card( String s, int f)
    {
        suit = s;
        face = f;
    }

	// modifiers
	public void setFace( int f)
	{
	    face = f;
	}
	
	public void setSuit( String s)
	{
	    suit = s;
	}


  	//accessors
	public String getSuit()
	{
	    return suit;
	}
	
	public int getFace()
	{
	    return face;
	}
	
  	//toString
  	public String toString()
  	{
  	    return FACES[face] + " of " + suit;
  	}

 }
  
  
  class BlackJackCard extends Card //Card is superclass
  {
      //Create a constructor that recieves a suit and a face
      public BlackJackCard(String s,int f)
      {
          super(s, f ); //'super' refers to the Card superclass
      }
      //make a getValue() method that returns the worth of the card
      //based on the rules of BLackJack
public int getValue()
      {
          //create if statements for the conditions below
          //Ace is worth 11 for now
          if( getFace() == 1)
          return 11;
          //Jack, Queen, and King are all worth 10
          if( getFace() > 10)
          return 10;
          // all other cards are face value
        else 
        return getFace();
}
      
      //make a toString method
public String toString()
      {
         //return output example in lab
         return super.toString() + " " + getValue();
      }
  }

Advertisements
Loading...

We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy.