Someone please break this down piece by piece.

  • (2 Pages)
  • +
  • 1
  • 2

24 Replies - 951 Views - Last Post: 16 October 2010 - 09:41 AM Rate Topic: ***-- 2 Votes

#1 temalamorsa  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 41
  • Joined: 15-October 10

Someone please break this down piece by piece.

Posted 15 October 2010 - 08:41 PM

Ok, here's the story. I'm 15 years old and one day in class I decided I wanted to create a game for my TI-84 calculator. Once I began reading through some tutorials on coding for calculators I changed my mind. I figured that if I was going to spend the time making a game for the calculator I may as well learn Java and make a real game.
I have been teaching myself Java over the last few days using the Java tutorials on oracle.com. I just reached the section about classes and objects which I thought I understood. That is until I got to the exercises and checked my answer. The answer they gave for exercise 1 was this.

public class Card {
    private final int rank;
    private final int suit;

    public final static int DIAMONDS = 1;
    public final static int HEARTS = 2;
    public final static int SPADES = 3;
    public final static int CLUBS = 4;

    public final static int ACE = 1;
    public final static int DEUCE = 2;
    public final static int THREE = 3;
    public final static int FOUR = 4;
    public final static int FIVE = 5;
    public final static int SIX = 6;
    public final static int SEVEN = 7;
    public final static int EIGHT = 8;
    public final static int NINE = 9;
    public final static int TEN = 10;
    public final static int JACK = 11;
    public final static int QUEEN = 12;
    public final static int KING = 13;
    
    public Card(int rank, int suit) {
        assert isValidRank(rank);
        assert isValidSuit(suit);
        this.rank = rank;
        this.suit = suit;
    }
    
    public int getSuit() {
        return suit;
    }
    
    public int getRank() {
        return rank;
    }
    
    public static boolean isValidRank(int rank) {
        return ACE <= rank && rank <= KING;
    }

    public static boolean isValidSuit(int suit) {
        return DIAMONDS <= suit && suit <= SPADES;
    }

    public static String rankToString(int rank) {
        switch (rank) {
        case ACE:
            return "Ace";
        case DEUCE:
            return "Deuce";
        case THREE:
            return "Three";
        case FOUR:
            return "Four";
        case FIVE:
            return "Five";
        case SIX:
            return "Six";
        case SEVEN:
            return "Seven";
        case EIGHT:
            return "Eight";
        case NINE:
            return "Nine";
        case TEN:
            return "Ten";
        case JACK:
            return "Jack";
        case QUEEN:
            return "Queen";
        case KING:
            return "King";
        default:
            return null;
        }
    }
    
    public static String suitToString(int suit) {
        switch (suit) {
        case DIAMONDS:
            return "Diamonds";
        case CLUBS:
            return "Clubs";
        case HEARTS:
            return "Hearts";
        case SPADES:
            return "Spades";
        default:
            return null;
        }
    }

    public static void main(String[] args) {

        assert rankToString(ACE) == "Ace";
        assert rankToString(DEUCE) == "Deuce";
        assert rankToString(THREE) == "Three";
        assert rankToString(FOUR) == "Four";
        assert rankToString(FIVE) == "Five";
        assert rankToString(SIX) == "Six";
        assert rankToString(SEVEN) == "Seven";
        assert rankToString(EIGHT) == "Eight";
        assert rankToString(NINE) == "Nine";
        assert rankToString(TEN) == "Ten";
        assert rankToString(JACK) == "Jack";
        assert rankToString(QUEEN) == "Queen";
        assert rankToString(KING) == "King";

        assert suitToString(DIAMONDS) == "Diamonds";
        assert suitToString(CLUBS) == "Clubs";
        assert suitToString(HEARTS) == "Hearts";
        assert suitToString(SPADES) == "Spades";

    }
}



I was wondering if anyone out there could break this down for me step-by-step so I can at least begin to wrap my head around it.
P.S. These tutorials are the first time I have ever programmed anything other than making some scripts for Morrowind with TES: Construction Set.

Edited by macosxnerd101: Please, :code:.

Is This A Good Question/Topic? 0
  • +

Replies To: Someone please break this down piece by piece.

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8325
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Someone please break this down piece by piece.

Posted 15 October 2010 - 09:13 PM

Ouf... make an array will ne a lot easier

from
public static String rankToString(int rank) {
switch (rank) {
case ACE:
return "Ace";
case DEUCE:
return "Deuce";
case THREE:
return "Three";
case FOUR:
return "Four";
case FIVE:
return "Five";
case SIX:
return "Six";
case SEVEN:
return "Seven";
case EIGHT:
return "Eight";
case NINE:
return "Nine";
case TEN:
return "Ten";
case JACK:
return "Jack";
case QUEEN:
return "Queen";
case KING:
return "King";
default:
return null;
}
}



to
static final String[] values = {"", "As", :Deuce", "Three:,.... "Queen", "King"};

public static String rankToString(int rank) {
  return values[rank];
}


lot easier as you see and a lot less to type/code

Also, do not over use assert() they are to check run time error not badly written code
you shouldn't use them in that case as ... your program will work or not at the first time

Happy coding
Was This Post Helpful? 0
  • +
  • -

#3 temalamorsa  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 41
  • Joined: 15-October 10

Re: Someone please break this down piece by piece.

Posted 15 October 2010 - 09:26 PM

View Postpbl, on 15 October 2010 - 08:13 PM, said:

Ouf... make an array will ne a lot easier

from
public static String rankToString(int rank) {
switch (rank) {
case ACE:
return "Ace";
case DEUCE:
return "Deuce";
case THREE:
return "Three";
case FOUR:
return "Four";
case FIVE:
return "Five";
case SIX:
return "Six";
case SEVEN:
return "Seven";
case EIGHT:
return "Eight";
case NINE:
return "Nine";
case TEN:
return "Ten";
case JACK:
return "Jack";
case QUEEN:
return "Queen";
case KING:
return "King";
default:
return null;
}
}



to
static final String[] values = {"", "As", :Deuce", "Three:,.... "Queen", "King"};

public static String rankToString(int rank) {
  return values[rank];
}


lot easier as you see and a lot less to type/code

Also, do not over use assert() they are to check run time error not badly written code
you shouldn't use them in that case as ... your program will work or not at the first time

Happy coding

Thank you for your help, but I wasn't looking for a way to simplify it. This is the answer Java gave me for a question in the tutorial. I was hoping someone could explain to me what each of these pieces does.
Was This Post Helpful? -1
  • +
  • -

#4 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8325
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Someone please break this down piece by piece.

Posted 15 October 2010 - 09:33 PM

View Posttemalamorsa, on 15 October 2010 - 10:26 PM, said:

I was hoping someone could explain to me what each of these pieces does.

So you mean you didn't wrote it ?
We don't really like cheaters who cut and paste code here
Was This Post Helpful? 2
  • +
  • -

#5 LynnL  Icon User is offline

  • D.I.C Head

Reputation: 21
  • View blog
  • Posts: 109
  • Joined: 13-April 09

Re: Someone please break this down piece by piece.

Posted 15 October 2010 - 09:42 PM

-1 to PBL ?
This guy helps dozen of people everyday with right advices 99% of the times
You are not acting the right way if you want help in the future
Was This Post Helpful? 0
  • +
  • -

#6 temalamorsa  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 41
  • Joined: 15-October 10

Re: Someone please break this down piece by piece.

Posted 15 October 2010 - 09:54 PM

In response to both your posts I'm very grateful that he tried to help, but I was simply asking for someone to explain to me what each piece of the code does since I have next to no programming knowledge. In response to whether or not I cheated. NO. This isn't for a class or anything. I was going through the tutorials on the Java website and they have you do exercises along the way. I clicked "Check my answers" and that piece of code popped up. I don't understand a single piece of it which is why I was asking if someone could explain each piece of it to me so I might begin to understand how to implement the various pieces of a Java code.
Was This Post Helpful? 0
  • +
  • -

#7 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8325
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Someone please break this down piece by piece.

Posted 15 October 2010 - 09:56 PM

View PostLynnL, on 15 October 2010 - 10:42 PM, said:

-1 to PBL ?
This guy helps dozen of people everyday with right advices 99% of the times
You are not acting the right way if you want help in the future

Don't worry
-1 after 3 posts his reputation is kind of made :-)
Won't bother trying to help him anymore :)
Thanks for cancelling the -1 he gave me
Was This Post Helpful? 0
  • +
  • -

#8 temalamorsa  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 41
  • Joined: 15-October 10

Re: Someone please break this down piece by piece.

Posted 15 October 2010 - 09:58 PM

View Postpbl, on 15 October 2010 - 08:56 PM, said:

View PostLynnL, on 15 October 2010 - 10:42 PM, said:

-1 to PBL ?
This guy helps dozen of people everyday with right advices 99% of the times
You are not acting the right way if you want help in the future

Don't worry
-1 after 3 posts his reputation is kind of made :-)
Won't bother trying to help him anymore :)
Thanks for cancelling the -1 he gave me

What does the -1 even do? I just clicked it because frankly the response you gave didn't answer my question. I didn't mean any offence by it. I'm sorry if I offended you in any way.
Was This Post Helpful? 0
  • +
  • -

#9 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8325
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Someone please break this down piece by piece.

Posted 15 October 2010 - 10:00 PM

View Posttemalamorsa, on 15 October 2010 - 10:54 PM, said:

In response to both your posts I'm very grateful that he tried to help, but I was simply asking for someone to explain to me what each piece of the code does since I have next to no programming knowledge. In response to whether or not I cheated. NO. This isn't for a class or anything. I was going through the tutorials on the Java website and they have you do exercises along the way. I clicked "Check my answers" and that piece of code popped up. I don't understand a single piece of it which is why I was asking if someone could explain each piece of it to me so I might begin to understand how to implement the various pieces of a Java code.

So ? You ask us to explain to you code you haven't written]Rules are clear

We will not do your homework for you! Do not ask us to give you code!

and the standard one
[rules][/rules]

Happy coding
Was This Post Helpful? 1
  • +
  • -

#10 temalamorsa  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 41
  • Joined: 15-October 10

Re: Someone please break this down piece by piece.

Posted 15 October 2010 - 10:05 PM

I don't understand why you think I'm asking you to do homework for me? I'm simply asking someone to explain to me what the various components that comprise this code do so I can better grasp the syntax. Also from now on I will post with the
 
format I didn't see that when looking through the BB Code.
Was This Post Helpful? 0
  • +
  • -

#11 Alex6788  Icon User is offline

  • kitties == adorable


Reputation: 144
  • View blog
  • Posts: 1,667
  • Joined: 15-July 10

Re: Someone please break this down piece by piece.

Posted 15 October 2010 - 10:09 PM

View Posttemalamorsa, on 15 October 2010 - 10:58 PM, said:

View Postpbl, on 15 October 2010 - 08:56 PM, said:

View PostLynnL, on 15 October 2010 - 10:42 PM, said:

-1 to PBL ?
This guy helps dozen of people everyday with right advices 99% of the times
You are not acting the right way if you want help in the future

Don't worry
-1 after 3 posts his reputation is kind of made :-)
Won't bother trying to help him anymore :)
Thanks for cancelling the -1 he gave me

What does the -1 even do? I just clicked it because frankly the response you gave didn't answer my question. I didn't mean any offence by it. I'm sorry if I offended you in any way.

The rep system is for thanking and showing that a post was helpful but just because it didn't answer your question you don't have to -1, -1 is mostly for if someone is giving bad advice or being rude.
Was This Post Helpful? 2
  • +
  • -

#12 temalamorsa  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 41
  • Joined: 15-October 10

Re: Someone please break this down piece by piece.

Posted 15 October 2010 - 10:12 PM

Quote

The rep system is for thanking and showing that a post was helpful but just because it didn't answer your question you don't have to -1, -1 is mostly for if someone is giving bad advice or being rude.

Thank you for explaining that to me. I thought it just gave the answer itself a -1 not the person. Kind of like how Yahoo answers works. Now that I know I will not be giving anyone a -1 unless they deserve it. BTW pbl i gave you +1 to cancel out the -1 I gave you.
Was This Post Helpful? 1
  • +
  • -

#13 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8325
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Someone please break this down piece by piece.

Posted 15 October 2010 - 10:17 PM

View Posttemalamorsa, on 15 October 2010 - 11:12 PM, said:

Quote

The rep system is for thanking and showing that a post was helpful but just because it didn't answer your question you don't have to -1, -1 is mostly for if someone is giving bad advice or being rude.

Thank you for explaining that to me. I thought it just gave the answer itself a -1 not the person. Kind of like how Yahoo answers works. Now that I know I will not be giving anyone a -1 unless they deserve it. BTW pbl i gave you +1 to cancel out the -1 I gave you.

So here is a +1 to cancel Lynnl to you... hope she won;t be pissed off
Was This Post Helpful? 1
  • +
  • -

#14 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10397
  • View blog
  • Posts: 38,471
  • Joined: 27-December 08

Re: Someone please break this down piece by piece.

Posted 15 October 2010 - 10:18 PM

Basically, this class models a Card object. I'm assuming since you have gotten to classes and Objects that you are familiar with methods and constants. The new elements for you are probably the assert statements. Basically, an assert statement goes something like: assert boolean-condition;. It's like an if statement, except if the boolean condition evaluates to false, an Assertionerror is thrown. It's helpful in validating and testing, but not really seen in the real world.

You will also see variables rank and suit in the class, but not in the method. These are instance variables associated with each individual Card object. So if you have Card c = new Card(2,3);, that will be the Deuce of Spades, with c.rank == Card.DEUCE == 2, and c.suit = Card.SPADES == 3.

The main() method basically tests the rankToString() method.

If you have more specific questions on a part of the code, feel free to ask. Also, you may want to check out the Java Tutorials Section. We have plenty of good tutorials on class design. You may also want to read my tutorial Moving Away From Parallel Arrays, which covers class design in an easy to understand way. I think I did a better job than Sun in writing a class design tutorial, but then again, I'm a little biased. :)
Was This Post Helpful? 1
  • +
  • -

#15 temalamorsa  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 41
  • Joined: 15-October 10

Re: Someone please break this down piece by piece.

Posted 15 October 2010 - 10:24 PM

View Postpbl, on 15 October 2010 - 09:17 PM, said:

View Posttemalamorsa, on 15 October 2010 - 11:12 PM, said:

Quote

The rep system is for thanking and showing that a post was helpful but just because it didn't answer your question you don't have to -1, -1 is mostly for if someone is giving bad advice or being rude.

Thank you for explaining that to me. I thought it just gave the answer itself a -1 not the person. Kind of like how Yahoo answers works. Now that I know I will not be giving anyone a -1 unless they deserve it. BTW pbl i gave you +1 to cancel out the -1 I gave you.

So here is a +1 to cancel Lynnl to you... hope she won;t be pissed off

Hey thanks. Just for good measure I'll give you +1 again. :lol:

View Postmacosxnerd101, on 15 October 2010 - 09:18 PM, said:

Basically, this class models a Card object. I'm assuming since you have gotten to classes and Objects that you are familiar with methods and constants. The new elements for you are probably the assert statements. Basically, an assert statement goes something like: assert boolean-condition;. It's like an if statement, except if the boolean condition evaluates to false, an Assertionerror is thrown. It's helpful in validating and testing, but not really seen in the real world.

You will also see variables rank and suit in the class, but not in the method. These are instance variables associated with each individual Card object. So if you have Card c = new Card(2,3);, that will be the Deuce of Spades, with c.rank == Card.DEUCE == 2, and c.suit = Card.SPADES == 3.

The main() method basically tests the rankToString() method.

If you have more specific questions on a part of the code, feel free to ask. Also, you may want to check out the Java Tutorials Section. We have plenty of good tutorials on class design. You may also want to read my tutorial Moving Away From Parallel Arrays, which covers class design in an easy to understand way. I think I did a better job than Sun in writing a class design tutorial, but then again, I'm a little biased. :)

Thank you!!! :bananaman: That actually made this make sense! I have 1 more question before I move on to the next section of the tutorial(I might even use yours so I can re-learn some of the concepts covered in the Java tutorial in a new way). My question is what exactly it is that constructors do. The Java tutorial wasn't very clear on that.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2