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!!