简体   繁体   中英

I'm using something i don't entirely understand, clarification?

Throughout my Card class i'm using the throw new IllegalException and it results in the program compiling and most of it working, but i dont know why?

Im returning a String throughout most of my switch statements but i declared them an int?

so how is this working out?

and In the case i want to construct an invalid card, like one with suit 5, which doesn't exist, a simple "This is an invalid suit" print line would be nice but the IllegalException causes a run-time error when i try to excecute the main method.

Class Card:

public class Card
{

    private final int CLUBS = 0;
    private final int DIAMONDS = 1;
    private final int HEARTS = 2;
    private final int SPADES = 3;

    private int points = 0;

    private int RANK;
    private int SUIT;



    /**
     * Constructor for objects of class Card
     */
    public Card(int _rank, int _suit)
    {
        this.RANK = _rank;
        this.SUIT = _suit;
    }


    private String translateSuit(int _suit)
    {

        switch(_suit)
        {
            case 0:
                return "Clubs";
            case 1:
                return "Spades";
            case 2:
                return "Hearts";
            case 3:
                return "Diamonds";
        }
        throw new IllegalArgumentException("Invalid suit: " + _suit);
    }

    private String translateRank(int _rank)
    {
        switch(_rank)
        {

            case 0:
                return "Ace";
            case 1:
                return "Two";
            case 2:
                return "Three";
            case 3:
                return "Four";
            case 4:
                return "Five";
            case 5: 
                return "Six";
            case 6:
                return "Seven";
            case 7:
                return "Eight";
            case 8:
                return "Nine";
            case 9:
                return "Ten";
            case 10:
                return "Jack";
            case 11:
                return "Queen";
            case 12:
                return "King";

        }
        throw new IllegalArgumentException("Invalid rank: " + _rank);
    }

    public void setRank(int _rank)
    {
        this.RANK = _rank;
    }

    public int getRank()
    {
        return this.RANK;
    }

    public void setSuit(int _suit)
    {
        this.SUIT = _suit;
    }

    public int getSuit()
    {
        return this.SUIT;
    }

    public String toString()
    {
        return this.translateRank(RANK) + " of " + this.translateSuit(SUIT) + " -- points: " + this.points;
    }
}

and my main method, which is testing the confirmation of the constructor. I was asked to create 6 cards, 2 valid, 3 INVALID cards(1 invalid suit, 1 invalid rank, and 1 of both), and 1 RANDOM card(still working to figure this out)

public static void main(String [ ] args)
    {


        int testNum = 1;
        Card twoOfClubs = new Card(1, 0);
        Card aceOfHearts = new Card(0, 2);
        Card invalid1 = new Card(12, 5);
        Card invalid2 = new Card(15, 2);


        System.out.println(testNum + ": " +
            (twoOfClubs.toString().equals("Two of Clubs -- points: 0") 
                                            ? "Pass" : "Fail"));
        ++testNum;                                    
        System.out.println(testNum + ": " +
            (aceOfHearts.toString().equals("Ace of Hearts -- points: 0") 
                                            ? "Pass" : "Fail"));





        System.out.println(twoOfClubs);
        System.out.println(aceOfHearts);
        System.out.println(invalid1);
        System.out.println(invalid2);

    }

i'm using the throw new IllegalException and it results in the program compiling and most of it working, but i dont know why?

Basically speaking, an Exception is used to handle problems that happen at run time, not compile time. This is why your code compiles. The line you mentioned is a valid line of code, just like any other line.

Im returning a String throughout most of my switch statements but i declared them an int?

Are you sure this is your code? Some of your methods return a value of type String, yes. Both types, the one you specified in the method signature and the type of the values you return matches: String. Hint: if this wasn't the case, your code wouldn't compile.

What you are probably referring to by "them" is the type of the parameter of most of your methods, which is indeed int. However, this is entirely unrelated to the type of the return value.

and In the case i want to construct an invalid card, like one with suit 5, which doesn't exist, a simple "This is an invalid suit" print line would be nice but the IllegalException causes a run-time error when i try to excecute the main method.

Printing out said line gets the job done, for sure. But using exceptions is a far more versatile way of handling "problems" in your program.

A simple println is a single line that you can easily insert in your Card class. But you should realise that it is not the concern of the Card class to deal with the problems that come up during its usage. What if you don't want to print that line out, but instead open a pop up window with a message describing the error? Adding all the code that create the window into the Card class is a bad idea, because it is unrelated the actual Card.

You might be tempted to say " So what, I just create special return value, like "" (empty string) or "error" to tell the caller of the method that something went wrong. " which would allow you to migrate the code that deals with a possible error outside of the Card class. However, this is nothing else but a poor version of what Exceptions do.

To cut a long story short: Wrap calls to methods that throw exceptions into a try/catch(/finally) block to check for any problems during execution of this method.

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