Win a copy of Svelte and Sapper in Action this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

Help OO'ing my code please...

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

Thankyou.
*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 ]
 
Ranch Hand
Posts: 1067
2
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Sheriff
Posts: 3036
12
Mac IntelliJ IDE Python VI Editor Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1067
2
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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".
 
Poop goes in a willow feeder. Wipe with this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic