Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 11-27-2007, 12:51 AM
Member
 
Join Date: Nov 2007
Posts: 8
mandrake446 is on a distinguished road
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
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 11-27-2007, 12:52 AM
Member
 
Join Date: Nov 2007
Posts: 8
mandrake446 is on a distinguished road
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);
}
}
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
help for payroll project in java mageshwari New To Java 2 04-09-2008 03:46 AM
UML to Java project banie NetBeans 3 01-28-2008 11:16 AM
Help with Java project ducster New To Java 11 12-03-2007 04:08 AM
Java Project Help Gambit17 New To Java 3 11-05-2007 12:53 PM
Building A Java Project In Eclipse JavaForums Eclipse 0 05-22-2007 10:34 PM


All times are GMT +3. The time now is 08:02 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org