Search code examples
javamethodscompiler-errorscomputer-science

How would I write the getter and setter methods for my class?


I'm a beginner with java and I need to write the get() and set() methods for my PlayingCard class and I need someone to explain why I keep getting an error saying "actual and formal argument lists differ in length".

This is the code that I was given to work with:

 class PlayingCard
 {
      int value;
      String suit;
 }

and I have to write these methods:

  • getValue
  • getSuit
  • setValue
  • setSuit

This is the last rendition of the code that I tried, but I don't know why I keep getting the error.

 public int getValue() {
     return value;
 }
 
 public String getSuit() {
     return suit;
 }
 
 public int setValue() {
     return value;
 }
 
 public String setSuit() {
     return suit;
 }

I tried to follow the lecture slides that we were given, but nothing seems to be working. As I said before, I'm a newbie at working with java, so I don't have the eyes to understand what is wrong just yet.


Solution

  • void

    I'm pretty new programmer too so bear with me, I don't think your setter methods should return anything. Usually they're void and look something like:

    public void setSuit(String assignedSuit){
        suit = assignedSuit;
    }
    

    This code means whenever you call upon your setSuit method with the a string parameter assignedSuit it can change the suit variable of your card object.

    I think your getter methods are fine though. Hope this helps.