Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: Classes of classes? [Very beginner asking a question] [w/ Codes]

  1. #1
    Junior Member
    Join Date
    Dec 2013
    Posts
    9
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Classes of classes? [Very beginner asking a question] [w/ Codes]

    Okay so right now I am working on my 2nd program yet. I just started Java a few days ago and programming--overall-- for only a week, so a lot of information is very foreign to me.

    What I've been doing is following some basic tutorials, and with each section I learn, I will make my own program to test my own abilities and understanding.

    This means that I am trying to work out the problems of the programs i want to make with less resources (since I have not covered many topics yet)

    I am now working on a very minor card game. (It will simply be executed in the play function of Eclipse. There will be no special graphics or anything)

    What I thought would be a good idea was to create a class for the characteristics required of each card:
    package cardGame;
     
    public class cards {
     
    		String name;
     
    		int traitBarrier;
    		String traitCardType;
    		int cardNumber;
     
     
    		public void setName(String n){
    			name = n;
    		}
     
    		public String getName(){
    			return name;
    		}
     
    		public void setTraitBarrier(int b){
    			traitBarrier = b;
    		}
     
    		public int getTraitBarrier(){
    			return traitBarrier;
    		}
     
    		public void setCardNumber(int c){
    			cardNumber = c;
    		}
     
    		public int getCardNumber(){
    			return cardNumber;
    		}
     
    		public void setTraitCardType(String t){
    			traitCardType = t;
    		}
     
    		public String getTraitCardType(){
    			return traitCardType;
    		}
     
    }

    Then I created another class to database all the created cards(referencing the previous class):
    package cardGame;
     
    public class cardLibrary {
    	public static void main(String[] args) {
     
    	cards card1 = new cards();
    	cards card2 = new cards();
    	cards card3 = new cards();
    	cards card4 = new cards();
    	cards card5 = new cards();
    	cards card6 = new cards();
    	cards card7 = new cards();
    	cards card8 = new cards();
    	cards card9 = new cards();
    	cards card10 = new cards();
     
     
    	card1.name = "Flames Elemental";
    	card1.cardNumber = 1;
    	card1.traitCardType = "Fr";
    	card1.traitBarrier = 3;
     
    	card2.name = "Earth Elemental";
    	card2.cardNumber = 2;
    	card2.traitCardType = "Ert";
    	card2.traitBarrier = 5;
     
    	card3.name = "Aquatic Necromancer";
    	card3.cardNumber = 3;
    	card3.traitCardType = "Wtr";
    	card3.traitBarrier = 4;
     
    	card4.name = "Soul Hunter";
    	card4.cardNumber = 4;
    	card4.traitCardType = "Dk";
    	card4.traitBarrier = 4;
     
    	card5.name = "Crusading Monk";
    	card5.cardNumber = 5;
    	card5.traitCardType = "Lit";
    	card5.traitBarrier = 6;
     
    	card6.name = "Explosion Vigilante";
    	card6.cardNumber = 6;
    	card6.traitCardType = "Fr";
    	card6.traitBarrier = 2;
     
    	card7.name = "Gate Keeper";
    	card7.cardNumber = 7;
    	card7.traitCardType = "Ert";	
    	card7.traitBarrier = 8;
     
    	card8.name = "Priestess of the Lake";
    	card8.cardNumber = 8;
    	card8.traitCardType = "Wtr";
    	card8.traitBarrier = 3;
     
    	card9.name = "Jr. Assailant";
    	card9.cardNumber = 9;
    	card9.traitCardType = "Dk";
    	card9.traitBarrier = 1;
     
    	card10.name = "Divine Alchemist";
    	card10.cardNumber = 10;
    	card10.traitCardType = "Lit";
    	card10.traitBarrier = 3;
     
    	}
    }

    Finally in my main class…

    I seem to be confused on how exactly I can call the cards from my cardsLibrary class into my main class to create the deck.
    Did I skip some steps that were required for me to be able to do this?


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Classes of classes? [Very beginner asking a question] [w/ Codes]

    how exactly I can call the cards from my cardsLibrary into my main class
    Can you explain what you mean by "call the cards"? How will the Card objects be used in the other class?
    The Card variables need to be defined at the class level, not as local variables in the main method. At the class level they can be accessed by other methods in the CardsLibrary class.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Dec 2013
    Posts
    9
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: Classes of classes? [Very beginner asking a question] [w/ Codes]

    Sorry, my terminology is no good.

    Basically the idea is to have the cards from the cardsLibrary class be a deck of 10 cards.
    These 10 cards all have a number assigned to them (1-10)
    I am hoping to have these cards be drawn from the deck with a random number generator (1-10).
    If the generator calls on a number that is the card drawn.

    There is also then going to be (at least I'm working for that) code so that type the card you want to use and depending on the variables defined in cardsLibrary you will either win/lose the round.

    Hopefully that gives the idea of what I want to do with them.

    So if I'm understanding correctly, I should combine the two classes I have shown here?
    and then I can access the class data (from the combined classes) to be on the way of achieving my goal?

    Thank you for your patience!

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Classes of classes? [Very beginner asking a question] [w/ Codes]

    I should combine the two classes
    I don't think I said that. The Card class should be separate.
    The CardLibrary class needs some methods so that other classes can access the Card objects it holds. The CardLibrary class probably does not need a main() method unless it is needed for testing the methods that are added to the CardLibrary class.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Dec 2013
    Posts
    9
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: Classes of classes? [Very beginner asking a question] [w/ Codes]

    Well then I am not understanding correctly.
    I'll look into it more.

    Thanks!

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Classes of classes? [Very beginner asking a question] [w/ Codes]

    If you have any specific questions, please ask them.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Classes of classes? [Very beginner asking a question] [w/ Codes]

    This is an intermediate to advanced level project, inappropriate for one's second Java program. Spend some time - several months of intense study - learning the basics, including an initial understanding of Object Oriented Programming (OOP). Those months of study should include writing hundreds of simple programs that demonstrate and explore the language's features (vocabulary) and syntax (grammar), to familiarize you with the basics, to build your confidence in using the language, and to gain an appreciation for and understanding of the reasons for OOP and how to use it.

    You're not likely to take my advice. When you don't, you will become discouraged by this project and abandon programming. If you're persistent, you will come back in a few months (days, years?), perhaps to this same project, imagining you've gained some programming wisdom from the air, but you will repeat the same cycle. The way to break out of that cycle is to take my advice and learn programming the right way.

    Whichever path you take, good luck!

Similar Threads

  1. A quick Question about classes
    By boyton in forum Java Theory & Questions
    Replies: 2
    Last Post: May 5th, 2013, 08:52 PM
  2. Question about classes...
    By tyeeeee1 in forum Java Theory & Questions
    Replies: 3
    Last Post: December 1st, 2012, 12:59 PM
  3. Replies: 6
    Last Post: October 31st, 2012, 06:16 AM
  4. Question about creating and using classes.
    By iDizzle in forum Object Oriented Programming
    Replies: 20
    Last Post: April 1st, 2012, 03:14 PM
  5. i/o classes question
    By p0oint in forum What's Wrong With My Code?
    Replies: 4
    Last Post: September 28th, 2010, 04:04 PM