aspose file tools*
The moose likes Beginning Java and the fly likes How do I know?? (methods vs classes vs main) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "How do I know?? (methods vs classes vs main)" Watch "How do I know?? (methods vs classes vs main)" New topic
Author

How do I know?? (methods vs classes vs main)

Janeice DelVecchio
Saloon Keeper

Joined: Sep 14, 2009
Posts: 1665
    
  11

All right.... I'm doing this project for school and it seems pretty big.... well to a "n00b" anyway ;-)

I'm going to start writing the pseudocode.... even though I don't know how to do all the things needed yet (like making a GUI and reading from/writing to a file and stuff).

How do I know, for sure, what goes into its own class, what goes into the main class, and what is only a method of something else's class?

In my head, I think I wanna make a new class for everything, though logically I know that can't be the answer. On the other end of the extreme, if everything goes into the main class, it's not really OO programming, is it?

Maybe a little guidance would help....
Janeice


When you do things right, people won't be sure you've done anything at all.
Sebastian Janisch
Ranch Hand

Joined: Feb 23, 2009
Posts: 1183
How do I know?


What the topic is about when there is no meaningful subjectline ? ;)

It basically comes down to the principle of cohesion.
One class should have a single purpose.

As an example, database needs to not go together with GUI code.

When you code, ask yourself what tasks does this class perform that should be independent from each other. If the list gets bigger and bigger you want to create separate classes for each of the tasks.

This enhances reusability and maintainability.

The same applies for methods. If you write a piece of code that could be used elsewhere (for instance retrieving the content of a file), put it into it's own method. This prevents you from code dublication.


JDBCSupport - An easy to use, light-weight JDBC framework -
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18846
    
  40

Personally, I would not worry about it too much -- it will come with experience. In the end, there are generally more than one right way to do something.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Janeice DelVecchio
Saloon Keeper

Joined: Sep 14, 2009
Posts: 1665
    
  11

So maybe the best way to go about this is make a big list of procedures then group them together into objects?

I'm still thinking in terms of "not OO" here I think, but I don't know how to make the leap....

for example (really watered down version):
1. create gui
2. get user input from gui
3. create array from user input
4. do some calculations
5. output calculations to gui

I think 1, 2, and 5 go into main
3, and 4 would go into another class, each with its own method

Or would the main be nothing except the launching of the gui, and 1,2,5 would be in its own class.... 3 and 4 is just for the math? I suppose in this scenario, #3 could be in the main and 1,2,4, and 5 would be somewhere else.........

Am I on the right track here?
Janeice
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

"The right track" is going to be different based on who's doing it :)

Henry's right: don't worry too much about it at this stage of the game. And remember--once you've written it, you may identify other ways to make it cleaner, more elegant, etc. This is a skill that only comes with practice--be patient!
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38881
    
  23
I would take a different tack. Start with the functionality, connecting to database or whatever. Get that working with output to the command line. Then divert that output (and input) to a GUI.

Do it in tiny bits; code for 10 minutes then compile and run and see whether it still works. Test every method as you write.
Janeice DelVecchio
Saloon Keeper

Joined: Sep 14, 2009
Posts: 1665
    
  11

Thanks everyone!
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38881
    
  23
You're welcome

And I hope we have helped and not confused you
Janeice DelVecchio
Saloon Keeper

Joined: Sep 14, 2009
Posts: 1665
    
  11

All right..... Not confused yet.

I started with the GUI (I wanted to get what I thought was the hard part out of the way -- wasn't that hard! )......

Here's my next question about classes......

Can I have as many as I want/need? No matter HOW SMALL they are???

Now that I've written some of my own code, NOW I want to make everything in its own object/class.

What are the pros/cons to having many classes?

/Janeice/
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

The biggest issue is keeping track of where everything is. (IDEs can help mitigate this to some extent.)

Some things simply don't merit having their own source file--why bother putting tiny little listeners that have almost no functionality in a separate files if they're not used anywhere else? Might as well keep it local to where it's actually being used.

Granularity is often a matter of personal taste and opinion. People chide me by saying that I won't be happy until everything is a single line, and to some extent that's true--but there's a point of diminishing returns, and little anonymous inner classes that aren't useful anywhere else are one of those things... for *me*.
Janeice DelVecchio
Saloon Keeper

Joined: Sep 14, 2009
Posts: 1665
    
  11

Thanks!

I suppose as I code more I'll figure out how I'd like to do it.....

I'll try not to segregate TOO much...

/Janeice/
Billy Korando
Greenhorn

Joined: Sep 18, 2009
Posts: 15
There would be three major parts to this program:
Creating the GUI, display output
Handling user input, validation etc.
And performing operations on the user input

I agree with other when they say you don't need to worry about it too much in this stage of the game. However really there shouldn't be much in your "main class" other than calls to the other classes (a call to you create GUI class, followed by a call that listens for user input).


Check out my blog on software development: http://www.turnleafdesign.com
v mallikarjunarao
Greenhorn

Joined: Aug 24, 2009
Posts: 2
since i am new in java ,need to what is the purpose of constructors and where it is use?
what is the purpose of static and where it uses?
i am waiting for you are reply ..........
thaxs frds in advance...........
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9303
    
  17

Hi mallikarjunarao

Please Use Real Words.

Start a new topic for your problem. Don't use someone's topic to ask your own new question. You can click on to start a topic.


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38881
    
  23
Welcome to JavaRanch

Please don't ask unrelated questions in somebody else's thread. This is called hi-jacking a thread, and is not regarded as at all friendly. Please start a new thread with your questions in, then we can all have a look at them.http://java.sun.com/docs/books/tutorial/essential/concurrency/index.html

Are you suggesting, David Newton, that we should write programs occupying a whole line? You want to try FORTH programming; everything can go directly onto a stack and the functions can be very very short.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

My first "real" job was programming Forth one summer back in 1987, an automated cereal boxer.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38881
    
  23
You weren't telling us about the cereal boxes and the soup at Exeter two weeks ago, were you?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38881
    
  23
No, it was Nick Nelson telling us about soup. Do you know him?

And shall we get back to the official topic of this thread
Janeice DelVecchio
Saloon Keeper

Joined: Sep 14, 2009
Posts: 1665
    
  11

Billy Korando wrote:
I agree with other when they say you don't need to worry about it too much in this stage of the game. However really there shouldn't be much in your "main class" other than calls to the other classes (a call to you create GUI class, followed by a call that listens for user input).


Things are coming along nicely.... I have the main method creating a new GUI.... something like:



Then I have the whole GUI set-up in that NewUserInterface class. I have inner classes listening for someone to click the buttons (add entry, run report, and exit). The exit button works. To test the buttons I made the listener code display an outputbox that says "Added" or "Report Run" for their respective buttons. I was able to append text typed in one of the textboxes to the "report" area.

I also have most of the code for the arrays and calculations set up in a practice/test class.

Should I put the array/calculation code INTO the listener classes? My original plan was to pass the information to the new class and make the array there and pass it back. (see my other thread).
I guess my other option (to avoid this landmine) might be to make new inside classes for the calculations.

Am I making this too complicated??

Thanks for all the help so far!
Janeice
 
jQuery in Action, 2nd edition
 
subject: How do I know?? (methods vs classes vs main)