Java Deck Of Cards And Dealing Five Cards
Aug 2, 2014
Basically I started writing my code in one class, then I split it up into a Card class and a DeckOfCards class and I now need to figure out how to get it all to work together. I get a little confused with calling methods sometimes, especially when separate classes are in play. I think I just need a method to deal out . Besides getting it all working together correctly. I'm also having trouble creating the method to deal out five cards that also tells how many cards are left in the deck and I believe I need a toString method but I honestly do not know how to go about that.
Design and implement a class called Card that represents a standard playing card. Each card has a suit and a face value. Then create a class called DeckOfCards that stores 52 objects of the Card class. Include methods to shuffle the deck, deal a card and report the number of cards left in the deck. The shuffle methods should assume a full deck. Create a driver class (CardsGame) with a main method that deals five cards from the shuffled deck, printing each card as it is dealt. Make sure to write the appropriate constructors, getters, setters, toString and other methods as required for both classes.
The main class, CardsGame Class
import java.util.Scanner;
public class CardsGame {
public static void main (String [] args) {
DeckOfCards deck = new DeckOfCards();
//call shuffle
deck.shuffle();
[Code] ....
View Replies
ADVERTISEMENT
Aug 2, 2014
I have an assignment for my summer class. Basically I started writing my code in one class, then I split it up into a Card class and a DeckOfCards class and I now need to figure out how to get it all to work together. I get a little confused with calling methods sometimes, especially when separate classes are in play. I think I just need a method to deal out . Besides getting it all working together correctly. I'm also having trouble creating the method to deal out five cards that also tells how many cards are left in the deck and I believe I need a toString method but I honestly do not know how to go about that. FYI, I think the prof would rather arrays then enums since we're dealing with arrays right now hence the array for the 52 card deck.
Here are the directions...
Design and implement a class called Card that represents a standard playing card. Each card has a suit and a face value. Then create a class called DeckOfCards that stores 52 objects of the Card class. Include methods to shuffle the deck, deal a card and report the number of cards left in the deck. The shuffle methods should assume a full deck. Create a driver class (CardsGame) with a main method that deals five cards from the shuffled deck, printing each card as it is dealt. Make sure to write the appropriate constructors, getters, setters, toString and other methods as required for both classes.
The main class, CardsGame Class
Java Code:
import java.util.Scanner;
public class CardsGame {
public static void main (String [] args) {
DeckOfCards deck = new DeckOfCards();
//call shuffle
deck.shuffle();
[code]...
View Replies
View Related
Aug 3, 2014
I redid my entire code to use array-lists instead of just arrays my professor finally got back to me and said he doesn't want us to use lists. The assignment is to create a CardGame driver class, then create a "card" in a card class, then a "deck of cards" in a DeckOfCards class, shuffle, and deal 5 random cards and "deal a card and report the number of cards left in the deck". That last line in quotes is what I do not know how to do.
Also, I renamed a lot of variables via some suggestions and the assignments states "Make sure to write the appropriate constructors, getters, setters, toString and other methods as required for both classes." I think they're appropriate. Should I change some of he methods to better getters and setters identifiers?
Here is my code.
import java.util.Random;
public class CardsGameTest {
//execute application.
public static void main(String[] args) {
DeckOfCards myDeck = new DeckOfCards();
myDeck.shuffle(); //shuffle cards.
[code].....
The instructions also say to "print each card as it is dealt" does that mean 5 cards one at a time? Anyways, I was thinking that in the driver class I could add a for loop to the for loop and as it deals a card it could run through the second for loop and print how many cards are left in the deck.
View Replies
View Related
Sep 14, 2014
I have two questions.
1 - I don't understand why I'm getting an empty stack error when calling the removecard method in my Table class line 13?
2 - I am trying to use a constructor to populate my deck of cards instead of a method when creating my theDeck object in my Table class line 11 but I get the following error:
java.lang.StackOverflowError
at java.util.Vector.<init>(Unknown Source)
at java.util.Vector.<init>(Unknown Source)
at java.util.Stack.<init>(Unknown Source)
at Deck.<init>(Deck.java:7)
at Deck.<init>(Deck.java:34)
public class Card {
String enseigne;
int valeur;
[Code]....
View Replies
View Related
May 30, 2014
Assuming we have an array that is int[] cardDeck = new int[52] and each card is given a value with the following method; this deck does not include Jokers. Note: 11, 12, and 13 and used to represent Jacks, Queens, and Kings respectively, and 1 is used to represent Aces.
Java Code:
public static void loadDeck(int[] cardDeck)
{
for(int i = 0; i < cardDeck.length; i++)
{
if(i+1 > 39)
cardDeck[i] = i+1-39;
else if(i+1 > 26)
[Code] ....
View Replies
View Related
Mar 12, 2014
I have red underlines under "Card" and "cards" in my Deck class and "deal" in my driver and i don't know how to fix them to get it to properly run.
Card Class:
Java Code:
public class Card
{
public final static int ACE = 1; // note: or use enumerated types
public final static int TWO = 2;
public final static int THREE = 3;
public final static int FOUR = 4;
public final static int FIVE = 5;
public final static int SIX = 6;
public final static int SEVEN = 7;
[code]...
View Replies
View Related
Sep 30, 2014
I am trying to make a program that will make a deck of 52 cards, but not shuffle it. I can't get rid of the errors in the code that I have so far.
Java Code:
public class Deck {
private Card[] deck;
private int cardsUsed;
public Deck() {
deck = new Card[52];
int cardCt = 0;
for ( int suit = 0; suit <= 3; suit++ ) {
for ( int value = 1; value <= 13; value++ ) {
deck[cardCt] = new Card(value,suit);
cardCt++;
}
}
cardsUsed = 0;
}
} mh_sh_highlight_all('java');
I am getting red squiggly lines on lines:
#3 under "Card"
#8 under "deck" and "Card"
#12 under "deck" and "Card"
View Replies
View Related
May 15, 2014
I am trying to assign random numbers to a deck of cards without repeating the numbers. What am I doing wrong?
package hokm;
import java.util.Random;
import java.util.Scanner;
public class Hokm
{
public static void main(String[] args) {
int [][] number=new int[52][2];
[Code] .....
View Replies
View Related
Feb 12, 2014
I am creating a deck of 52 cards each with their rank(1 2 3...King, Ace) and suit(Clubs... Spades).
I want to be able to print out value of each card after creating them such as - 2 of CLUBS and so on. I am able to do that now. Only problem is, when it comes to the picture cards, I am representing them with numbers 11(Jack), 12(Queen) 13(King) and 14(Ace). I can't quite get around to changing the numbers to be represented as the actual words (King and so on).
I imagine that I should store the jack,queen,king,ace values in an enum too and call it in my loop but can't quite get it. Should I have like a 3rd for loop in my following codes to make it work?
Java Code:
//My main method. The Card class follows below
public static void main(String[] args) {
Card[] cards = new Card[52];
int i = 0;
for (Card.Suits suit : Card.Suits.values()) {
for (int y = 2; y < 15; y++) {
cards[i] = new Card(y, suit.name());
i++;
[Code] .....
View Replies
View Related
Jul 28, 2014
"In this assignment you will use an applet to display images of playing cards. The applet should load a deck of 52 playing card images from the "images" folder that you downloaded. The applet should shuffle the deck (use a random number generator) and display the first 10 cards of the shuffled deck. Display the cards in two rows of five cards each."
That is my goal for this assignment. I've got my code compiling and I will post it below and I've got an html page but when I try to open it I get an error
import java.applet.Applet;
import java.awt.Graphics;
import java.awt.Image;
import java.util.Random;
public class Assignment12 extends Applet {
Image card1;
Image card2;
[Code] ....
View Replies
View Related
Aug 18, 2014
So I'm trying to make an applet that displays images of cards. The applet has a deck of 52 playing card images. It should shuffle the deck with a random number generator and display the first 10 cards of the shuffled deck.
With this code I have about 100 errors and I'm not sure what I'm doing wrong: I know I have to use arrays in order to ultimately display the images, but do I have to create Image objects for the Image [] cards array? I am getting many "class, interface, enum expected" errors as well as others.
import java.util.Random;
import java.awt.Image;
import java.applet.Applet;
import java.awt.Graphics;
public class unit12 extends Applet
{
[Code] .....
View Replies
View Related
Feb 24, 2015
I have a string of variable tags (tag) filled with variables, and using a Scanner (scan) to manipulate and generate data dictionary add cards for each. The variables are delimited with new line character, and the variables are structured such that the actual name of the variable contains info as to its data type, size, etc. What I need to do is move the scanner to a particular point into the string then use .nextInt() or similar. I don't want to do this to the whole variable, just where I start it from. How can I do this?
E.g..
/* variables in String tag...
ANS_s20Tx12VDstr
ANS_:Tx67MSvis
ANS_zRx11MSctr
...
..
*/
Scanner scan = new Scanner( tag );
int ptr = tag.indexOf( "_" );
ptr++;
if ( Character.toLowerCase( tag.charAt( ptr ) ) == 's' )
// then start at the "_" character and grab the nextInt()
View Replies
View Related
Apr 8, 2015
i'm trying to make a random card shuffler but the output would sometimes have same value multiple times. For example it might print out A5 at the fifth index then print out A5 again as the 32 index.
public class randomGenerateCards {
public static void main(String[] args) {
String temp;
String[] cards = new String[]
{ "A1", "A2", "A3", "A4", "A5", "A6", "A7", "A8", "A9", "A10", "A11",
"A12", "A13", "S1", "S2", "S3", "S4", "S5", "S6", "S7", "S8",
"S9", "S10", "S11", "S12", "S13", "H1", "H2", "H3", "H4", "H5",
"H6", "H7", "H8", "H9", "H10", "H11", "H12", "H13", "D1", "D2",
"D3", "D4", "D5", "D6", "D7", "D8", "D9", "D10", "D11", "D12",
[code]....
View Replies
View Related
Dec 5, 2014
I curious for tips for moving forward within my current code for my counting cards GUI interface. I need two labels one for the card deck, and the other for the randomized card face. When the card face shows, in the count value text field the value of the card is added. For each card this is to happen through until the whole deck. There is also a card count text field to count how many cards have been dealt out. Now with that being said, I am being held back by getting the two labels to show in my GUI, the buttons show, but I need getting the cards and its value to show and initialize, with the card count in the card count text field.
import java.lang.Math;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class CardsGui extends JFrame
[Code] ....
View Replies
View Related
Apr 27, 2015
I have tried to add a scroll bar line 28 - 31 and can't figure out why its not working. Its not even showing the scroll bar!
public class TheFrame extends JFrame {
private static ThePanel canvas;
private String deckImagesToUse;
/**
* The constructor creates a Frame ready to display the cards
*/
public TheFrame(String cardImgFile) {
[Code] .....
View Replies
View Related
Aug 18, 2014
So I'm trying to make an applet that displays images of playing cards. The applet should load a deck of 52 playing card images (the folder is called "images"). The applet should shuffle the deck (using a random number generator) and display the first 10 cards of the shuffled deck. I then have to display the cards in two rows of five cards each.
With this code I have about 100 errors and I'm not sure what I'm doing wrong:
import java.util.Random;
import java.awt.Image;
import java.applet.Applet;
import java.awt.Graphics;
public class unit12 extends Applet
{
Image card1, card2, card3, card4, card5, card6, card7, card8, card9, card10;
[Code] ....
View Replies
View Related
Nov 27, 2014
I am trying to sort the 5 cards in decreasing order and this is exactly what my professor gave us but it has errors.
int pos;
int max;
for (pos = 0; pos < hand.length; pos++){
max = pos;
for (int i = pos + 1; i < hand.length; i++){
if (hand[max] < hand[i]){
max = i;
}
}
if (max != pos){
int temp = hand[pos];
hand[pos] = hand[max];
hand[max] = temp;
}
}
}
Line 6 has an error with the < operator.
Line 11&13 says cannot convert from card to Int.
This method is passing in Card[] hand
View Replies
View Related
May 9, 2014
Part of my app is to provide an interface to an operator to manage multiple printers of smart cards.
A JSP page displays all printers and each printer has a print button.
I want the operator to press several buttons and each button pressed starts a thread.
Threads should run in parallel and pressing a button does not cancel the processing of the previous button.
View Replies
View Related
Feb 18, 2014
i am trying to store a number of different value cards for a number of players. E.g there can be 2 , 4 or more players and each player can have any number of cards.
I have decided to use an arraylist for cards and tried using an array for players. But after coding and reading some information online, i realised that it is not possible to have an array of arraylist.
View Replies
View Related
Oct 29, 2014
You should write a class that represents a circle object and includes the following:
1. Private class variables that store the radius and centre coordinates of the object.
2. Constructors to create circle objects with nothing supplied, with just a radius value supplied and with a radius and centre coordinates supplied.
3. Public instance methods that allow the radius and centre coordinates to be set and retrieved (often known as set/get methods).
4. Public instance methods that return the circumference and area of the circle.
5. A public class method that tests if two circle objects overlap or not
Here is my code:
import java.lang.Math;
public class Circle {
private double xCentre, yCentre, Radius;
// constructors
public Circle() {
xCentre = 0.0;
yCentre = 0.0;
Radius = 1.0;
[Code] ....
The second program just has to demonstrate each aspect addressed in the brief I pasted at the top.s of what else I can do.
This is as far as I got during the 3 hour lab session I had and both compiled fine but when running just displayed all the text eg. "Circumference of first circle is ", but didn't display any numeric values. I don't have the facilities to actually run the program unless I'm in the computer lab, I have a short opportunity to go in tomorrow but that will be the last so I'm doubtful that I'll get it fully working in time.
The problem is that when this code runs it doesn't display any numerical values, ie nothing is being passed between the two programs.
View Replies
View Related
Oct 4, 2014
I am working with jsoup right now and I am trying to get a range of file extensions such as doc, docx, txt, pdf, and so on. Anyways i have looked through the jsoup api and cant seem to find what I am looking for. The closest thing I have found is
Elements files = doc.select("a[href$=.doc]");
This should work but only grabs one extension. How to grab multiple at one time? My guess would be:
Elements files = doc.select("a[href$=.doc]").select("a[href$=.docx]").select("a[href$=.txt]");
However I'm not sure for certain. Anyways I will provide a link for the api .....
View Replies
View Related
Apr 11, 2014
I am trying to solve involves an object array each object has two instance variables, a string(origin) and a double(price), that was originally unsorted I have already sorted the array. There are a total of 11 possible origins and 6 prices, so obviously some origins share prices with others. The array is sorted by origin alphabetically ignoring case. I now have to write to a file how many instances of each origin there is, tally up all the prices for each origin and a total for all prices. I get bonus points for utilizing the presorted array to my advantage for the final output and calculations.
It would be easy to write a branching statement and tally up all the origins and prices etc... However this is a linear operation and doesn't seem to benefit from the presorted array at all so I came up with what I think is a descent alternate solution.
I was thinking that I could go through the array one object at a time and stop when I came to an origin of the next type. For example: there are 12 names called "Adam" and then a name called "Bob". So if I stop at bob, check the index and add 1 then I know how many Adams there were. I can then continue this process with all the other names, all the while calculating the combined prices and all.
View Replies
View Related
Feb 20, 2014
I'm new to Java and I'm trying to create a spanning tree in the desired order with 4 nodes (0,1,2,3). I've got the following code:
import java.util.ArrayList;
public class A {
public static boolean any(ArrayList<Integer> input) //To check for if any element in the ArrayList == 1
{
boolean answer = false;
for(int i=0;i<input.size();i++) {
if(input.get(i)==1)
[Code] ....
What happens is that the input parameter adj and hence the original adjmat inside main gets changed everytime I enter the method "connected", and I don't want this to happen. I understand that this is due to the main method being static and hence adjmat becomes static as well, but how do I change the code such that adjmat only gets modified after coming out of the connected function, and not while inside it?
View Replies
View Related
Oct 5, 2014
I am working on a small brain teaser project where I am taking a string input from a Scanner, and turning into ascii. The problem comes into play when the string has a space in it, so if the question is what's your name? and you say Michael Jackson, Michael gets converted then Jackson becomes the answer to the next question, rather then the second portion of the current string.
This is an older version of what I'm doing currently, but it had the same basic problem with spaces.I will say I did my current version entirely different.
nner user_input = new Scanner (System.in);
//Creates a string
String favoriteFlick;
System.out.println("Enter the title of your favorite film?");
favoriteFlick = user_input.next();
[Code] .....
View Replies
View Related
Jun 6, 2014
I am trying to create an array filled with the object Card. It keep throwing the exception that the "deck is empty". I am not sure why that's happening because the code for filling the array seems fine.
public class Deck {
private Card[] deck;
private CardPile cardPile;
private int numCards;
public Deck() throws InvalidDataException{
this.deck = new Card[52];
[Code] .....
View Replies
View Related
May 17, 2014
I created the below code and it works fine. It is a deck card game. I just want to divide my code into classes so that it seems more organized.
Java Code:
package getimage;
import java.awt.BorderLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Collections;
import javax.swing.*;
[code]....
View Replies
View Related