So in class we had to do the ArrayBag class, which im sure all of you know about. If not its all over the internet. Anyway our project is to turn that into a black Jack game. I however have no idea how to do this. I would want to make a card class a deck class then the black jack class. However once again i dont know how i could implement that into the Array bag class. Someone help! Ill add the codes for what i have so far.
Array bag
package bag;
//I will will keep T because its the norm, but i really don't want to.
public class ArrayBag <T> implements BagInterface<T>{
private final T [] bag; // our generic array
private static final int DefualtItemCapacity = 52; // our default capacity of items...Make DEFAULT all caps because all the i's made it look ugly
private int numberOfItems ;
public ArrayBag (int itemCapacity){
numberOfItems = 0 ; // set numberOfItems to empty 0
@SuppressWarnings("unchecked") // java i know what im doing, #Suppressed !
T[] tempBag = (T[]) new Object [itemCapacity];
bag= tempBag;
}// end constructor
public ArrayBag() {
this(DefualtItemCapacity);
} // if nothing, call DefualtItemCapacity, dont have to check for null if we overload the method
// thank you eclipse, for creating the methods from the interface :)
@Override
public int getCurrentSize() {
// TODO Auto-generated method stub
return numberOfItems;
}// end current size
@Override
public boolean isFull() {
// TODO Auto-generated method stub
return numberOfItems==bag.length; // return numberOfItems as full
}// end full
@Override
public boolean isEmpty() {
// TODO Auto-generated method stub
return numberOfItems==0;// return numberOfItems as empty
}// end empty
@Override
public boolean add(T newItem) {
boolean result = true ;
if(isFull()){ // bag is full , return false
result = false ;
} else { // bag is not full add new item
bag[numberOfItems]= newItem ;
numberOfItems++;
}
return result;
}// end bool add
@Override
public T remove() {
T temp = bag[numberOfItems-1]; // bag numberOfItems is now one less
bag[numberOfItems-1]=null ; // then we have to set that spot to null
numberOfItems--; // then make sure numberOfItems is one less aswell
return temp;
}// end T remove
@Override
public boolean remove(T anItem) {
boolean result = true ;
for (int i = 0; i < numberOfItems; i++){
if(bag[numberOfItems]== anItem ) {
bag[numberOfItems] = null ;
/* items[numItems-1] = null;*/ // this might shift it left
numberOfItems--;
result = true;
}else {
result = false ; // item not found
}
}
return result;
}// end bool remove
@Override
public void clear() {
while(!isEmpty()){ // while the bag is not empty we can remove stuff.
remove();
}
}// end clear
@Override
public T grab() {
int i;
while (numberOfItems != 0){ // while numberOfItems is not empty
i = (int)(Math.random( ) * numberOfItems) + 1; // i is a random value
return bag[i]; // return random item from bag
}
return null ; // is empty , return null
} // end grab
@Override
public int getFrequencyOf(T anItem) { // count how many items are the same
int count=0 ; // count how many are the same
for (int i = 0; i < numberOfItems; i++){
if(bag[i]==anItem){ // if current item = an item add to the count value
count++;
}
}
return count;
}// end Frequency
@Override
public boolean contains(T anItem) {
while(anItem != null){
for (int i = 0; i < numberOfItems; i++){
if (anItem.equals(bag[i])){ //bag contains the specified item, use .equals because java is dumb and == gives an error.
return true;
}
}
}return false; // bag doesnt contain item
}// end contains
@Override
public T[] toArray() { // creates and returns an array containing all of the items
T[] result = (T[]) new Object [numberOfItems];
for(int i =0; i < numberOfItems; i++){
result[i]=bag[i];
}
return result;
} // end toArray
} // end class
CardClass
package bag;
public class Card {
private String cardSuit ;
private int card ;
Card(String suit, int card ){
this.cardSuit = cardSuit ;
this.card = card ;
}
public String toString(){ // Mr.Englend, i overloaded the toString :'(
String CardNumber = null;
switch (this.card){
case 1:
CardNumber = "Ace" ;
break;
case 2:
CardNumber = "Two" ;
break;
case 3:
CardNumber = "Three" ;
break;
case 4:
CardNumber = "Four" ;
break;
case 5:
CardNumber = "Five" ;
break;
case 6:
CardNumber = "Six" ;
break;
case 7:
CardNumber = "Seven" ;
break;
case 8:
CardNumber = "Eight" ;
break;
case 9:
CardNumber = "Nine" ;
break;
case 10 :
CardNumber = "Ten" ;
break ;
case 11:
CardNumber = "Jack" ;
case 12:
CardNumber = "Queen" ;
case 13:
CardNumber = "King" ;
break;
case 14:
CardNumber = "Ace" ;
break;
default:
CardNumber = "ERROR" ;
break;
}
return CardNumber + "Of " +cardSuit.toString() ;
}
}
BlackJack Class
package bag;
import java.util.Scanner;
public class BlackJack {
private static ArrayBag Deck = new ArrayBag();
private Card[] mycard ;
//private static Card Card = new Card();
public static void main(String[] args) {
boolean play = true ;
boolean winner = false ; // bool win
int money = 100 ; // amount of money
int bet = 0 ; // amount user bets
Scanner input = new Scanner(System.in);
Deck.add(3);
System.out.println(Deck.isEmpty());
System.out.println(Deck.contains(3));
System.out.println(Deck.toArray());
}// end main
void shuffle(){
for(int i = 1; i <= 52 ; i++){
System.out.println(Deck.add(i));
}
}
void drawCard(){
while(!Deck.isEmpty()){
} // end while not empty
} // draw card
}// end class
A simple google search can be your best friend: http://math.hws.edu/eck/cs124/javanotes4/c5/ex-5-5-answer.html