#Logic error pt2

1 messages · Page 1 of 1 (latest)

vital mantleBOT
#

<@&987246399047479336> please have a look, thanks.

vital mantleBOT
#

While you are waiting for getting help, here are some tips to improve your experience:

Code is much easier to read if posted with syntax highlighting and proper formatting.

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.

humble flame
#

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]

ornate palm
#

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

humble flame
#

but when i compare the two classes im not sure where the major difference is

ornate palm
#

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

humble flame
#

right

ornate palm
#

and incase of a double hearts or whatever, u need to add a layer of sorting

humble flame
#

so i need to add new code instead of modifying it

#

and that code comes from card2?

ornate palm
#

u can copy the code from card2 and u need to alter it for card1

humble flame
#

what method exactly would that be? compareTo?

ornate palm
#

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 )

humble flame
#

ok let me check

ornate palm
humble flame
#

lol

#

i also made a silly mistake of not being able to compare Card1 and Card2

ornate palm
#

to compare things, depending on what your usuing, its mostly equals or compareTo so pick your flavour

ornate palm
humble flame
# ornate palm to compare things, depending on what your usuing, its mostly equals or compareTo...
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);
    }
}```
ornate palm
#

yeah that's the same overpaste from yesterday

#

u only need compareTo

humble flame
#
   
 @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;
    }```
ornate palm
#

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

humble flame
#

right, and i also need to add getIndexForSuit code in there

ornate palm
#

yes ( and no at the same time, but your not at that level of thinking yet )

humble flame
#

what does that even mean

ornate palm
#

remember yesterday when u started overthinking

humble flame
#

i doubt that will be the case today (maybe a little bit)

ornate palm
#

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 )

humble flame
#

yea well, the task i have doesnt require coding at all

ornate palm
#

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

humble flame
#

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);
        

    }```
ornate palm
#

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

humble flame
#

i did the copying card2 on card1 and alter it no?

ornate palm
#

code went poof ?

humble flame
#

you talking about suits code

ornate palm
#

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

fallen pagoda
#

what is the difference between Card1 and Card2 ?

ornate palm
#

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 )

humble flame
#

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;
    }```
fallen pagoda
#

what is the difference between Card1 and Card2 @humble flame ?

fallen pagoda
#

the heck is that

humble flame
#

Card2 is working for me somewhat. But card1 im having problems with

humble flame
#
    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