• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Java OO - First time help!

 
Greenhorn
Posts: 20
Eclipse IDE Debian Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Marshal
Posts: 79977
396
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Marshal
Posts: 79977
396
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 20
Eclipse IDE Debian Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.)
 
Dan Moody
Greenhorn
Posts: 20
Eclipse IDE Debian Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Greenhorn
Posts: 21
Eclipse IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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



 
Campbell Ritchie
Marshal
Posts: 79977
396
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Marshal
Posts: 79977
396
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
. . . to add to thatThat is how you could call the Car class; you can do similar thing with the Movie class.
 
Dan Moody
Greenhorn
Posts: 20
Eclipse IDE Debian Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
lowercase baba
Posts: 13091
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Dan Moody
Greenhorn
Posts: 20
Eclipse IDE Debian Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 13091
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Think of how dumb the average person is. Mathematically, half of them are EVEN DUMBER. Smart tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic