8 Replies - 536 Views - Last Post: 15 November 2012 - 03:24 PM Rate Topic: -----

#1 lobstilops  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 15-November 12

Generating Random Card, having difficulties generating random numbers?

Posted 15 November 2012 - 11:48 AM

 /**
 * @(#)Card.java
 *
 *
 * @author Evan JR
 * @version 1.00 2012/11/14
 */

import java.util.Random;

public class Card 
{ 
	private int suit;
	private int face;
	private String suitName;
	private String faceName;
	
	public Card()
	{	
		Random gen = new Random();
		int suit = gen.nextInt(4)+1;
		int face = gen.nextInt(13)+1;
		
	}
	
	
	public int getSuit()
	{
		return suit;
	}
	
	public int getFace()
	{
		return face;
	}

	public String faceName()
	{
		switch(face)
		{
			case 1: faceName = "Ace";
			case 2: faceName = "Two";
			case 3: faceName = "Three";
			case 4: faceName = "Four";
			case 5: faceName = "Five";
			case 6: faceName = "Six";
			case 7: faceName = "Seven";
			case 8: faceName = "Eight";
			case 9: faceName = "Nine";
			case 10: faceName = "Ten";
			case 11: faceName = "Jack";
			case 12: faceName = "Queen";
			case 13: faceName = "King";
		}
	return faceName;		
	}
	
	public String suitName()
	{
		switch(suit)
		{
			case 1: suitName = "Diamonds";
			case 2: suitName = "Clubs";
			case 3: suitName = "Hearts";
			case 4: suitName = "Spades";
		}
	return suitName;
	}
	
	public String getsuitName()
	{
		return suitName;
	}
	
	public String getfaceName()
	{
		return faceName;
	}
    
}




Hello there. I'm a fairly novice Java programmer, and I have a lab assignment due at my University. My prof isn't available as he's gone for a few days so I'm stuck to try and solve this problem myself.

I'm having difficulties with this random generator as my driver program below is giving me bizarre values.

Card@8746d23
Card@13a9192b
Card@42b36022
Card@e6c7a64
Card@73d742a1

Process completed.



Any help you can give me is greatly appreciated, I understand you cannot just simply give me all the answers. I need to figure this myself, but I'm stumped :(

Is This A Good Question/Topic? 0
  • +

Replies To: Generating Random Card, having difficulties generating random numbers?

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8332
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Generating Random Card, having difficulties generating random numbers?

Posted 15 November 2012 - 11:54 AM

It is because your Cartd class does not have a toString() method that says what to display when a Card object is used as a String. In that case in displays the address in memory of the Card object.

Try to add to your Card class

public String toString() {
   return "I am " + face + " of " + suit;
}


Was This Post Helpful? 0
  • +
  • -

#3 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1398
  • View blog
  • Posts: 3,097
  • Joined: 05-April 11

Re: Generating Random Card, having difficulties generating random numbers?

Posted 15 November 2012 - 12:05 PM

Your switch is all wrong. You will need to add a break after each case, else it will 'fall through' every case

switch(face) {
	case 1:
		faceName = "Ace";
		break;
	case 2:
		faceName = "Two";
		break;
	...
}


Was This Post Helpful? 1
  • +
  • -

#4 lobstilops  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 15-November 12

Re: Generating Random Card, having difficulties generating random numbers?

Posted 15 November 2012 - 12:06 PM

View Postpbl, on 15 November 2012 - 11:54 AM, said:

It is because your Cartd class does not have a toString() method that says what to display when a Card object is used as a String. In that case in displays the address in memory of the Card object.

Try to add to your Card class

public String toString() {
   return "I am " + face + " of " + suit;
}



Oh ok! Thank you that makes a lot of sense. I remember my prof mentioning toString methods a small bit, so that must be what he is talking about.

I hate to bother you but, now that that is working I'm having more difficulties. This is related to the random number generation. It is only generating zeros, five times in a row. I'm very confused as to the reason of this?

/**
 * @(#)CardDriver.java
 *
 *
 * @author EvanJR
 * @version 1.00 2012/11/14
 */

public class CardDriver {
        
    
    public static void main(String[] args) 
    	{
    		Card c1;
    		for (int i=1; i<=5; i++)
    		{
    			c1 = new Card();
    			System.out.println (c1);
    		}
    	
    		
        
    }
}




My driver program is giving me this:

Face value: 0, and Suit Value: 0
Face value: 0, and Suit Value: 0
Face value: 0, and Suit Value: 0
Face value: 0, and Suit Value: 0
Face value: 0, and Suit Value: 0

Process completed.



Can you notice anything wrong with the number generation? It should be getting a 1-4 for suit and 1-13 for face value. Thanks for anything you can do.
Was This Post Helpful? 0
  • +
  • -

#5 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1398
  • View blog
  • Posts: 3,097
  • Joined: 05-April 11

Re: Generating Random Card, having difficulties generating random numbers?

Posted 15 November 2012 - 12:11 PM

You are declaring local variables in your constructor
You should be using the instance variables instead
int suit = gen.nextInt(4)+1; //Local variable
suit = gen.nextInt(4)+1; //Setting the instance variable


Was This Post Helpful? 0
  • +
  • -

#6 lobstilops  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 15-November 12

Re: Generating Random Card, having difficulties generating random numbers?

Posted 15 November 2012 - 12:12 PM

View PostCasiOo, on 15 November 2012 - 12:11 PM, said:

You are declaring local variables in your constructor
You should be using the instance variables instead
int suit = gen.nextInt(4)+1; //Local variable
suit = gen.nextInt(4)+1; //Setting the instance variable



Oh goodness. I can't believe I didn't see that haha. Wow. Thank you very much! That was a really stupid mistake...
Was This Post Helpful? 0
  • +
  • -

#7 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7746
  • View blog
  • Posts: 13,104
  • Joined: 19-March 11

Re: Generating Random Card, having difficulties generating random numbers?

Posted 15 November 2012 - 12:36 PM

View PostCasiOo, on 15 November 2012 - 02:05 PM, said:

Your switch is all wrong.


Yes, you need breaks in your switch. But a switch is the wrong thing to use here. You've got a data lookup here, why are you not just looking up the data?

Declare an array:
String[] faceName = { "Ace",  "Two", "Three" ...} // you can fill in the values



Now your method is a little tidier:
public String faceName()
 	return faceName[face];
}


Same thing for suits, of course.

This post has been edited by jon.kiparsky: 15 November 2012 - 12:48 PM

Was This Post Helpful? 1
  • +
  • -

#8 baavgai  Icon User is online

  • Dreaming Coder
  • member icon

Reputation: 5831
  • View blog
  • Posts: 12,683
  • Joined: 16-October 07

Re: Generating Random Card, having difficulties generating random numbers?

Posted 15 November 2012 - 01:02 PM

You're using suitName and faceName as local variables in methods. Stop that right now.

Why +1? Computers start at zero. Don't fight it.

e.g.
import java.util.Random;

public class Card { 
	private static final Random gen = new Random();
	private final int suit, face;
	
	public Card() {
		// no, this is a local value
		// int suit = gen.nextInt(4)+1;
		// int face = gen.nextInt(13)+1;
		int n = gen.nextInt(52);
		// you want to set your instance variables
		this.suit = n/13;
		this.face = n%13;
	}
	
	
	public int getSuit() { return suit; }
	public int getFace() { return face; }

	public String getSuitName()
		if (suit==0) { return "Diamonds"; }
		if (suit==1) { return "Clubs"; }
		if (suit==2) { return "Hearts"; }
		return "Spades";
	}



Hope this helps.
Was This Post Helpful? 0
  • +
  • -

#9 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8332
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Generating Random Card, having difficulties generating random numbers?

Posted 15 November 2012 - 03:24 PM

View PostCasiOo, on 15 November 2012 - 03:05 PM, said:

Your switch is all wrong. You will need to add a break after each case, else it will 'fall through' every case

switch(face) {
	case 1:
		faceName = "Ace";
		break;
	case 2:
		faceName = "Two";
		break;
	...
}



didn't bother, fgaveName() is never called

And OP, btw, not really a good idea to have a method name and a variable name with the same name
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1