user2827773
user2827773

Reputation: 21

error: constructor Card in class Card cannot be applied to given types;

I'm new to programming to please forgive me if it looks horrible. I'm not quite too sure what I'm doing.

When I compiled my code I get the following error:

error: constructor Card in class Card cannot be applied to given types;
required: no arguments
found: String, String, int
reason: actual and formal arguments differ in length

this is my code:

public void testConvenienceConstructor() {
    System.out.println("Card...");
    Card instance = new Card("4", DIAMONDS, 4);
    assertEquals("4", instance.getName());
    assertEquals(DIAMONDS, instance.getSuit());
    assertEquals(4, instance.getValue());

Here's my Card class code:

package model;

public class Card implements CribbageConstants {
//-----fields-----
private String name;     //the name of the card
private String suit;     //the suit of the card
private int value;       //the value of the card

//---------- Constructors ---------
/**
 * No argument constructor - set default values for card
 */
public Card() {

        name = "ACE";
        suit = "CLUBS";
        value = 1;
    }
//-------------- Utility methods --------------

    /**
     * Provide a text representation of a card.
     *
 * @return The banana's name, suit, and value
 */
public String getName() {
    return name;
}

public String getSuit() {
    return suit;
}

public int getValue() {
    return value;
}

//------mutator-----
public void setName(String name) {
    this.name = name;
}

public void setSuit(String suit) {
    this.suit = suit;
}

public void setValue(int value) {
    this.value = value;
}
//-----------utility methods------------
}

Upvotes: 1

Views: 2124

Answers (3)

No Idea For Name
No Idea For Name

Reputation: 11597

You are trying to construct a card here:

Card instance = new Card("4", DIAMONDS, 4);

using a constructor that does not exist.

In the class Card you need to create a constructor that accept the given types:

public Card(String nme, String suit, int val) {

        name = nme;
        suit = suit;
        value = val;
}

You must also send DIAMONDS as a string I believe (Surrounded by double quotes):

Card instance = new Card("4", "DIAMONDS", 4);

If you don't want to add another constructor you can change the initiate code:

Card instance = new Card();
instance.setName("4");
instance.setSuit("DIAMONDS");
instance.setValue(4);

Upvotes: 1

hasan
hasan

Reputation: 24195

Add this to your class:

public Card(String name, String suit, int value)
{
    this.name = name;
    this.suit = suit;
    this.value = value;
}

Upvotes: 1

musical_coder
musical_coder

Reputation: 3896

With new Card("4", DIAMONDS, 4);, you're calling a constructor for Card that takes a String, a String, and an int. But no such constructor exists! So that's why the compiler is upset.

Add this to your code:

public Card(String name, String suit, int value) {
    this.name = name;
    this.suit = suit ;
    this.value = value;
}

Upvotes: 1

Related Questions