Question:
How do I assign more than one value to an int in JAVA?
pinkbubbles
2011-07-27 11:57:45 UTC
So I'm supposed to make a card class. It says to the int is supposed to be numbers from 1-13. How do I declare that(w/o arrays)?
Seven answers:
?
2011-07-27 12:20:52 UTC
You can't.



An int is one and only one number, ranging from -2^31 to 2^31-1 (unless it's unsigned).



You may have misunderstood the assignment; I'm guessing it means that the class has one integer, which is in the range of 1-13. You just implement a check that the int is in that range and you're done.
peteams
2011-07-27 12:20:20 UTC
You question is unclear, you could mean it has the values at different times, as in:



for (int i = 1 ; i <= 13 ; i++)

{

// i has the value 1-13 here.

}



Alternatively it might mean you want to store in the int whether the number 1-13 is present. You can do that by using the bitwise operators that treat numbers like an array of 1 or 0 bits.



bits |= 1 << n; // Adds n to the integer bits.

bits &= ~(1 << n); // Removes n from the interger bits.

if ((bits & (1 << n)) != 0) // Tests if n is in the integer bits
?
2016-10-05 08:43:36 UTC
there's a modulus operator, represented via %, for ending up this sort of activity. relatively, with 2 integers a and b, the integer fee of ap.c.b is the rest you get while dividing a via b. So working example, 10p.c.3 is a million, on the grounds that 3 is going into 10 as much as 3 situations to make 9 and leaves a million left over. If a divides gently via b, then the end results of ap.c.b ought to be 0. So on your case, with integers i and x, you will possibly positioned some thing like this afterwards: boolean isOk = (ip.c.x==0); on the grounds that ip.c.x supplies an integer fee, and comparing that integer fee with 0 making use of == supplies a boolean consequence which would be saved in a boolean variable. EDIT: the countless different solutions above are giving inefficient code. you do no longer would desire to try this: boolean isOk = (ip.c.x==0?real:fake) or this: if(ip.c.x==0) { isOk=real; } else { isOk=fake; } the two one among them are inefficient and greater durable to verify and comprehend. via fact the == operator is already returning a boolean fee, you may assign that fee directly to a boolean variable.
green meklar
2011-07-27 18:34:56 UTC
In this case, the int in question will be declared as a non-static member variable inside the card class. That means that every instance of the card class will have its own variable with that name, and in these different instances, the variable may have different values. For instance, let's say I declared the card class like this:



public class card

{

public int a;

public card()

{

a=1;

}

}



Then elsewhere, if I put the following code:



card x=new card();

card y=new card();

card z=new card();

y.a=5;

z.a=13;

System.out.print(""+x.a+", "+y.a+", "+z.a+"\n");



and executed it, the following would be printed to the console:



1, 5, 13



See how that works?
Jim Maryland
2011-07-27 12:29:45 UTC
You can't assign a single int variable more than one value. I'm guessing your teacher is asking you to make a class that can take a value between 1 & 13. I've given you a class below that can handle it. Likely you'll want to make a Deck class that would initialize the PlayingCard objects. If you wanted multiple cards, I'd use something like this in your main method:



List playingCards = new ArrayList();



// Not zero based this time since we want between 1 & 13

for (int i = 1; i < 14; i++) {

PlayingCard playingCard = new PlayingCard(i, "heart");

playingCards.add(playingCard);

}



This would initialize a templated list of PlayingCard objects.







public class PlayingCard {

private int cardNumber = 0;

private String cardSuit = null;



/**

* Default constructor made private so callers are forced to use parameterized version.

*/

private PlayingCard() {



}



/**

* Parameterized constructor to create and set the PlayingCard object

* @param cardNumber

* @param cardSuit

* @throws Exception

*/

public PlayingCard(int cardNumber,String cardSuit) throws Exception {

try {

setCardNumber(cardNumber);

setCardSuit(cardSuit);

} catch (Exception e) {

throw e;

}

}



/**

* Sets the card number. Values must be between 1 & 13

* @param cardNumber

* @throws Exception

*/

public void setCardNumber(int cardNumber) throws Exception {

if (cardNumber < 1 || cardNumber > 13) {

throw new Exception ("Card number out of range. Use values between 1 and 13.");

}

this.cardNumber = cardNumber;

}



/**

* Returns the number value of the card

* @return

*/

public int getCardNumber() {

return cardNumber;

}



/**

* Sets the card suit. Valid values are heart, diamonds, clubs, or spades.

* @param cardSuit

* @throws Exception

*/

public void setCardSuit(String cardSuit) throws Exception {

if (null == cardSuit || cardSuit.length() == 0) {

throw new Exception ("Must specify a card suit");

}

if (!cardSuit.equalsIgnoreCase("hearts") &&

!cardSuit.equalsIgnoreCase("diamonds") &&

!cardSuit.equalsIgnoreCase("clubs") &&

!cardSuit.equalsIgnoreCase("spades")) {

throw new Exception ("Card suit must be hearts, diamonds, clubs, or spades");

}



this.cardSuit = cardSuit;

}



/**

* Returns the card suit.

* @return

*/

public String getCardSuit() {

return cardSuit;

}

}
Gardner
2011-07-27 12:16:59 UTC
You declare it as an integer and then you write your code to insure that the value falls between those ranges. You can't declare it with a preset range.
deonejuan
2011-07-27 12:30:46 UTC
You are supposed to make a Card class.



public class Card {

private String suit;

private int rank;

static String[] value = {"Ace", "One", ..."Jack", "Queen", "King"};

static String[] suits = {"Clubs", "Hearts", "Diamonds", "Spades"};

public Card( int suit, int rank ) {

this.suit = suit;

this.rank = rank;

}

public String toString() {

return "The " + value[ rank ] + " of " + suits[ suit ];

}

}



// then make a driver class

public class Deck {

public static void main( String[] args ) {

Card[] deck = new Card[ 52 ];

int count = -1;

for( int i = 0; i< 4; i++)

for( int j = 0; j<13; j++)

deck[ ++count ] = new Card( i, j );



for( int i = 0; i< deck.length; i++)

System.out.println( deck[ i ] );



}

}


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...