Click here to Skip to main content
15,347,523 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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.


import java.util.*;

// where i need to write my class

public class prog {
  public static void main(String[] args) {
    try {
      GameCard book = new GameCard("2", "BLACK HEART");
    } catch (GameException e) {
      System.out.print(e.getMessage());  // Invalid Figure
    }
  }
}


What I have tried:

public class GameCard{
private String figure;
private String type;

class GameCard(String figure, String type) throws GameException {
this.figure = figure;
this.type = type;

private List<String> figure = Arrays.asList("7", "8", "9", "10", "J", "Q", "K", "A")

private List<String> type = Arrays.asList("RED HEART", "BLACK HEART", "CLOVER", "CARO")
Posted
Updated 17-May-22 22:59pm
Comments
Richard Deeming 18-May-22 4:36am
   
And?

Dumping your homework assignment without explaining precisely what you have tried and where you are stuck does not make this a question.

And if you're expecting someone to do your homework for you, you've come to the wrong site.

While we are more than willing to help those that are stuck, that doesn't mean that we are here to do it all for you! We can't do all the work, you are either getting paid for this, or it's part of your grades and it wouldn't be at all fair for us to do it all for you.

So we need you to do the work, and we will help you when you get stuck. That doesn't mean we will give you a step by step solution you can hand in!
Start by explaining where you are at the moment, and what the next step in the process is. Then tell us what you have tried to get that next step working, and what happened when you did.

If you are having problems getting started at all, then this may help: How to Write Code to Solve a Problem, A Beginner's Guide[^]
   
Your code is a start. However I would use HashSet, instead of List in order to store the allowed figures and types. Moreover I would make them static.
Then, all you need is implementing the validity check in GameCard constructor, using the contains method of the HashSet.
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900