• Post Reply Bookmark Topic Watch Topic
  • New Topic

developing a program using the object oriented mind set  RSS feed

 
Will Klem
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As a newbie, I find myself thinking in procedures instead of objects. I remember reading somewhere that the initial program development should deal with what to do, not how to do it (to make the best use of objects). How is only important after the classes have been created. The classes handle both the what and (using a combination of statements and methods) the how. Assuming that my understanding of this is correct, I am presenting a programming problem here with what I believe is the proper what mindset for creating classes. Please critique my proposed objects to compute the solution. Don't spare my feelings. If I am on the wrong track, please offer either correction or suggestions.

Here is the programming problem:
Using the console for interaction, ask the user for an integer between 1 and 10000. Convert the integer to hexadecimal, and give the user the result. User input error handling will not be addressed in this early staging of my programming. I am not including methods yet because I want to make sure I have the classes right before moving to how to do it.

What needs to be done:
1) a Communicate class (containing the main method) to:
  • prompt user for the integer
  • accept keyboard input

  • 2) a Divide class to:
  • perform repeated division on the integer
  • perform modulus division and save the result

  • 3) a Convert class to:
  • accept all the values of modulus division and convert them to hexadecimal values
  • print the result to the console.


  • Thanks for your replies.
     
    Knute Snortum
    Sheriff
    Posts: 4087
    112
    Chrome Eclipse IDE Java Postgres Database VI Editor
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I think you're off to a good start.

    Communication Class: definitely. Can be reused in other projects.

    Divide Class: this can be combined with the last class.

    Hex Conversion: This could be a static method in a Math Utilities class or its own class.

    To be clear: when you aren't practicing OOP, there are ways to convert to hex already; but this does not make creating and using the class a useless endeavor.
     
    Knute Snortum
    Sheriff
    Posts: 4087
    112
    Chrome Eclipse IDE Java Postgres Database VI Editor
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Oh, and I would not have the Hex Conversion class print anything. This can be done by the calling method or driver class.
     
    Steffe Wilson
    Ranch Hand
    Posts: 165
    12
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    There's no reason why you shouldn't attempt this exercise, but I would just just caution that because no data needs to be stored at any point then there isn't implicitly any need for any objects. So its not an ideal candidate for learning about OO. All of the functionality could be achieved through static methods, because the exercise itself is entirely procedural. That's my take anyway - others may disagree!
     
    Winston Gutkowski
    Bartender
    Posts: 10573
    65
    Eclipse IDE Hibernate Ubuntu
    • Likes 2
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    One technique I find useful is called "lexical analysis", which sounds very complicated, but is actually extremely simple. But first you need a succinct (and complete) description of the problem. Paring out the wheat from the chaff of your post, I get:
    Will Klem wrote:Using the console for interaction, ask the user for an integer between 1 and 10000. Convert the integer to hexadecimal, and give the user the result.

    What you do then is to go through the description and pull out all the important nouns and verbs - and you may have to go through it several times to get a complete list. Deciding what's "important" does take a bit of practise, but most of the time it's fairly obvious.

    Going through yours, I get:
    Nouns: Console, User, Integer, Hexadecimal, Result.
    Verbs: Ask, Convert, Give.

    Now it's not a 100% thing, but in general nouns equate to classes, and verbs to methods or actions. And the nice thing about it is that they are in the language of the original problem. You, for example, have a 'Communicate' class - and there's nothing wrong with that - but the problem description called it a 'Console'.

    So: is there anything missing? Well, those limits (1 and 10000) look like they might be important, so I'd probably add a "Limits" or "Bounds" noun as well, maybe with the actual bounds in brackets.

    The next thing is to pair up verbs (or actions) with nouns - ie, plug in some possible "methods" to your classes. For example:
  • Console asks User (for an Integer, which must be within Limits)
  • ? converts Integer (to Hexadecimal)
  • Console gives Result (to User)
  • So: what converts an Integer to hexadecimal? It's not immediately obvious from your requirements, but a little searching will tell you that Java's Integer class actually has a method to exactly that already (two actually: toString() and toHexString()).
    Also:
    What's a Result? Isn't it just the converted value?
    What about a User? For a game or a website it might be important, but here it's just the person who will be interacting with the Console, so it probably isn't worth a separate class (at least at the moment).
    And Integer? Well, you already know that Java has its own Integer class that will easily handle a number within our Limits - and it also converts to hex, so it seems silly not to use it.

    Note that we haven't done a stroke of coding yet, but the design is already starting to take shape.

    User input error handling will not be addressed in this early staging of my programming. I am not including methods yet because I want to make sure I have the classes right before moving to how to do it.

    Actually, methods are quite important - or at least their signatures are - because they describe what a class can do. It's the code inside the method that you don't need to worry about just yet, because that IS the "how".
    Also: Do you really want to allow your user to input 27864726, even at this stage? It's up to you, but your requirements do state that the input has to be between 1 and 10000.

    What needs to be done:
    2) a Divide class to:
  • perform repeated division on the integer
  • perform modulus division and save the result

  • 3) a Convert class to:
  • accept all the values of modulus division and convert them to hexadecimal values
  • print the result to the console.

  • Ah, So you plan on rolling your own "converter" as well. Fair enough, but it doesn't seem to be a specific requirement. It's also something you could leave until later.
    I suspect that a 'Divide' class might be overkill (although its good that you're thinking about it) - division (or modulus) is an action; and moreover, one that is available on most numeric types. I'd also suggest that your 'Converter' would be much more reusable if it could convert to any base, not just 16 - or perhaps any base between 2 and 36.

    But just to back up a bit, suppose you wrote the following method in your Console (or Communicate) class:you could then get the main part of your code working by simply calling convert(value, 16) on whatever integer you get back from your user, safe in the knowledge that it will work because Integer.toString() has been around for a very long time.

    Once you know it's all working, then you can start on your "converter" class; and once you're satisfied that IT works properly, change your convert() method to use it.
    Do you see the idea? Do ONE thing at a time, and isolate its effects as much as you possibly can.

    Hope it helps - and I should point out that the technique above is just one; there are many others.

    Winston
     
    Campbell Ritchie
    Marshal
    Posts: 55772
    163
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Somebody else asked a rather similar question: try here, and you will probably get some useful hints.
     
    Will Klem
    Greenhorn
    Posts: 21
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thank you all for your helpful replies. This post will concentrate on Winston's reply because he captured the essence of what I was looking for.

    To begin with I chose this programming problem because it mirrored an extra credit question on a math exam in the 1960's, "Convert 10000 to hexadecimal." Obviously my wording of the problem was not in programmer/developer jargon as revealed by the Communicate/Console class name conflict. ... my mistake, lesson learned (I hope!) ... My initial intent was to tackle the solution the hard way first, creating my own methods, as a learning exercise. Using the Java API classes was to the follow up program. (Maybe I should rethink that.) The parsing of the problem using lexical analysis to pair nouns with verbs to create classes and methods really captured the basic question at hand.

    So: is there anything missing? Well, those limits (1 and 10000) look like they might be important, so I'd probably add a "Limits" or "Bounds" noun as well, maybe with the actual bounds in brackets.
    and
    Do you really want to allow your user to input 27864726, even at this stage? It's up to you, but your requirements do state that the input has to be between 1 and 10000.
    I see that error handling should be addressed. That would provide an easily modified template for use in other programs also.

    I'd also suggest that your 'Converter' would be much more reusable if it could convert to any base, not just 16 - or perhaps any base between 2 and 36.
    That makes sense, but is base 36 a significant number from the programming aspect or just a random number you chose?

    But just to back up a bit, suppose you wrote the following method in your Console (or Communicate) class:

    private final String convert(int value, int base) {
    return Integer.toString(value, base);
    }
    you could then get the main part of your code working by simply calling convert(value, 16) on whatever integer you get back from your user, safe in the knowledge that it will work because Integer.toString() has been around for a very long time.
    I had not considered how to do that at this early stage of planning. Thanks, I will find your suggestion helpful.

    The time and effort you spent to "get inside my head" to address the big picture of my post is greatly appreciated.
     
    Campbell Ritchie
    Marshal
    Posts: 55772
    163
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Will Klem wrote:. . .
    is base 36 a significant number from the programming aspect or just a random number you chose?
    . . .
    36? That is three dozen, a nice round number. It also means that you can use Z/z as the digit for 35. If you wanted base 37 you would run out of symbols for the digits.
     
    Winston Gutkowski
    Bartender
    Posts: 10573
    65
    Eclipse IDE Hibernate Ubuntu
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Will Klem wrote:The time and effort you spent to "get inside my head" to address the big picture of my post is greatly appreciated.

    You're most welcome. Glad I could help.

    And 36 isn't plucked out of nowhere; it's the number of digits and letters in the English alphabet, and therefore the highest base you can convert without any major queries about what a "digit" is. Hexadecimal already assumes that you use the first 6 letters.

    Winston
     
    Will Klem
    Greenhorn
    Posts: 21
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    The explanation of letters + numbers makes sense.

    In the process of rereading the entire thread I keyed in on this excerpt from Knute:
    This can be done by the calling method or driver class.

    A search of the Java API sent me to the Interface Driver class explanation. It seems to be targeted to data bases. Are there other common uses?
     
    Campbell Ritchie
    Marshal
    Posts: 55772
    163
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    A driver class is an informal term for a class designed solely to make other classes do their work. It will usually have one method with many calls to methods of other classes. It will probably also have a main method to start it off.
     
    Campbell Ritchie
    Marshal
    Posts: 55772
    163
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    The Driver interface appears to be designed for driving a connection to an SQL database. That is rather different from the informal use of driver clss.
     
    Will Klem
    Greenhorn
    Posts: 21
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Then would the informal use of the term driver class in practical application be something like this?

    A Bank driver class contains individual method calls to these classes: CheckingAccounts, SavingAccounts, CertificatesOfDeposits, BankCards, etc. Then the various classes called perform the necessary work using their methods and return to the driver class.

    EDIT for punctuation
     
    Campbell Ritchie
    Marshal
    Posts: 55772
    163
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Yes. Once you have got your development work done you will probably use a different technique to run the different methods.
     
    Will Klem
    Greenhorn
    Posts: 21
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Reading between the lines, the driver class is for testing and debugging before deploying the program; hence,
    you will probably use a different technique to run the different methods.

    Did I get that right?
     
    Campbell Ritchie
    Marshal
    Posts: 55772
    163
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Yes, I think you were right.
     
    Will Klem
    Greenhorn
    Posts: 21
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thanks. Your replies are helpful.
     
    Stevens Miller
    Bartender
    Posts: 1444
    30
    C++ Java Netbeans IDE Windows
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Winston Gutkowski wrote:One technique I find useful is called "lexical analysis", which sounds very complicated, but is actually extremely simple.
    ...
    Now it's not a 100% thing, but in general nouns equate to classes, and verbs to methods or actions.


    Winston, that is brilliant. Don't know if you came up with it yourself, or got it elsewhere, but this is the first time I've encountered it. You get a cow.
     
    Will Klem
    Greenhorn
    Posts: 21
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    OK, I give up! How does someone get a cow, and what does that signify?
     
    Stevens Miller
    Bartender
    Posts: 1444
    30
    C++ Java Netbeans IDE Windows
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Click on the word cow in my comment above and find out!
     
    Will Klem
    Greenhorn
    Posts: 21
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thanks. Being color blind, I could not see the dotted line underline until I leaned real far into the monitor.
     
    Winston Gutkowski
    Bartender
    Posts: 10573
    65
    Eclipse IDE Hibernate Ubuntu
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Stevens Miller wrote:Winston, that is brilliant. Don't know if you came up with it yourself, or got it elsewhere, but this is the first time I've encountered it. You get a cow.

    Thanks, but I'm afraid the technique was invented by someone much cleverer than me. I'm not even sure that "lexical analysis" is the official name, but I've certainly heard it called that before.

    The main things I like about it are that it's (a) very simple, and (b) it focuses your mind on something other than just the problem, which might otherwise become overwhelming.
    I also find that it helps to avoid distractions at a point where you really don't need them.

    However, just to re-iterate, you do need a good - and comprehensive - description of the problem to work from.

    Winston
     
    Stevens Miller
    Bartender
    Posts: 1444
    30
    C++ Java Netbeans IDE Windows
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Regardless of where it originated, it does seem useful. I probably wouldn't have thought so before I learned OOP, but it's a great match for the OOP philosophy(?). As you often say, the best move is sometimes to stop coding. I've been reading a lot about requirements lists and use cases lately. Developing those seems to be a good way to get that description and they help make it clear that coding shouldn't start until after you know what the problem is and how you want to solve it.

    Of course, after one has written some code and has something that works, the iterative cycle of new/improved requirements leading to more use cases and new/changed code can follow. But isolating the development of that description from the implementation phase is something I have really come to see for its importance.

    Heh. Not like it was when I got started, but things are different now.
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!