I started learning object-oriented programming and the instructor said that "getter" and "setter" functions are used to access and change certain private variables in another class.
For an assignment I was told to make a program that creates a deck of cards, prints it, shuffles it, and finally prints the shuffled deck. The assignment said I have to use good object-oriented principles.
I wrote this code and it works correctly
import java.util.Random;
public class CardDeck {
static String[] suitArray = {"Clubs", "Diamonds", "Hearts", "Spades"};
static String[] faceValue = {"1", "2", "3", "4", "5", "6", "7", "8", "9", "10","Jack", "Queen", "King"};
static String[] deckOfCards = new String[52];
public static void createDeck()
{
int i = 0;
for ( int j = 0; j < 4; j++ )
{
for ( int k = 0; k < 13; k++ )
{
getDeckOfCards()[i++] = getSuitArray()[j] + getFaceValue()[k];
}
}
for(i = 0; i < 52; i++)
{
System.out.println(deckOfCards[i]);
}
}
public static String[] getSuitArray() {
return suitArray;
}
public static void setSuitArray(String[] suitArray) {
CardDeck.suitArray = suitArray;
}
public static String[] getFaceValue() {
return faceValue;
}
public static void setFaceValue(String[] faceValue) {
CardDeck.faceValue = faceValue;
}
public static String[] getDeckOfCards() {
return deckOfCards;
}
public static void setDeckOfCards(String[] deckOfCards) {
CardDeck.deckOfCards = deckOfCards;
}
}
I have only included the portion the creates the deck.
is there harm in using the getter and setter methods? if not i'll keep them in case my instructor wants to see me use them