It's been a bit since I've been here. I was just wondering -- I'm putting together a little program to arrange my list of CD's (for those of you who don't know) and I'm in the process, reading through HFJ, of writing the prepcode. How do you know what classes to create and what variables and methods to put in them, or is that something that can vary from person to person, or how you want the program to be structured? Basically, I want to
1.Take entries for the CD Information (Artist, Album Title, Record Label and Year of Release)
2. Check to see if the Year of Release is valid
3. Once all CD's have been entered, print out the list and ask if any changes need to be made
4. Make any necessary changes
5. Ask how the user wants the information to be displayed -- either alphabetically by Artist, or Album etc.
6. Print the result
7. Then save the result for later use.
I'm nowhere near being able to do all of this yet -- I'm still learning. So far I'm just starting to work out steps 1 and 2. But I'm confused as to what classes I might need and what methods to put in them. So far I've got one class, which creates a CD object and checks the entered information. I think the code for the inputting the CD information needs to go in a separate class (same for the editing part of the program).
How do you guys figure out the best way to write your code -- I mean, how do you know when the code for a class is too much or too complicated and should go somewhere else?
I mean, how do you know when the code for a class is too much or too complicated and should go somewhere else?
Classes should be highly cohesive, that they should have only one objective. Assuming you have a simple POJO representing the state of the CD, you should try to encapsulate the operations manipulating CD objects in to another class.
So far I'm thinking that in my CD Class I Declare the variables and then perhaps include a method that sets them based on the entered information, including checking to see if the Year of Release is valid. Does that sound right? Should the method that prints the list be in another class as well?
Christopher Laurenzano wrote:can you tell me what you mean by POJO?
So far I'm thinking that in my CD Class I Declare the variables and then perhaps include a method that sets them based on the entered information, Does that sound right?
POJO is Plain Old Java Object. It usually contains state and the methods for manipulating the state (getters and setters - getYear(), setYear(int) etc..)
including checking to see if the Year of Release is valid.
Yes, normally simple checks are performed inside the setter methods.
Should the method that prints the list be in another class as well?
You can override the toString() method inside POJO to just print the states of the object, Or if this is more complex thing than that then you should move that code somewhere else.