Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Cameron Raw
15,473 PointsClasses? Separate files?
Hi, I'd like some help on the Java course. On the Hangman game project, why do we have three separate files for Hangman, Prompter and Game? And why are some files in our projects saved as .class?
I feel like I could do with some instruction on how and why Java products are best organised like this. When does something go in the Prompter file and not the Game file? Is there clear distinction between that? What are the best codes of practice here? I'm making my own Hangman in Workspaces as a check to see how far I can get without getting stuck, but I realized I wasn't even really sure on how to set up a Java document from scratch. I understand the reason we weren't told these things in depth was because we didn't need them at the time, but I'd like some pointers on the bare basics of structuring and preparing a new Java project so I can start applying all these great things Craig has taught in these videos. At the moment I can't even start without copying and pasting some stuff over from the project files, which I feel like I should know and understand in the same way Nick's classes taught us how to set up a brand new HTML file. (html tags, header, etc).
I hope that makes sense, really enjoying the Java course and am learning a lot. Thanks!
2 Answers
Allan Clark
10,810 PointsThe .class files are the files that the JVM is using when actually running the program. When we compile a program all of the man-made classes (.java files) are converted into something the computer can understand, those are the .class files.
As for class structure, we try to group as much of the relevant data and behavior together as possible. For this example, the Prompter would be where all the variables and methods that deal with user input would go. Things like prompting a guess from the user, asking if the user would like to play again, anything that directly involves the user. The Game class is where the logic of the game is stored, like checking the guess. The Hangman class is the client class, it could contain settings for the game or the functionality for picking the word the user is trying to guess, but the most important part for it is that it has the marker for telling the computer where to start executing code.
i.e.
public static void main(String[] args) {
//some swag code
}
Usually very little else will go into the client class. In fact this method inside a class is all you need to have a functioning program......it won't do much but it will run.
If you are interested you may want to look into the Model-View-Controller (MVC) design pattern for more info on how/why things should be grouped.
Hope this helps.
Cameron Raw
15,473 PointsHi, thanks. I really think that's helped!
Ok so the 'client class' is the file we actually compile and run, and what we should try to do is keep as much of the code in the other classes as possible.
Allan Clark
10,810 PointsThe client class is the only class we tell the compiler to compile with the "javac" command (or whatever IDE you are using), the rest are compiled automatically.
Sahan Wijesinghe
2,453 PointsSahan Wijesinghe
2,453 PointsThanks for the answer :) i was looking for it.