• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Rob Spoor
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
  • Scott Selikoff
Bartenders:
  • Piet Souris
  • Jj Roberts
  • fred rosenberger

How can I write a GameCard class for a Valid Playing Cards Game

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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:

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

   Copy Code
   private List<String> type = Arrays.asList("RED HEART", "BLACK HEART", "CLOVER", "CARO")
 
Marshal
Posts: 8296
592
Mac OS X VI Editor BSD Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch!

I assume that's the code snippet you were given, which you need to enrich with your code.

So, what have you got so far? It seems you posted only the initial code and forgot to add in your part.
 
Saloon Keeper
Posts: 13978
315
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please enclose your code in code tags like [code]this[/code]. I have added them to your post this time.

The syntax of the constructor in your GameCard class is invalid. You have the keyword "class" in front of the constructor, which is illegal.

You must add code to the GameCard constructor to validate the figure and type parameters. The code must throw an exception if these parameters don't have a valid value.
 
Cris Marinescu
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I did like this till now, but I'm trying to figure out how to continue. I suppose it's a much efficient solution

 
Stephan van Hulst
Saloon Keeper
Posts: 13978
315
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Cris Marinescu
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm thinking to continue like this or should I use a boolean method?
It's not clear how i use those parametres of contructor. I will look in documentation and I will return with a better code

 
Stephan van Hulst
Saloon Keeper
Posts: 13978
315
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Cris Marinescu
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"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
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic