aspose file tools*
The moose likes Beginning Java and the fly likes Java OO - First time help! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Java OO - First time help!" Watch "Java OO - First time help!" New topic
Author

Java OO - First time help!

Dan Moody
Greenhorn

Joined: Feb 27, 2011
Posts: 20

Hello guys, first post so, here goes:

I'm currently working my way through The Head First Java Book (very good book) and I've just got onto the OO stages... I can copy the code fine, thats not the issue, and I'm pretty sure the movie test drive and movie are separate files....

So do i put the files in the same package or just the same folder/directory? and if so, what do I do in order to make the files talk to eachother propoerly?

Sorry to sound like an utter idiot but I'm pretty sure thats what this forum is for.


Thanks guys.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39834
    
  28
Welcome to the Ranch

At this stage, leave out any package names in the .java files. The package name should be easy to find; it looks like this . . . and must be the first line of code.
Create a directory like this . . . or similar, and navigate to it like thisLearn basic Java™ before you try dealing with packages
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39834
    
  28
You will need one .java file for each class marked public class at its beginning. The class Foo must be in a file called Foo.java.
If you put all your files in one directory, the different objects will have no difficulty finding each other to "talk to". Indeed, if there is any difficulty finding code, the compiler will complain and refuse to produce .class files, so you will know soon enough.
Dan Moody
Greenhorn

Joined: Feb 27, 2011
Posts: 20

Campbell Ritchie wrote:You will need one .java file for each class marked public class at its beginning. The class Foo must be in a file called Foo.java.
If you put all your files in one directory, the different objects will have no difficulty finding each other to "talk to". Indeed, if there is any difficulty finding code, the compiler will complain and refuse to produce .class files, so you will know soon enough.


thanks, the other problem I have now, is that I have my two files, I have my code and everything else (I'm using eclipse if this helps) but the two files won't "talk to eachother" and none of them will compile and run. If I've figured this out correctly, I want MovieTestDrive to compile and run to read code from the other file.

here's what I've got for code:



It would be cool if I get the hang of OO as something tells me the majority of my programming (should I even reach that standard) will rely on it.


Thanks,


Edit by mw: Added CodeTags to preserve formatting.
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Dan Moody wrote:...the two files won't "talk to eachother" and none of them will compile and run...

Your code compiles and runs for me. What are the exact error messages you're getting?

(Note that there is currently no output when this runs, but you can add that by calling playIt() on your Movie instances.)


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Dan Moody
Greenhorn

Joined: Feb 27, 2011
Posts: 20

marc weber wrote:
Dan Moody wrote:...the two files won't "talk to eachother" and none of them will compile and run...

Your code compiles and runs for me. What are the exact error messages you're getting?

(Note that there is currently no output when this runs, but you can add that by calling playIt() on your Movie instances.)


OK, and how would I call playIt() ?

eclipse just terminates it without giving any error messages


something else which might help, the reason I use eclipse to compile and run is because no matter what I try, cmd just wont do it...somehting to do with javac
Death Arte
Greenhorn

Joined: Feb 17, 2011
Posts: 21

Dan Moody wrote:
OK, and how would I call playIt() ?


Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39834
    
  28
I don't like the way you are setting your fields in the Movie class. Make all its fields private, and initialise them in the constructor. Like thisNote here you are setting speed to a default value of 0 in all cases, and there will be other methods which change the speed.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39834
    
  28
. . . to add to thatThat is how you could call the Car class; you can do similar thing with the Movie class.
Dan Moody
Greenhorn

Joined: Feb 27, 2011
Posts: 20

Thank you all!

I understand it all now, thanks very much (I can bet that I'll be back in 5 minutes though)

I can now sleep!



I suppose the next question is, why can't I compile and run in cmd?
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11479
    
  16

Dan Moody wrote:I suppose the next question is, why can't I compile and run in cmd?

That would depend on what error you get and what exactly you are typing on the command line (and what directory you are in when you do so...)

Assuming you are in the same directory as your .java files, you should be able to type

javac *.java

to compile them all. Try that, and tell us what you get.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Dan Moody
Greenhorn

Joined: Feb 27, 2011
Posts: 20

fred rosenberger wrote:
Dan Moody wrote:I suppose the next question is, why can't I compile and run in cmd?

That would depend on what error you get and what exactly you are typing on the command line (and what directory you are in when you do so...)

Assuming you are in the same directory as your .java files, you should be able to type

javac *.java

to compile them all. Try that, and tell us what you get.


according to my pc:
'javac' is not recognized as an internal or external command, operable program or batch file.

you've probably answered this one 100 times...and more!
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11479
    
  16

your PATH is not set up right. First, confirm you have the java compiler. Mine is here:

C:\Program Files\Java\jdk1.6.0_23\bin

when I do a 'dir' on that directory, I see (among other things) a "javac.exe".

find where yours is, and then simply update your PATH. You will need to close any open cmd windows for it to take effect.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Java OO - First time help!