卡对象和参数化构造函数 [英] Card Object and parameterized constructor

查看:63
本文介绍了卡对象和参数化构造函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我无法理解构造函数的概念。我必须创建以下程序,因为你可以看到我已经非常搞砸了。任何帮助将不胜感激。谢谢!



问题2:卡片对象

创建一个代表扑克牌的类。扑克牌有两个属性,一套西装和一个等级。包含一个公共

参数化构造函数,用于设置卡片具有套装和等级。包括访问套装和排名的getter,但是

你不应该包括setter在创建它们后更改它们。

包括一个测试主文件,证明你的课程有效。



包getcard; 
import java.util.Scanner;
class CardObject {
private int 诉;
private int rank;

public CardObject( int new_suit, int new_rank){
suit = new_suit;
rank = new_rank;

}
public static void ArrayCard(CardObject c){
String [] suit = { 分会 钻石 Hearts < span class =code-string>黑桃}; // http://www.oopweb .com / Java / Documents / ThinkCSJav / Volume / chap11.htm
String [] ranking = { Ace 2 3 4 5 6 7 8 9 10 Jack Queen King};
扫描仪 = 扫描仪(系统。);
系统。 out .println( 输入西装)的1到4的整数;
int new_suit = in .nextInt();
系统。 out .println( 输入等级)的1到13之间的整数;
int new_rank = in .nextInt();
系统。 out .println( 您的卡是 + rank [c.rank] + of + suit [c .suit]);

解决方案

您应该使用枚举类型,而不是使用套装的整数值。您的字符串值也应该是您的类的一部分,允许类的getter方法返回卡的详细信息。请参阅 http://docs.oracle.com/javase/tutorial/java/index.html [ ^ ]以获得更多帮助。

Hello guys,

I am having trouble understanding the concept of constructors. I have to create the following program as you can see I have greatly messed up. Any help would be greatly appreciated. Thank you!

Problem 2: Card Object
Create a class that represents a playing card. Playing cards have two attributes, a suit and a rank. Include a public
parameterized constructor that sets the card to have a suit and a rank. Include getters to access the suit and rank, but
you should not include setters to change them once they are created.
Include a test main that demonstrates that your class works.

package getcard;
import java.util.Scanner;
class CardObject{
    private int suit;
    private int rank;

   public CardObject(int new_suit,int new_rank){
       suit=new_suit;
       rank=new_rank;

       }
   public static void ArrayCard(CardObject c){
       String[] suits={"Clubs","Diamonds","Hearts","Spades"};//http://www.oopweb.com/Java/Documents/ThinkCSJav/Volume/chap11.htm
       String[] ranks={"","Ace","2","3","4","5","6","7","8","9","10","Jack","Queen","King"};
       Scanner in=new Scanner(System.in);
        System.out.println("Enter an integer from 1 to 4 for a suit");
        int new_suit=in.nextInt();
        System.out.println("Enter an integer from 1 to 13 for a rank");
        int new_rank=in.nextInt();
        System.out.println("Your card is "+ranks[c.rank]+ "of" +suits[c.suit]);

解决方案

Rather than using an integer value for the suits, you should use enum types. Your string values should also be part of your class, that allows the class's getter method to return the card details. See http://docs.oracle.com/javase/tutorial/java/index.html[^] for some further help.


这篇关于卡对象和参数化构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆