Results 1 to 2 of 2
  1. #1
    mandrake446 is offline Member
    Join Date
    Nov 2007
    Posts
    8
    Rep Power
    0

    Default Help Java project.

    I know its horrible... I feel horrible about it; i'm still working on it but this Java project is killing me... Just so you know i dont have to know programing after this class, i just had a choice between 3 computer classes and picked the one i guess i'm worst at... I've done four major projects before this one. I average getting about 75% correct. We code in BlueJ...The reason i'm finally breaking down for help is it was due on November 21st. o well enough of my babble if ur going to help ur going to help; if not ur not.

    Heres the assignment



    Open the project cards containing your code for class PlayingCard and create the two new
    classes in this project space:

    Part 4: Implement class CardDeck that has the following declaration:

    public class CardDeck {
    private PlayingCard[] deck;
    public static final int DECK_SIZE = 52;
    //number of cards still in the deck:
    private int numCardsLeft;
    public CardDeck(){ ? }
    private void removeCard(int index){ ? }
    public void shuffle(){ ? }
    public void wholeDeck(){ ? }
    public PlayingCard topCard(){ ? }
    public PlayingCard anyCard() { ? }
    }

    In your implementation, you must follow these guidelines:

    1. Constructor of the class must properly initialize the CardDeck object: create the array object with number of "compartments" equal to DECK_SIZE and assign it to the field deck ; then create all 52 cards in such a way that there are no duplicate cards in the deck. You must use Java loop(s) to create the above PlayingCard objects. In addition, the constructor must assign the value DECK_SIZE to the field numCardsLeft.

    2. Method removeCard(int index) must first check that the index is between 0 and the value of numCardsLeft-1. If it is true, then the method must

    ? shift all the cards in the deck array starting with index+1 and up to
    numCardsLeft-1 position left by one "compartment"

    ? set the last value in the deck to null, like so:
    deck[numCardsLeft-1] = null;

    ? decrement value of numCardsLeft by 1

    3. Method shuffle()randomly re-arranges cards in the deck:

    a) Declare a local variable temp of the type PlayingCard

    B) Generate two random numbers i and j between 0 (inclusive) and DECK_SIZE
    (exclusive) and swap the two cards stored at these index positions in the array:
    temp = deck[i];
    deck[i] = deck[j];
    deck[j] = temp;
    Repeat step B) at least 50 times, to get a good shuffle!

    4. Method wholeDeck() must display descriptions of all the cards in the deck on screen. In order to do this, use an enhanced for loop to iterate through the deck array, and for each PlayingCard element of it call method describeCard() of class PlayingCard.

    5. Method topCard()calls removeCard with 0 passed as the parameter value, and
    returns a copy of PlayingCard object that was removed (that is, the card that used to be at index 0 in the array, before we removed it).

    6. Method anyCard() removes a PlayingCard object stored at a random index in the
    array: using an object of class java.util.Random, generate a random number between 0 and numCardsLeft-1, and pass this value as the parameter to the method
    removeCard. Method anyCard must return a copy of the of PlayingCard object that
    was removed.

    Part 5: Implement class CardDeckTest that contains only the method
    public static void main (String[] args) which does the following:

    ? create an object of type CardDeck

    ? call the method wholeDeck()

    ? call the method shuffle()

    ? call the method wholeDeck() again

    ? call the method topCard(), store its result in a variable, and then use this variable to call method displayCard()of class PlayingCard

    ? call the method anyCard(), store its result in a variable, and then use this variable to call method displayCard()of class PlayingCard

    demo programs:

    use proper indentations

  2. #2
    mandrake446 is offline Member
    Join Date
    Nov 2007
    Posts
    8
    Rep Power
    0

    Default

    My code for PlayingCard. Sorry its so sloppy, told u i'm not the greatest at this.

    public class PlayingCard
    {
    private int suit; // 1, or 2, or 3, or 4
    private int rank; // Ace = 1, J = 11, Q = 12, K = 13
    private boolean faceUp; // if value is true, card is facing up
    private String color; // "red" or "black"

    // names of four suits represented as integer constants
    public static final int Spades = 1;
    public static final int Diamonds = 2;
    public static final int Clubs = 3;
    public static final int Hearts = 4;

    //Constructors for objects of class PlayingCard
    public PlayingCard(int suit, int rank, boolean faceUp) {
    this.suit = suit;
    this.rank = rank;
    this.faceUp = faceUp;
    setColor();
    }

    public PlayingCard() { //no-arg constructor for testing
    this(Hearts, 12, true);
    }

    private void setColor(){
    switch(suit){
    case Spades:
    color = "black";
    break;
    case Diamonds:
    color = "red";
    break;
    case Clubs:
    color = "black";
    break;
    case Hearts:
    color = "red";
    break;}
    }

    // Accessor methods
    public int getSuit(){
    return suit;
    }
    public int getRank(){
    return rank;
    }
    public boolean isFaceUp(){
    return faceUp;
    }

    /**
    * Modifier for field faceUp
    */
    private void setFace(boolean up_or_dn){
    faceUp = up_or_dn;
    }

    public void flipCard(){
    if(faceUp = true){
    faceUp = false;
    }else if(faceUp = false){
    faceUp = true;}
    }

    /**
    * Print description of the card on screen
    */
    public void describeCard(){
    if(rank == 1)
    System.out.print("Ace of ");
    else if (rank > 1 && rank < 11)
    System.out.print(rank + " of ");
    else if (rank == 11)
    System.out.print("Jack of ");
    else if (rank == 12)
    System.out.print("Queen of ");
    else if (rank == 13)
    System.out.print("King of ");
    else
    System.out.print("Invalid rank; ");

    switch(suit){
    case Spades:
    System.out.println("Spades");
    break;
    case Clubs:
    System.out.println("Clubs");
    break;
    case Hearts:
    System.out.println("Hearts");
    break;
    case Diamonds:
    System.out.println("Diamonds");
    break;
    default:
    System.out.println("invalid suit");
    }
    }

    /** a method to display the image of the card on Canvas
    * @param x x-coordinate on canvas for image location
    * @param y y-coordinate on canvas for image location
    */
    public void displayCard(int x, int y){
    Canvas c = Canvas.getCanvas();
    String imageFileName = "";
    String rank = "";
    String suit = "";
    if(faceUp == false){
    imageFileName = "b1fv.png";}
    else {

    switch(this.suit){
    case 1:
    suit = "s";
    break;
    case 2:
    suit = "d";
    break;
    case 3:
    suit = "c";
    break;
    case 4:
    suit = "h";
    break;
    }
    switch(this.rank){
    case 1:
    rank = "1";
    break;
    case 2:
    rank = "2";
    break;
    case 3:
    rank = "3";
    break;
    case 4:
    rank = "4";
    break;
    case 5:
    rank = "5";
    break;
    case 6:
    rank = "6";
    break;
    case 7:
    rank = "7";
    break;
    case 8:
    rank = "8";
    break;
    case 9:
    rank = "9";
    break;
    case 10:
    rank = "10";
    break;
    case 11:
    rank = "j";
    break;
    case 12:
    rank = "q";
    break;
    default:
    rank = "k";
    break;}


    imageFileName = (suit + rank + ".png");
    }
    c.drawImage(imageFileName, x, y);

    c.wait(10);
    }
    }

Similar Threads

  1. help for payroll project in java
    By mageshwari in forum New To Java
    Replies: 2
    Last Post: 04-09-2008, 02:46 AM
  2. UML to Java project
    By banie in forum NetBeans
    Replies: 3
    Last Post: 01-28-2008, 10:16 AM
  3. Help with Java project
    By ducster in forum New To Java
    Replies: 11
    Last Post: 12-03-2007, 03:08 AM
  4. Java Project Help
    By Gambit17 in forum New To Java
    Replies: 3
    Last Post: 11-05-2007, 11:53 AM
  5. Building A Java Project In Eclipse
    By JavaForums in forum Eclipse
    Replies: 0
    Last Post: 05-22-2007, 09:34 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •