• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Rob Spoor
  • Bear Bibeault
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:
  • Frits Walraven
  • Himai Minh

Putting Separate methods together

 
Greenhorn
Posts: 27
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Lets say i have several methods like add, delete, retrieve, and view an array and i want to run each at a specific time, maybe in a certain process. Ive made a program that does this however it requires me to call the method one after the other which seems more procedural programming rather than object oriented. Is there another way to have all these methods created and called at a certain time while also looping the entire process. Essentially my logic is this: User is asked to pick from 3 options, 1 add, 2 view, 3 delete. if they pick 1 they go on to the add method and add to the array, then the program should ask the user again for 1 of the 3 options. right now i have this being done by giving the options and under each option is className.methodName(); How could i make this more object oriented.

 
Marshal
Posts: 72905
330
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That is the old‑fashioned way to display a menu. You end up with something like this:-That menu code looks the same in procedural and object oriented code. It says somewhere in Bertrand Meyer's old book Object‑Oriented Software Construction that object orientation doesn't alter the structure of algorithms nor make them shorter to write. The only way I can think of to make a menu more object‑oriented would be to write a GUI and link each action to a different control or component. There are stylistic improvements you can make to my code. You can also use a utility class for keyboard input, which I think is more reliable than a “naked” Scanner.
 
Saloon Keeper
Posts: 23689
161
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Campbell, you didn't define a case 0 clause. Even though it does nothing, without it, the default will be taken and it will print the error message.

A simple menu class could execute its options directly, as shown. A more complex one might allow registering callbacks. And in an object-oriented design, it would be more common to decouple the option input code from the menu dispatcher.
 
Campbell Ritchie
Marshal
Posts: 72905
330
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That was daft of me, wasn't it.
 
Ranch Foreman
Posts: 404
10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When I saw Campbell's response I scrolled past the top line with his name on it and directly looked at the code and the exegesis.
When I saw the name of Bertrand Meyer invoked, I knew for sure it was Campbell.

I remembered that quote, but also came to it myself after studying OOPS seriously the first time.
For me, what my brain formulated was "hard algorithms are still hard, the hard parts of programming are still there, and still kinda hard".

The first time I had seen that was in the mid-1980's.

Lord British had been interviewed, after releasing Ultima IV, the first of his games to be designed using such methodologies (~1985?).

His quote in Electronic Games magazine or some such place was "It really is a very nice tool for organizing things, but the hard parts are still hard!"

I lent the magazine to a dear friend who passed away in 1992.  We had spent forever discussing programming together.  I never got the magazine back, but I remembered (at least a paraphrase of) the Lord British quote.
 
Gurtej Grewal
Greenhorn
Posts: 27
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Right now i have the program running like this, the menu is given and depending on user input, a method is called. After the method is completed, className.menu(); is called to essentially loop the process. Is this the standard process of doing this?

Edit: I got this feedback to do this but im not too sure what to do with "splitting user interaction and data"
 
Marshal
Posts: 7982
560
Mac OS X VI Editor BSD Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What about if you share the code what you're talking about? So far everything is in everyone's imagination only.
 
Gurtej Grewal
Greenhorn
Posts: 27
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
PhoneBookV2 Class


PhoneBook Class
 
Liutauras Vilda
Marshal
Posts: 7982
560
Mac OS X VI Editor BSD Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks. Oh... You should have shared with us your code earlier. Well, there are numerous problems.

Gurtej Grewal wrote:Is this the standard process of doing this?


Your requirements drive your program. Not some standards.

Let me mention some of the things which are questionable. You have so many static methods, that's not "standard"

The structure of the program is fairly very hard to understand.

For instance:
PhoneBookV2 class calls method PhoneBook.task() method
PhoneBook.task() method itself calls PhoneBook.AddNumber()

However, PhoneBookV2 also calls PhoneBook.AddNumber() at the same level as PhoneBook.task() (which itself calls PhoneBook.AddNumber())

Perhaps it is clearer like this. Look how your structure looks like:

Why it is so? What are you trying here? What are your intentions?

You did not explain your application. Are you following some requirements?


 
Gurtej Grewal
Greenhorn
Posts: 27
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Requirements: Demonstrate understanding of arrays and switch statement (pretty simple, just trying to go the extra mile for myself to learn)

Program Intentions:
Take User input of numbers 1-3.
1: Add a number to the phonebook
2: speed dial a number in the phonebook
3: Exit the program

If 1 is chosen;
Ask the user for a number, check to see if it is a number and add it to the phone book. Now ask the user again for what they want to do (numbers 1-3)

if 2 is chosen;
Ask the user for a number 0-9, if a number is saved there, Dial the number, if not, state no number is saved in that location. Again ask for which task between 1-3.

if 3 is chosen;
Exit the program.
 
Gurtej Grewal
Greenhorn
Posts: 27
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ive made some edits to the original code. ive removed PhoneBook.AddNumber(); and PhoneBook.CallNumber(); as task will call them when necessary. How would i go about removing the PhoneBook.task(); from the other methods and simply have a loop to run the same process. And in general what is the best way to avoid using so many static methods?
 
Saloon Keeper
Posts: 8220
71
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Suggest this for getting rid of statics. Replace this:
With:
Then have your PhoneBook class have fields scan and numbers so you don't have to constantly pass them around to your methods. Then make your methods non-static.

P.S. Never, ever close() a scanner made from System.in.

 
Gurtej Grewal
Greenhorn
Posts: 27
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Could you please expand on why i dont close scanner? Last time i posted a question on Stack Exchange Code Review, i was told i MUST close scanners when they are done being used, thats why i added the try resource block in the first place.
 
Carey Brown
Saloon Keeper
Posts: 8220
71
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You must close() every Scanner EXCEPT the one created from System.in because System.in is a system (go figure) resource and once closed cannot be re-opened.
 
Campbell Ritchie
Marshal
Posts: 72905
330
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Carey wrote close() with (), but you shouldn't call close() on most things. You should use Try‑with‑Resources instead. For the same reasons, you should not use try with resources on System.in, System.err and System.out. If Eclipse complains, pick the @SuppressWarnings("resource") option.
 
You showed up just in time for the waffles! And 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
reply
    Bookmark Topic Watch Topic
  • New Topic