• Post Reply Bookmark Topic Watch Topic
  • New Topic

Head First  RSS feed

 
Martin Messa
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

I am extremely newb in programming area, so please - bear with me. I am having a problem with my Objectville assignment, specifically the Making and testing Movie objects..
I am confused, as to how i can write 2 classes in one file (one being public (referred to as testdrive) and the other just class). I can't have 2 classes in one file, so how am i supposed to compile and run this ?

Reference i found on the internet (http://oreilly.com/catalog/hfjava2/chapter/ch02.pdf)

Thanks,

Martin
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66304
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can have multiple classes in a file; just one of them being the public class.
 
Martin Messa
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
it says i have to define the movietestdrive public class in a new file, how do i do that ?
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66304
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Create a new file for each public class. Remember that the file name must be the same as the public class, and that Java is case sensitive.
 
Martin Messa
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a project folder and two class files; when i try the code in Movie.java, it says that the MovieTestDrive has to be defined in its own file. Even though i already have two class files. Sorry for the stupid questions.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66304
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No worries about "stupid questions". Everyone here was "stupid" when they started out too.

Also, learn the difference between source files and class files. The term "class file" is used to describe the output of the compiler; the files that end with .class. They are not the files that you type your source code into.

If you already have two files, I assume that means you've defined at least two public classes? Right? If you need to define a third, then that means a third file. Your project will end up with the number of files that match the number of public classes that you define.

There's no limit to the number of classes and files that go into a Java program. It may seem overwhelming at the moment, but most Java programs end up with hundreds or thousands of classes.
 
Martin Messa
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Right, it seems that (i am using Eclipse) when i create a new class file, it actually creates .java ending file, not .class file. Any idea, what i am doing wrong ?
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66304
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nothing. The file type of .java is correct for the source files in which you type your code.

When you compile those files with the javac compiler, it will create .class files from them. You do not create the .class files, the compiler does.
 
Martin Messa
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Currently, i have two files in this project - Movie.java and MovieTestDrive.java - only Movie.java contains code (the code on this link http://oreilly.com/catalog/hfjava2/chapter/ch02.pdf Making and testing Movie objects). My MovieTestDrive.java does not contain any code. When i try to run, it still says, that public class MovieTestDrive{ must be defined in its own file. I'm lost
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66304
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If the file doesn't contain any code, what does it contain?
 
Martin Messa
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Haha, good question. I moved the MovieTestDrive code to the MovieTestDrive.java file. Now I have a second question. When i run it, it only outputs the "Playing the Movie", but not the options. What might be wrong ? It seems that it outputs only the Movie.java code, not the MovieTestDrive.java code
 
Daniel Hirning
Ranch Hand
Posts: 50
1
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Martin,

The code typed into MovieTestDrive is mainly for the purpose of the Sharpen Your Pencil test below it. The code only has one output, and that is a string designed in class Movie, so what you are getting is correct.

If you wanted to see some of the variables in action, you could change the playIt() method.

eg.

Anyway, I would suggest reading the rest of the classes and objects chapter at the least before playing too much.

cheers,
Daniel
 
Martin Messa
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you very much for your help !
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!