• Post Reply Bookmark Topic Watch Topic
  • New Topic

OOP basic question for an old newbie  RSS feed

 
J R Hatch
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

So, I started programming back when BASIC was new, but purely as a hobby. I have done little since PASCAL went away-ish. I think I get most of the basic concept of OOP, but the particular implementation seems to be escaping me.

I'm writing a card-based game (actually several using the same kind of nonstandard deck) in java and I want to have a Deck class, so I wrote it (with a nested (inner) Card class). But now, when I am writing the actual code for the actual game part, I can't figure out what directory to put the resulting classes in so that I can use instances of them within my game, or how to get them to be recognized. I could just copy/paste the source file for the classes as a nested class in my game, but that seems to be a non-OOP solution. Or maybe I'm not getting the idea.

Help or pointers to help would be appreciated

JR
 
fred rosenberger
lowercase baba
Bartender
Posts: 12563
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am FAR from an expert, but I would not make the Card class a nested inner class. I would make it a class in and of itself. Then the Deck class would have a collection of Cards.

My rational for this is that a Card can move from a Deck to a Hand to a DiscardPile, so each of these other classes may also need to know about the Card Class.

I suppose you could argue that a Card will ALWAYS be a part of a Deck, whether it has been dealt to a hand or not...
 
Paul Clapham
Sheriff
Posts: 22819
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
J R Hatch wrote:I can't figure out what directory to put the resulting classes in so that I can use instances of them within my game, or how to get them to be recognized.


The directory name has to match the name of the package you put the classes in.

Or is your question that you can't decide on a name for the package? Call it "model" or "data" if that's all that's holding you back.
 
Greg Brannon
Bartender
Posts: 563
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OO programming principles and where to put files in directories may be related, but barely.

You're working from the command line? Do you have a directory structure for your source files with projects separated into their own folders? If you have a directory structure similar to that, I suggest you put all source files for the same project in one directory. If you change to that directory before compiling your main source file, all other source files should be located by the compiler and also compiled, or you'll get an error message that some files couldn't be found.

Before getting to the OO part of your question, did you start with the basics, the obligatory "Hello.java" program, for example? Did you already review data types, looping, branching, and conditional statements? Did you write and execute the exercise or practice programs that went along with that review? I'm just trying to understand where you're starting from. Based on your years away from programming, if you skipped those topics because you thought you knew them already, I recommend you turn back a few pages and review those topics. If you successfully made it through those topics and are ready for harder stuff, great!

Classes are collections of related data and methods (or subroutines/functions) that make sense. What makes sense in your card-based program may not make sense in someone else's, so it's hard to evaluate your design with the few details you've given. Perhaps if you showed us your Card class we could be more helpful.
 
J R Hatch
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Greg Brannon wrote:OO programming principles and where to put files in directories may be related, but barely.


That is likely true, but from my perspective they seem linked in that one of the two is the likely source of my problem.

Greg Brannon wrote:
You're working from the command line? Do you have a directory structure for your source files with projects separated into their own folders? If you have a directory structure similar to that, I suggest you put all source files for the same project in one directory. If you change to that directory before compiling your main source file, all other source files should be located by the compiler and also compiled, or you'll get an error message that some files couldn't be found.


I have a directory called "programming" which is where all java and class files are stored so far. I have subdirectories into which I plan to move various source and class files once I figure out how the packaging system works, but my problem precedes that. I wrote the code for the deck and card classes in the same file, and I wanted to write a little command line tester program with a main thread just to make sure the methods I've included work, but when I try to declare public deck testDeck; and later testDeck=new deck();, the compiler tells me that Deck is an unknown symbol, presumably because I haven't told it what file to look in to find that class. How do I tell it?

Greg Brannon wrote:
Before getting to the OO part of your question, did you start with the basics, the obligatory "Hello.java" program, for example? Did you already review data types, looping, branching, and conditional statements? Did you write and execute the exercise or practice programs that went along with that review? I'm just trying to understand where you're starting from. Based on your years away from programming, if you skipped those topics because you thought you knew them already, I recommend you turn back a few pages and review those topics. If you successfully made it through those topics and are ready for harder stuff, great!


Yeah. I did. I also taught myself enough java to write a different game as a source/client pair back in 2003, but I didn't stick with it and have since switched from windows to Liinux, so I'm forgetting/missing a lot of the deployment stuff more than the programming stuff, if that makes sense.


Greg Brannon wrote:
Classes are collections of related data and methods (or subroutines/functions) that make sense. What makes sense in your card-based program may not make sense in someone else's, so it's hard to evaluate your design with the few details you've given. Perhaps if you showed us your Card class we could be more helpful.


Sure. I'm going to develop web-based versions (as a project to teach myself, and also for my friends) of the boardgame "Tsuro" and several house-rule variants (and different games with the same deck). My card and deck source (and I recognize these are probably pretty clumsy, but I learn best by getting myself on a project) follow:



 
fred rosenberger
lowercase baba
Bartender
Posts: 12563
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I added code tags to your post above. It makes it much easier to read by preserving the spacing. you can either type the tags by hand:
[ code]
[ /code]

(without the spaces) and paste your code between them, or paste in your code, highlight it, and click the 'code' button, just like you wanted to make it BOLD or ITALIC.
 
J R Hatch
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
fred rosenberger wrote:I added code tags to your post above. It makes it much easier to read by preserving the spacing. you can either type the tags by hand:
[ code]
[ /code]

(without the spaces) and paste your code between them, or paste in your code, highlight it, and click the 'code' button, just like you wanted to make it BOLD or ITALIC.


Thanks
 
Greg Brannon
Bartender
Posts: 563
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
J R Hatch wrote:
I wrote the code for the deck and card classes in the same file, and I wanted to write a little command line tester program with a main thread just to make sure the methods I've included work, but when I try to declare public deck testDeck; and later testDeck=new deck();, the compiler tells me that Deck is an unknown symbol


By convention, Java classes are named with capital letters. tsuroDeck should be TsuroDeck. You could strike out and be different, but it helps the dialog to use a common language. Now, when someone sees TsuroDeck, they'll know it's a class. tsuroDeck could be a variable name, or tsuroDeck() a method. Package names are usually all lower case.

And as you probably already know, Java is case sensitive, so try to be consistent when you discuss your code.

Looking at your error message, public deck testDeck; and testDeck = new deck(); expect to find a class named deck with a no-argument constructor. I don't see a class called deck in what you've shown us. Did you change the name?

I've looked at your tsuroDeck() constructors, but they hurt my head.
 
Paul Clapham
Sheriff
Posts: 22819
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Following on from Greg's mention of coding conventions, here's a link to the coding conventions which were put together way back in the dawn of Java (when you and I were only middle-aged?): Code Conventions for the Java Programming Language

Pretty much everybody follows these conventions, more or less, to the point that if you violate them, then like Greg said, your code becomes harder for other Java programmers to understand. I haven't looked at them for a long time, and when I looked them up just now I was surprised to see the document was 24 pages long. Overkill, you might well say. But if you scan through the document it might be a useful learning experience. I don't mean you should memorize it or anything like that, but it does seem to give an overview of the basic structure of Java.
 
Campbell Ritchie
Marshal
Posts: 56525
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And welcome to the Ranch
 
J R Hatch
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Greg Brannon wrote:
By convention, Java classes are named with capital letters. tsuroDeck should be TsuroDeck. You could strike out and be different, but it helps the dialog to use a common language. Now, when someone sees TsuroDeck, they'll know it's a class. tsuroDeck could be a variable name, or tsuroDeck() a method. Package names are usually all lower case.


Oops. yeah. I forgot that. Easily fixable.

Greg Brannon wrote: And as you probably already know, Java is case sensitive, so try to be consistent when you discuss your code.


Noted

Greg Brannon wrote:Looking at your error message, public deck testDeck; and testDeck = new deck(); expect to find a class named deck with a no-argument constructor. I don't see a class called deck in what you've shown us. Did you change the name?




I changed the name for my original post, since I figured people had no interest in knowing that I was writing it for Tsuro. The test program I wrote which is giving me the "cannot find symbol" error is at the bottom of this post. (in lines 4 and 7) (also a lot of errors of the form "non-static variable deck cannot be referenced from a static context" which I presume might go away if I fix the problem with declaring "deck")(changed capitalization)

Greg Brannon wrote:I've looked at your tsuroDeck() constructors, but they hurt my head.


Yeah, the cards in Tsuro are the 35 possible arrangements (discounting symmetry) of paths between 8 position on the edges of the cards. You can see 4 of the cards in this image from a combinatorial mathematics blog: (sorry if the image is too large - I didn't see guidelines anywhere).



I coded that in the constructor of the card as the "whereGo" field, to be used in the "fromTo" method, which will answer the question "If I enter this card from position x (numbered 0-7 clockwise starting at top left), where do I exit?" (fromTo also uses whether the card has been rotated before being put in position). Thus, when I create the 35 cards I need to establish those relationships somewhere. Sounds like a job for the constructor to me.








package jtsuro;

class DeckTest{
public TsuroDeck deck;

public static void main(String[] args) {
deck=new TsuroDeck();

System.out.println(deck[0].fromTo(0));
System.out.println(deck[0].fromTo(1));
System.out.println(deck[5].fromTo(0));
System.out.println(deck[5].fromTo(1));

deck[0].rotate();

System.out.println(deck[0].fromTo(0));
System.out.println(deck[0].fromTo(1));
System.out.println(deck[5].fromTo(0));
System.out.println(deck[5].fromTo(1));

deck.shuffle();

System.out.println(deck[0].fromTo(0));
System.out.println(deck[0].fromTo(1));
System.out.println(deck[5].fromTo(0));
System.out.println(deck[5].fromTo(1));



}
}

 
J R Hatch
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So, can no one tell me how to let the TestDeck.class file access the TsuroDeck.class file?
 
Paul Clapham
Sheriff
Posts: 22819
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, apart from the non-OO terminology in that question... you just posted some code which does exactly that. Was there a problem with it?

As for the question: Classes don't access classes. The way for a TestDeck object to interact with a TsuroDeck object is like this:

(1) The TestDeck object acquires a reference to a TsuroDeck object. It might do that by creating a new TsuroDeck object, as in your example, or it might be passed one as a parameter of a method.

(2) Once it has such a reference, it can call any of the methods of that object which it has access to.
 
J R Hatch
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:Well, apart from the non-OO terminology in that question... you just posted some code which does exactly that. Was there a problem with it?

As for the question: Classes don't access classes. The way for a TestDeck object to interact with a TsuroDeck object is like this:

(1) The TestDeck object acquires a reference to a TsuroDeck object. It might do that by creating a new TsuroDeck object, as in your example, or it might be passed one as a parameter of a method.

(2) Once it has such a reference, it can call any of the methods of that object which it has access to.


But my code is not working. When I javac the TestDeck.java file, I get "cannot find symbol" error (in lines 4 and 7) (also a lot of errors of the form "non-static variable deck cannot be referenced from a static context" which I presume might go away if I fix the problem with declaring "deck") - and it's not due to the capitalization - I fixed that. I'm trying to find out which files go in which directories so that when TestDeck.java is being compiled, the compiler knows where to find TsuroDeck.class so that it knows that it can create an instance of it instead of coughing.
 
Paul Clapham
Sheriff
Posts: 22819
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh, okay, we aren't even talking about programming yet. Here's what you should do.

Create a directory whose name is (e.g.) "code". Make that your current working directory before you start using the java and javac commands.
Yours is called "programming"? That's fine too, just use that.

Then under that directory, create a directory corresponding to each package you're planning to use. For example if you want a "jtsuro" package
then create a directory named "jtsuro" under that "programming" directory. (The directory name must be the same as the package name.)

Put Java code for each class in the directory which corresponds to its package. For example you would have a DeckTest.java file in the
"programming/jtsuro" directory. Likewise you would put your TsuroDeck.java file in the directory for its package, whatever that is.

To compile a class, make sure your working directory is your "programming" directory. Then call javac like this:

javac jtsuro.DeckTest.java


As you have already found, you'll have to compile dependent classes first, before you compile classes which use those dependent classes.
 
J R Hatch
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:Oh, okay, we aren't even talking about programming yet. Here's what you should do.

Create a directory whose name is (e.g.) "code". Make that your current working directory before you start using the java and javac commands.
Yours is called "programming"? That's fine too, just use that.

Then under that directory, create a directory corresponding to each package you're planning to use. For example if you want a "jtsuro" package
then create a directory named "jtsuro" under that "programming" directory. (The directory name must be the same as the package name.)

Put Java code for each class in the directory which corresponds to its package. For example you would have a DeckTest.java file in the
"programming/jtsuro" directory. Likewise you would put your TsuroDeck.java file in the directory for its package, whatever that is.

To compile a class, make sure your working directory is your "programming" directory. Then call javac like this:

javac jtsuro.DeckTest.java


As you have already found, you'll have to compile dependent classes first, before you compile classes which use those dependent classes.


And that is precisely what I needed. Thanks!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!