java - 来自 Java 官方教程的 Stuck on Card/Deck 练习

标签 java oop

我卡在了 Java 教程的一部分,特别是 this exercise .该练习要求您:

1- Write a class whose instances represent a single playing card from a deck of cards. Playing cards have two distinguishing properties: rank and suit. Be sure to keep your solution as you will be asked to rewrite it in Enum Types.

Hint: You can use the assert statement to check your assignments. You write:

assert(boolean expression to test);  

If the boolean expression is false, you will get an error message. For example,

assert toString(ACE) == "Ace"; 

should return true, so there will be no error message.

2- Write a class whose instances represent a full deck of cards. You should also keep this solution.

3- Write a small program to test your deck and card classes. The program can be as simple as creating a deck of cards and displaying its cards.

我真的很想做这个练习,但问题是我从来没有玩过纸牌,所以我完全不知道如何创建这个程序以及纸牌应该具有什么属性等。我在维基百科上查了一下但知识非常有限,这永远无法让我构建所需的类:Card.java , Deck.java , 和程序 DisplayDeck.java .

对于不了解纸牌的人来说,有什么好的替代练习可以用来测试与上述练习相同的概念? (可能是静态变量和实例变量等)

谢谢。

顺便说一句,这不是家庭作业问题,我正在为一个商业项目学习 Java。

最佳答案

我认为花时间熟悉该示例是值得的,因为它经常用于描述编程概念。让我试着为您提炼出扑克牌的描述:

首先,保留this picture 牌组(整个牌组)开放供引用。

垂直向下排列,您有{黑桃 ♠、方 block ◇、梅花 ♣、红心 ♡} = 4 花色

水平穿过列,您有 {2, 3, ..., 10, Jack>, Queen, King, Ace} = 13 ranks.

牌组中总共有 4 x 13 = 52 张牌。

每张牌都由(rank, suit)对标识,例如(Ace, Spades ♠) 和 (10, Diamonds ◇) 我们分别读作“Ace of Spades”和“Ten of Diamonds”。

关于java - 来自 Java 官方教程的 Stuck on Card/Deck 练习,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/478918/

相关文章:

java - 使用akka进行实时并行计算

java - Spring 启动: Sharing a bean between different components

java - 使用lucene进行数据库搜索

c# - 重载构造函数和重用代码

php - 面向对象的 PHP 中缺少哪些基本对象?

java - 在编译时删除某些 Java 类的使用

Java list 属性缺失

c# - 具有值类型和引用类型的对象将如何存储在 .NET 中?

Java Swing Frame 导航到另一个 Frame

Python abc 和工厂函数