简体   繁体   中英

In the default constructor initialize attributes

I'm kind of stuck while doing my homework and I'm having a hard time to understand what to do next. So my instructions are as follows:

  1. Create a new class called Card . The Constructors from superclass check box will create a constructor stub in the code.

  2. Create an Enumerated Type in a new source file called Suit with the values NONE , CLUBS , HEARTS , SPADES , DIAMONDS .

  3. Create an Enumerated Type in a new source file in the same package as the Card class, called Rank with the values JOKER , TWO , THREE , FOUR , FIVE , SIX , SEVEN , EIGHT , NINE , TEN , JACK , QUEEN , KING , ACE .

  4. In the class Card ,
    A) Create a class attribute (field) called rank of type Rank .
    B) Create a class attribute (field) called suit of type Suit .
    C) Create getters and setters for these fields. Make the setters private, since we only want the constructor to set the card Suit and Rank (we would not want a programmer or user to set more than one ACE of CLUBS for instance).

  5. Create a constructor that accepts a Suit and a Rank and use those values to set the attributes.

  6. In the default constructor, initialize the suit and rank attributes. Use the this constructor to call the constructor created in the previous step and initialize the rank and suit with a value of your choice (for example: Rank.ACE , Suit.CLUBS ).

  7. In the main method of the class, construct a default Card to demonstrate the default constructor initialization.

  8. Also construct a few additional cards using the Suit and Rank constructor. Use local variables to hold the objects.

  9. Using the toString() method from Enumerated Types, print the Suit and Rank of each Card created.

  10. Create a Deck class that contains a field that is an array of fifty four cards (Containment).

  11. Create a constructor on the Deck class that initializes the array of cards with the standard deck and two jokers, using for loops that iterate over the Suit and Rank values and set the array members with the appropriate Card constructor. Remember that the two Jokers only exist as NONE suits and only the Jokers use the NONE suit. There are many possible solutions for creating only two jokers. I used an iterator, left them out of the loop and added them in the beginning, individually. Use an if statement to exclude the jokers within a for each loop and add them before or after.

  12. Create a toString() method in the Deck class, using another for loop to print all the Card s in the array by calling the toString() method on each card. This method “asks” the Card to print its rank and suit by only calling toString() on the Card object. There should be no reference to Rank or Suit anywhere in this method. Let Card 's toString() method do the work (Delegation).

  13. Create a main method in the Deck class that creates a Deck . Then print the deck using System.out.println() . This will call Deck 's toString() method, which will call toString() on each Card , printing the entire deck.

    public class Card {

    //Default Constructor
    Card (){
    }

   //Create a class attribute (field) called rank of type Rank.
   Rank rank;
   //Create a class attribute (field) called rank of type Suit
   Suit suit;
   //Create an Enumerated Type in a new source file called Suit
   public enum Suit{
   NONE, CLUBS, HEARTS, SPADES, DIAMONDS
   }
   //Create an Enumerated Type in a new source file called Rank
   public enum Rank{
   JOKER, TWO, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT, NINE, TEN, JACK,QUEEN,KING, ACE        
   }

  //Getters & Setters for Rank/Suit
 //Make the setters private, since we only want the constructor to set the   card Suit and Rank
 public Rank getRank() {
 return rank;
 }
 private void setRank(Rank rank) { 
 this.rank = rank;
 }
 public Suit getSuit() {
 return suit;
 }
 private void setSuit(Suit suit) {
 this.suit = suit;
 }

//Create a constructor that accepts a Suit and a Rank and use those values   to set the attributes.
Card ( Rank Rank, Suit Suit){ 
suit = Suit;
rank = Rank;
}






    public static void main(String[] args) {

    }



    }

Constructor is used to initialised a instance of a object. Setters are used to set the values of the variables and getters return this. Have a look at the example below,

public class Test {

    //private members of the instance
    private String strVariable;
    private int intVariable;

    //constructor
    //constructor sets the values of strVariable and intVariable when a Test object is intialized
    public Test(String strVariable, String intVariable) {

        this.strVariable = strVariable;
        this.intVariable = intVariable;
    }

    //setter method for setStrVariable
    //this will overwrite the value of setStrVariable that was assinged when the object was intilized
    public void setStrVariable(String strVariable) {

        this.strVariable = strVariable;
    }

    //getter for the setStrVariable
    //this will return the value of setStrVariable
    public String getStrVariable() {

        return this.strVariable;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM