#Logic error pt2
1 messages · Page 1 of 1 (latest)
While you are waiting for getting help, here are some tips to improve your experience:
If nobody is calling back, that usually means that your question was not well asked and hence nobody feels confident enough answering. Try to use your time to elaborate, provide details, context, more code, examples and maybe some screenshots. With enough info, someone knows the answer for sure.
Don't forget to close your thread using the command </help-thread close:1027500463647621170> when your question has been answered, thanks.
Here is my current code: https://paste.ofcode.org/C3u2jBPJkSvuSULHX2DRfK
With the test file:
import java.util.Arrays;
public class Test {
public static void main(String[] args) {
AbstractCard[] cards = {new Card1("QD"),new Card1("9H"),new Card1("JD"),new Card1("AD")};
int n = cards.length;
boolean swapped = true;
for (int i = 0; i < n - 1 && swapped; i++) {
swapped = false;
for (int j = 0; j < n - i - 1; j++) {
if (cards[j].compareTo(cards[j + 1]) > 0) {
// swap elements
AbstractCard temp = cards[j];
cards[j] = cards[j + 1];
cards[j + 1] = temp;
swapped = true;
System.out.printf("it : %s\n", Arrays.toString(cards));
}
}
}
}
} ```
I get the output:
it : [QD, JD, 9H, AD]
it : [QD, JD, AD, 9H]
it : [JD, QD, AD, 9H]
But the correct output is:
initial: [QD, 9H, JD, AD]
it 1: [QD, JD, AD, 9H]
it 2: [JD, QD, AD, 9H]
it 3: [JD, QD, AD, 9H]
i don't have much time today, but like i told u yesterday, the output is correct, your problem is that your not doing 4 steps but 3
Yea, ive been trying to figure out since yesterday what you mean by "u have a solution in card 2 for the numerical sorting, apply it to card 1"
but when i compare the two classes im not sure where the major difference is
card 2 sorts in order ascending, but has a limit to jack ( the 11 and higher bit )
your assignment for card 1 is the same sorting, no limit
right
and incase of a double hearts or whatever, u need to add a layer of sorting
u can copy the code from card2 and u need to alter it for card1
what method exactly would that be? compareTo?
copy it over, remove the limit on jack or higher and then see it it sorts correctly ( take note it will no sort the doubles yet )
ok let me check
strange that i know your code already from heart :p
to compare things, depending on what your usuing, its mostly equals or compareTo so pick your flavour
nothing silly, i'm just going to say that u did not read your assigment
class Card1 extends AbstractCard {
private static final String[] SUIT_ORDER = {"D", "C", "H", "S"};
private static final String[] RANK_ORDER = {"2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A"};
public Card1(String card) {
super(card);
}
@Override
public int compareTo(AbstractCard otherCard) {
if (otherCard instanceof Card2) {
throw new IllegalArgumentException("Cannot compare Card2 to Card1");
}
int suitIndex1 = getIndexForSuit(this.suit);
int suitIndex2 = getIndexForSuit(otherCard.getSuit());
if (suitIndex1 != suitIndex2) {
return suitIndex1 - suitIndex2;
}
int rankIndex1 = getIndexForRank(this.rank);
int rankIndex2 = getIndexForRank(otherCard.getRank());
return rankIndex1 - rankIndex2;
}
private int getIndexForSuit(String suit) {
for (int i = 0; i < SUIT_ORDER.length; i++) {
if (SUIT_ORDER[i].equals(suit)) {
return i;
}
}
throw new IllegalArgumentException("Invalid suit: " + suit);
}
private int getIndexForRank(String rank) {
for (int i = 0; i < RANK_ORDER.length; i++) {
if (RANK_ORDER[i].equals(rank)) {
return i;
}
}
throw new IllegalArgumentException("Invalid rank: " + rank);
}
@Override
public final String toString() {
return this.rank + this.suit;
}
@Override
public int getValue() {
int rankValue = getIndexForRank(this.rank) + 1;
return Math.min(rankValue, 10);
}
}```
@Override
public int compareTo(AbstractCard otherCard) {
if (otherCard instanceof Card1) {
throw new IllegalArgumentException("Cannot compare Card1 to Card2");
}
int rankIndex1 = getIndexForRank(this.rank);
int rankIndex2 = getIndexForRank(otherCard.getRank());
return rankIndex1 - rankIndex2;
}```
your sorting is based on a template , so the code should be the same, only the order in the template changes
the thing u need to add is that IF the items are the same, then a sorting needs to be done based on suits
right, and i also need to add getIndexForSuit code in there
yes ( and no at the same time, but your not at that level of thinking yet )
what does that even mean
remember yesterday when u started overthinking
i doubt that will be the case today (maybe a little bit)
here ( and which is normal due to not really being into coding yet ), u have multiple methods of getting an index of something
and they all share the same code ( which is not good, and i called a code smell )
yea well, the task i have doesnt require coding at all
so not going to elaborate much on it, but once u get your code working, have a good look on your index methods, and then u will notice that u can do all that with just a single method
but i kinda need to since my code tracking is bad
public int compareTo(AbstractCard otherCard) {
if (otherCard instanceof Card2) {
throw new IllegalArgumentException("Cannot compare Card1 to Card2");
}
int rankIndex1 = getIndexForRank(this.rank);
int rankIndex2 = getIndexForRank(otherCard.getRank());
return rankIndex1 - rankIndex2;
}
private int getIndexForSuit(String suit) {
for (int i = 0; i < SUIT_ORDER.length; i++) {
if (SUIT_ORDER[i].equals(suit)) {
return i;
}
}
throw new IllegalArgumentException("Invalid suit: " + suit);
}```
milly, i'm not going to hold your hand in how to do it 😉
u got the code -working- in card2, copy it over and alter it so it works on card 1
then apply the condition to handle the sorting when a double is found
i did the copying card2 on card1 and alter it no?
you talking about suits code
im talking about the sorting method u made last night that orderd the cards
card 1 and 2 use the same fashion on sorting
the order of sorting changes, that is correct, but since u made that based on a string index, it does not matter as the string index defines the order. The flow is identical
what is the difference between Card1 and Card2 ?
the sorting order and card1 needs to sort on suit aswell, card 2 sorts on ace till jack and then returns equals, the rest is identical
( basic ascending sorting )
Ok, im gonna take baby steps again...
This is the orignal compareTo from Card1 I had:
@Override
public int compareTo(AbstractCard otherCard) {
if (otherCard instanceof Card2) {
throw new IllegalArgumentException("Cannot compare Card2 to Card1");
}
int suitIndex1 = getIndexForSuit(this.suit);
int suitIndex2 = getIndexForSuit(otherCard.getSuit());
if (suitIndex1 != suitIndex2) {
return suitIndex1 - suitIndex2;
}
int rankIndex1 = getIndexForRank(this.rank);
int rankIndex2 = getIndexForRank(otherCard.getRank());
return rankIndex1 - rankIndex2;
}```
This is my current compareTo for Card1 but and I need to add the code for rank and return i:
```java
public int compareTo(AbstractCard otherCard) {
if (otherCard instanceof Card2) {
throw new IllegalArgumentException("Cannot compare Card1 to Card2");
}
int rankIndex1 = getIndexForRank(this.rank);
int rankIndex2 = getIndexForRank(otherCard.getRank());
return rankIndex1 - rankIndex2;
}```
what is the difference between Card1 and Card2 @humble flame ?
the heck is that
just specification on card1 and card2. card1 has suits and ranks we need to compare. card2 has only ranks we need to compare
Card2 is working for me somewhat. But card1 im having problems with
public int compareTo(AbstractCard otherCard) {
if (otherCard instanceof Card2) {
throw new IllegalArgumentException("Cannot compare Card2 to Card1");
}
int suitIndex1 = getIndexForSuit(this.suit);
int suitIndex2 = getIndexForSuit(otherCard.getSuit());
if (suitIndex1 != suitIndex2) {
return suitIndex1 - suitIndex2;
} else {
int rankIndex1 = getIndexForRank(this.rank);
int rankIndex2 = getIndexForRank(otherCard.getRank());
if (rankIndex1 != rankIndex2) {
return rankIndex1 - rankIndex2;
} else {
// if suits and ranks are the same, compare based on string value
return this.toString().compareTo(otherCard.toString());
}
}
}```
@ornate palm