I need to write the implementation of a GameCard class that stores the information corresponding to a card. The class must have 2 private fields: a String that saves the figure (7, 8, 9, 10, J, Q, K or A) and a String that saves the type of playing card (RED HEART, BLACK HEART, CLOVER or CARO) . The class will have a constructor that receives a String and a String as parameters and throws a GameException exception, also defined by you, if one of the parameters is not part of the variants listed above.
The thrown exception message must be Invalid Figure if the first parameter is invalid, Invalid Type if the second parameter is invalid, or Invalid Figure and Type if none of the above parameters follow the rules.
This is what I tried, but i dont know how to continue:
I'm pretty sure that's not valid code. Do you know how to run the compiler yet? It will tell you about the mistakes in your code.
In your constructor, you're assigning figure to this.figure, but figure is a String and this.figure is a List. That's illegal.
I think the point of the assignment is to check whether the figure is one of the valid figures in the list. Before you do that, rename the lists to make it clear what they represent. Also make them static and final, because those two lists represent constants that are not bound to any particular instance of GameCard:
Now, you can use the Collection.contains() method to check whether your input strings are contained in the two lists.
When you've done that, I have another way to further improve your code.
The point of a constructor is to initialize the fields of a new object. A playing card has a rank and a suit (or as they are called in your code, a "figure" and a "type"), but there are no fields in your class to hold that information. The parameters of the constructor are there to accept input from the code that is creating the new object, and the values of those parameters must be assigned to fields of the class.
A job of the constructor is to validate parameters, meaning that the constructor checks whether the parameters have valid values before it finishes running. If the values are invalid, the constructor should throw an exception.
When an exception is thrown, execution of the constructor stops, and the calling code is left to deal with it (or not, and the application will crash). Even if you could, there is no point in returning a boolean from a constructor: either the arguments passed to the constructor are valid and the constructor finishes running normally, or the arguments are invalid and the constructor throws an exception, meaning no object is created. However, it's important to realize that a constructor doesn't have a return value, so you can't return anything.
We renamed the field "figure" to VALID_FIGURES so that we can use the contains() method to check the constructor parameter is valid. However, you still need to assign the values from the parameter to a field. So add a field "figure" and a field "type" to your class that will hold that information about a playing card, and assign them from the constructor parameters.
Your contains() call is not complete yet. It expects an argument. After all VALID_FIGURES.contains() doesn't mean anything. We need it to say something like "Does the list of valid figures contain the figure that was passed into the constructor?", but in code.
You haven't answered my question yet. Do you know how to use the compiler? It will tell you about mistakes in your code, such as that the contains() method requires an argument and that you can't return anything from a constructor.
"Do you know how to use the compiler?" -> Yes, I do know.
"So add a field "figure" and a field "type" to your class that will hold that information about a playing card, and assign them from the constructor parameters." -> I hope it's okay how I did it.
I tested my solution and it worked. Thank you very much for your hints. It helped me a lot!
pie. tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop