Ok, so my problem right now is that my teacher is asking us to make 3 classes. One class (PlayingCard) returns a rank and suit for each card, which I got working. The other one, deckofplayingcards, is supposed to use 2 instance variables of an array of 52 playingcard objects, and index of the top card in the deck. My constructor is giving me problems though. I pass my values back from PlayingCard as a string, and I need to declare an array of unique object cards as these strings. It is not working, to say the least. I have attached the photo of what he asks for specifically. H.W. assignment descirption
My problem now is, that I cannot make an array of objects (in DeckOfPlayingCards at the end of the code) equal to a string (which is generated in the method finalValue in PlayingCard). Any help please? Also, any other corrections or tips for my H.W./code would be appreciated. Here is my code. It would return when working correctly something like 1 of hearts. PlayingCard:
public class PlayingCard {
private String suit;
private String rank;
public String getSuit(String suitCounter){
String newSuit = null;
switch(suitCounter){
case("S"):
newSuit = "Spades";
break;
case("H"):
newSuit = "Hearts";
break;
case("C"):
newSuit = "Clubs";
break;
case("D"):
newSuit = "Diamonds";
break;
}
suit = newSuit;
return suit;
}
public String getValue(int valueCounter){
String newValue = null;
switch(valueCounter){
case(1):
newValue = "Ace";
break;
case(2):
newValue = "2";
break;
case(3):
newValue = "3";
break;
case(4):
newValue = "4";
break;
case(5):
newValue = "5";
break;
case(6):
newValue = "6";
break;
case(7):
newValue = "7";
break;
case(8):
newValue = "8";
break;
case(9):
newValue = "9";
break;
case(10):
newValue = "10";
break;
case(11):
newValue = "Jack";
break;
case(12):
newValue = "Queen";
break;
case(13):
newValue = "King";
}
rank = newValue;
return rank;
}
public String finalValue(String passedSuit, int passedValue){
String newSuit = getSuit(passedSuit);
String newValue = getValue(passedValue);
return newValue + " of " + newSuit
}
}
DeckOfPlayingCards
public class DeckOfPlayingCards {
PlayingCard[] card = new PlayingCard[52];
PlayingCard cardTop = new PlayingCard();
DeckOfPlayingCards(){
cardTop = card[0];
String randomNumberResult=null;
String randomNum1 = null;
String newval=null;
int randomNumGen1 = (int)(Math.random() * (4-1))+1;
if(randomNumGen1==1){
newval = "H";
}
else if(randomNumGen1==2){
newval = "S";
}
else if(randomNumGen1==3){
newval = "C";
}
else if(randomNumGen1==4){
newval = "D";
}
randomNum1 = newval;
int randomNumGen2 = (int)(Math.random() * (13-1))+1;
int randomNum2 = randomNumGen2;
PlayingCard finalVal = new PlayingCard();
for(int i=0; i<card.length; i++){
card[i] = finalVal.finalValue(randomNum1, randomNum2);
}
}
}
Aucun commentaire:
Enregistrer un commentaire