-1

I'm a total beginner in Java and I'm working on an assignment that's largely prefabricated/preformatted code, but I can't get it to work. In Eclipse, I get an error saying "Method cardToString(MyCard) is undefined for the type MyCardTester" before I even run it. I've looked at similar questions on Stackoverflow,

(Eclipse is telling me a method is undefined when it clearly is in fact defined, "The method is not defined for the type" error in simple program in java)

and they have different problems from me. I think that my problem may be with my classpath or run configuration, but those settings seem fine. Here is the code:

Here's the first class:

package EllevensGame;
import EllevensGame.MyCard;
import java.lang.String;

/**
 * This is a class that tests the Card class.
 */

public class MyCardTester {

    /**
     * The main method in this class checks the Card operations for consistency.
     *  @param args is not used.
     */
    public static void main(String[] args) {
        MyCard testCard = new MyCard("King", "Hearts", 13);
        String printStuff = cardToString(testCard);
        System.out.println(printStuff);
    }
}

Second class:

package EllevensGame;
/**
 * MyCard.java
 *
 * <code>MyCard</code> represents a playing card.
 */
import java.lang.String;

public class MyCard {

    /**
     * String value that holds the suit of the card
     */
    protected String suit;

    /**
     * String value that holds the rank of the card
     */
    protected String rank;

    /**
     * int value that holds the point value.
     */
    protected int pointValue;


   /**
     * Creates a new <code>Card</code> instance.
     *
     * @param cardRank  a <code>String</code> value
     *                  containing the rank of the card
     * @param cardSuit  a <code>String</code> value
     *                  containing the suit of the card
     * @param cardPointValue an <code>int</code> value
     *                  containing the point value of the card
     */
    public MyCard(String cardRank, String cardSuit, int cardPointValue) {
        //MyCard newCard = new MyCard(cardRank, cardSuit, cardPointValue); Not sure if this is right or not 
    }


    /**
     * Accesses this <code>Card's</code> suit.
     * @return this <code>Card's</code> suit.
     */
    public String suit() {
        return suit;
   }

    /**
     * Accesses this <code>Card's</code> rank.
     * @return this <code>Card's</code> rank.
     */
    public String rank() {
        return rank;
    }

   /**
     * Accesses this <code>Card's</code> point value.
     * @return this <code>Card's</code> point value.
     */
    public int pointValue() {
        return pointValue;
    }

    /** Compare this card with the argument.
     * @param otherCard the other card to compare to this
     * @return true if the rank, suit, and point value of this card
     *              are equal to those of the argument;
     *         false otherwise.
     */
    public boolean matches(MyCard otherCard) {
        if (otherCard.pointValue == (pointValue()) && (otherCard.rank.equals(rank)) && (otherCard.suit.equals(suit))) {
            return true;
        }
        else {return false;}
    }

    /**
     * Converts the rank, suit, and point value into a string in the format
     *     "[Rank] of [Suit] (point value = [PointValue])".
     * This provides a useful way of printing the contents
     * of a <code>Deck</code> in an easily readable format or performing
     * other similar functions.
     *
     * @return a <code>String</code> containing the rank, suit,
     *         and point value of the card.
     */
    //@Override
    public String cardToString(MyCard newCard) {
        String pointstring = String.valueOf(pointValue);
        String print = rank + " of " + suit + pointstring;
        return print;
    }
}

Final note: the code is supposed to create a "card" object for a card game (Ellevens).

Thanks!!

Brandon_J
  • 195
  • 3
  • 17

1 Answers1

4

cardToString is a method in MyCard, you must invoke it through the reference. Change

String printStuff = cardToString(testCard);

to

String printStuff = testCard.cardToString(testCard);

Although it might be better to make that method return a String based on the this instance (which would make more sense).

public String cardToString() {
    return rank + " of " + suit + pointValue;
}

And then

String printStuff = testCard.cardToString();

I then fixed your constructor

public MyCard(String cardRank, String cardSuit, int cardPointValue) {
    this.rank = cardRank;
    this.suit = cardSuit;
    this.pointValue = cardPointValue;
}

And ran it getting

King of Hearts13
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
  • Still get that error? **Not** `Method cardToString(MyCard) is undefined for the type MyCardTester`? Have you recompiled your code? – Elliott Frisch Dec 28 '18 at 02:47
  • Yes the error is a run-time error now. Sorry for staying "still" – Brandon_J Dec 28 '18 at 02:47
  • Well I fixed your constructor and ran it, and it works fine here. I suggest you try again. – Elliott Frisch Dec 28 '18 at 02:50
  • @Brandon_J Look at [this](https://stackoverflow.com/questions/18093928/what-does-could-not-find-or-load-main-class-mean) thread – GBlodgett Dec 28 '18 at 02:59
  • Holy moly I got it to work!! Fixed it based on https://stackoverflow.com/questions/25162773/how-do-i-add-a-directory-to-the-eclipse-classpath - I had a random (unnecessary) file required in the build path. Thank you so much!! – Brandon_J Dec 29 '18 at 00:51