Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
The moose likes Beginning Java and the fly likes Help OO'ing my code please... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Help OO Watch "Help OO New topic

Help OO'ing my code please...

Jay Ran

Joined: Feb 11, 2003
Posts: 2
Hi there,
I can write java programs that will compile after 2 or 3 runs of the compiler cleaning up my syntax and miscapitalizations. My largest problem is that I have a horrible tendency to write very procedural java code. I understand the basics of OO and conceptually "dig it", but that little step of application eludes me.
I have a simple urlconnection that sends a reader to another class which is a simple parser, I thought I could put the parser in its own class file so that I could "reuse" it sometime in the future. I am not sure how to go about this. Here is one of my attempts.

and here is the parser, it fails in the urlconnector when I try to call RequestParser.

*forgot to mention, RequestParser compiles fine, I run: java URLConnector "url" -classpath .
to include the current directory where the RequestParser.class resides.
[ February 11, 2003: Message edited by: Jeff Rand ]
[ February 11, 2003: Message edited by: Jeff Rand ]
William Barnes
Ranch Hand

Joined: Mar 16, 2001
Posts: 984

The first thing I notice is that you don't have any methods. Think about each class. What 'jobs' or 'tasks' would other objects which are using this classes want to do? Those could become methods.
So if I want an instance of your URLConnector class what would I want to be able to do? Maybe send a request, so that would be a method called SendRequest(). What input would this method need, what would it output? ...
You only need one 'main', and that could be in your test driver.

Please ignore post, I have no idea what I am talking about.
Greg Charles

Joined: Oct 01, 2001
Posts: 2835

What you want in RequestParser is a constructor, but what you have is a method called main. A method called "main" is usually the entry point to an application, and it then has the form:
public static void main(String [] args)
Defining main to take a Reader parameter as you have done is legal, so it does compile, but it won't be the entry point to an application, and it definitely won't be a constructor. When you construct the RequestParser with: new RequestParser(r), the JVM is looking for a matching constructor, not for a method called main. You could change the main method to a constructor, but you still wouldn't be really "OO". The constructor should just do basic initialization stuff, say setting up the reader. There should be a method, say called parse, that actually does the work.
Congratulations, you've taken your first step into a bigger world!
Jay Ran

Joined: Feb 11, 2003
Posts: 2
Thank you for your help I have it working now and am working on adding the methods suggested as well.
Thanks again.
William Barnes
Ranch Hand

Joined: Mar 16, 2001
Posts: 984

As Greg said, the constructor shouldn't do any real work. Just whatever is needed to get the object up and running.
Your test driver would (for example) 1] get an instance of the URLConnection class, 2] call a method to send a request, 3] call a method to see if any errors were returned, 4] call a method to get the response, 5] get an instance of the Parser class, 6] send the response to the Parser, ... clean up, ...
After all that works you can think about any relationship your 2 classes might have to one another. Two of the more popluar being "isA" and "hasA".
It is sorta covered in the JavaRanch Style Guide.
subject: Help OO'ing my code please...
Similar Threads
Http Post with XML example
post html login
httpclient athentification issue
Accessing Internet through Proxy Server
Downloading a file from web site