• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Use a method or create a new object and use it's methods

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm a Java enthusiast and write little utilities so use methods as needed. I needed to take a string and parse it (with a fairly simple algorithm) but decided to  create a new class that would have the methods needed to do what I needed (check for a special code in the string), the string being used to construct the object. Anyway, it made me wonder how folks decide when it is time for a new class versus just writing a method that can be called from  main. What also comes to mind is code where the author is writing  a game and there is the main method that  uses a Game class so that the entire game is an object in the Game class. I normally wouldn't think to do that - i would have a Player class and a Board class but not a Game class. So how does one decide? Just wondering about that. Maybe there are some guidelines on that.   Maybe I should be using more objects than I normally do. Any thoughts?
 
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Johnny Doe wrote:I needed to take a string and parse it (with a fairly simple algorithm) but decided to  create a new class that would have the methods needed to do what I needed (check for a special code in the string), the string being used to construct the object.



You've just described one of the oldest things in the book: a parser. This pattern is used in Java and any other language to construct an AST (Abstract Syntax Tree), representing the program, from the input source text characters. Depending on how complex your source language is, this can reasonably consist of an entire package of classes to represent the AST, not to mention the lexical analyzer and parser.

Honestly I would say don't worry about creating new classes or objects as long as things are clear to you conceptually. Java is designed to be efficient to do those things, and unless there is a specific performance problem with allocating too many objects, that can be optimized.

As far as the game stuff, a "Game" class has an important role as a single object responsible for orchestrating the logic of the game itself. This could be implemented either as a set of static methods, or a Singleton object, or just a regular object. There's no one right way to do it.

A typical "Game" class includes a start method, a render method, and an update method.

The render method is responsible for updating the screen, reflecting the current state of the world. This is where you call into your graphics API such as OpenGL or DirectX, whatever.

The update method computes the new state of the world based on the current state, actions, and time. This step includes a lot of things like collision detection, animation, etc.

A "Player" object would be a class with methods to poll the current state of the input device, move the player's avatar, select menus, etc.

 
Saloon Keeper
Posts: 10705
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As you go through your requirements keep an eye/ear out for nouns. Nouns typically become classes. Verbs become methods.

So, you have Board and Player but how do you play the "Game"? Many people define a Main class to contain the main() method but, if you're like me with lots of old and new projects, you'd end up with lots of Main classes which doesn't help when you want to find a particular one. "Game" is much more descriptive.

And if you follow the "main is a pain" approach to avoiding a plethora of static methods and variables, it is much, much cleaner.
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Damon McNeill wrote:. . . don't worry about creating new classes or objects as long as things are clear to you conceptually. . . .

Agree. It should be easy to create new classes and objects.

. . . a "Game" . . . could be implemented either as a set of static methods, or a Singleton object, or just a regular object. There's no one right way to do it. . . .

But there can be lots of wrong ways to do it. Both a singleton or a utility class would imply there is only one game being played at any one time. And what has this got to do with parsing?
 
girl power ... turns out to be about a hundred watts. But they seriuosly don't like being connected to the grid. Tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic