Page 1 of 2 12 LastLast
Results 1 to 20 of 28
Like Tree12Likes

Thread: How should I go about making my own version of Scanner?

  1. #1
    kkid is offline Senior Member
    Join Date
    Jul 2012
    Posts
    241
    Rep Power
    3

    Default How should I go about making my own version of Scanner?

    I am trying to make my own version of the Scanner object. I want to be able to do everything that the Scanner does but with added validation features if needed (Range checks, format checks etc...). Originally, my initial thought was to simply extend the Scanner class and add my own methods (e.g. nextIntRanged(int lower, int upper)) to provide this functionality. This way I also got to keep the core methods such as nextInt() so that unvalidated input can be used if required also.

    I have been told a lot of times not to extend classes because it is really bad to do so (I still don't understand why). I asked somebody else what to do and was told that I shouldn't go with my original plans ans instead should make a utility class with a global Scanner object and then make lots of static methods in there. Apparently that is better practice with less overhead.

    I was about to take his word for it but then realised that it will have the major problem, I think, of not having any of the default methods of Scanner. Therefore, if I wanted these, I would have to make another separate Scanner object for these times. In this case, surely it would be better to just have a single (extended) Scanner?

    Can somebody offer me some insight?

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default Re: How should I go about making my own version of Scanner?

    ???

    If you write your own class that creates a Scanner object as an instance variable why would you not have access to all the methods of Scanner?

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,091
    Rep Power
    20

    Default Re: How should I go about making my own version of Scanner?

    Either you have misunderstood the "don't extend" statement, or whoever told you that has.
    It sounds to me exactly like your requirement is to provide additional functionality to Scanner, so is in fact a perfect candidate for extension.

    You could wrap it, but I'm not too sure I see the point if all you're going to do is have identical methods simply passing down to the underlying Scanner object. It's not as if you're planning on removing access to any methods.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  4. #4
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,087
    Rep Power
    6

    Default Re: How should I go about making my own version of Scanner?

    Quote Originally Posted by kkid View Post
    I have been told a lot of times not to extend classes because it is really bad to do so (I still don't understand why).
    Ugh, that's pattern thinking - making things black & white so you don't have to think about it. I'd avoid that if I were you, it makes for terrible software engineers. Even bad designs can be a good idea in specific situations, and when you then make it a pattern to NEVER implement the bad design, you'll likely solve the problem where it would have really worked out in another poor way.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  5. #5
    kkid is offline Senior Member
    Join Date
    Jul 2012
    Posts
    241
    Rep Power
    3

    Default Re: How should I go about making my own version of Scanner?

    Quote Originally Posted by Junky View Post
    ???

    If you write your own class that creates a Scanner object as an instance variable why would you not have access to all the methods of Scanner?
    Let's say I make a utility class, MyUtilityClass. In MyUtilityClass, I have 2 methods; nextNumberRanged() and nextCharacter() (Just random examples, names not really important). Inside these methods, I use the basic Scanner.next...() methods surrounded by validation code. These methods use the same Scanner which is initialised as a global variable at the top of MyUtilityClass.

    What I am saying, is that if I wanted to do a simple nextInt() from the standard Scanner object from my other class (The class that calls the methods from the utility class) I will have to create a new scanner for use soley in this class. Therefore, if there came a point where I needed to call nextInt() and MyUtilityClass.nextNumberRanged() somewhere in the execution, 2 Scanners would have been created overall. Surely it would be better to have an extended Scanner class of my own which contains my extra methods, this way I can call MyScannerObject.nextRangedNumber() or I can call MyScannerObject.nextInt() and obly have one Scanner-like object created in the entire execution of my program.

    Have I explained it a little better now?

  6. #6
    kkid is offline Senior Member
    Join Date
    Jul 2012
    Posts
    241
    Rep Power
    3

    Default Re: How should I go about making my own version of Scanner?

    Quote Originally Posted by Tolls View Post
    It sounds to me exactly like your requirement is to provide additional functionality to Scanner, so is in fact a perfect candidate for extension.
    This is exactly what I am trying to do. Simply add functionality (whilst maintaining what the Scanner could originally do too).

    As I said, I cannot see any reason why I should not be able to use extends. I have simply been told, by many people, that it is relaly bad to use extends (I have never even been given a reason why).

  7. #7
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,091
    Rep Power
    20

    Default Re: How should I go about making my own version of Scanner?

    In this case just extend.

    'extend' can be abused in situations which are actually far better served by composition. This isn't one of them.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  8. #8
    kkid is offline Senior Member
    Join Date
    Jul 2012
    Posts
    241
    Rep Power
    3

    Default Re: How should I go about making my own version of Scanner?

    Quote Originally Posted by Tolls View Post
    In this case just extend.

    'extend' can be abused in situations which are actually far better served by composition. This isn't one of them.
    Would you mind breifly explaining such a situation so that I can judge for myself in the future?
    gimbal2 likes this.

  9. #9
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,087
    Rep Power
    6

    Default Re: How should I go about making my own version of Scanner?

    That is an excellent request. It is actually difficult to get a really good "small" example. But take this example.

    Extends:

    Java Code:
    public class Car {
    
    }
    
    public class Truck extends Car {
    
    }
    
    public class TruckGasoline extends Truck {
    
    }
    
    public class TruckDiesel extends Truck {
    
    }
    So we're now creating classes just to have a truck with a gasoline engine or a diesel engine. That's quite a maintenance nightmare because if you want to further specify this you'd have to start extending both classes.

    You could also use composition:

    Java Code:
    public class Engine {
    
      public Engine(String type){
         // you get the point
      }
    }
    
    public class Truck {
      private Engine engine;
    
      public Truck(Engine engine){
         this.engine = engine;
      }
    }
    Its still a poor example but I hope you get the idea. In this example you can still just extend Truck and the type of engine is neatly tucked away as a property of whatever Truck you make. If you'd want you can subclass Engine too. A far more flexible design.
    Tolls and kkid like this.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  10. #10
    KevinWorkman's Avatar
    KevinWorkman is online now Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,987
    Rep Power
    9

    Default Re: How should I go about making my own version of Scanner?

    Quote Originally Posted by kkid View Post
    Would you mind breifly explaining such a situation so that I can judge for myself in the future?
    Another example is one that I see far too often, even in the official Java tutorials:

    Java Code:
    public class Test extends JFrame{
    
       public Test(){
          setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          add(new JLabel("Hello World");
          setSize(200, 200);
          setVisible(true);
       }
    
       public static void main(String... args){
          Test test = new Test();
       }
    }
    In this example, there is absolutely no reason to extend JFrame, since you aren't changing any of its behavior. You don't gain anything by doing it this way instead of just creating an instance of JFrame, and in fact it adds confusion because people reading your code have to figure out what behavior you're changing. This gets much worse in longer pieces of code.

    I guess the tutorials just wanted to show how subclassing worked, but this is a pretty horrible reason to extend a class, imho.
    gimbal2, Tolls and kkid like this.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  11. #11
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,091
    Rep Power
    20

    Default Re: How should I go about making my own version of Scanner?

    Kevin beat me to the JFrame one (which is an example of extending that doesn't add functionality), and I can't think of a better general example than gimbal's (which is an example of extending that does add functionality, but is better represented by composition)...:)
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  12. #12
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,087
    Rep Power
    6

    Default Re: How should I go about making my own version of Scanner?

    Talking smack about code and then not providing the improved alternative. You lazy bastard.

    Java Code:
    public class Test2{
     
       private JFrame applicationWindow;
    
       public Test2(){
          applicationWindow = new JFrame("Hi");
          applicationWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          applicationWindow.add(new JLabel("Hello World"));
          applicationWindow.setSize(200, 200);
          applicationWindow.setVisible(true);
       }
     
       public static void main(String[] args){
          Test2 test = new Test2();
       }
    }
    I can provide another reason for extending: typing laziness.
    kkid and KevinWorkman like this.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  13. #13
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,413
    Rep Power
    20

    Default Re: How should I go about making my own version of Scanner?

    ... and Scanner is a final class.

    db
    JosAH and gimbal2 like this.
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  14. #14
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,087
    Rep Power
    6

    Default Re: How should I go about making my own version of Scanner?

    Quote Originally Posted by DarrylBurke View Post
    ... and Scanner is a final class.

    db
    *yaw drops on the floor*
    DarrylBurke likes this.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  15. #15
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,413
    Rep Power
    20

    Default Re: How should I go about making my own version of Scanner?

    *yaw*?
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  16. #16
    KevinWorkman's Avatar
    KevinWorkman is online now Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,987
    Rep Power
    9

    Default Re: How should I go about making my own version of Scanner?

    Quote Originally Posted by DarrylBurke View Post
    ... and Scanner is a final class.
    Doh. I even thought that but then figured, naaah, all these smart people are talking about extending it, so I must just be misremembering.

    OP- This means that you **can't** extend Scanner. You have to use composition instead.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  17. #17
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,413
    Rep Power
    20

    Default Re: How should I go about making my own version of Scanner?

    Quote Originally Posted by kkid View Post
    I asked somebody else what to do and was told that I shouldn't go with my original plans ans instead should make a utility class with a global Scanner object and then make lots of static methods in there. Apparently that is better practice with less overhead.
    Ask that somebody what makes static methods 'better practice' :-/

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  18. #18
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,087
    Rep Power
    6

    Default Re: How should I go about making my own version of Scanner?

    Quote Originally Posted by DarrylBurke View Post
    *yaw*?
    pitch, roll.

    *jaw. Hi, I'm gimby and I suck at English.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  19. #19
    KevinWorkman's Avatar
    KevinWorkman is online now Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,987
    Rep Power
    9

    Default Re: How should I go about making my own version of Scanner?

    I'll throw in another example, which is the same as Gimbal's truck example. Say you have a Rectangle class:

    Java Code:
    public class Rectangle{
       int x;
       int y;
       int width;
       int height;
    
       public Rectangle(int x, int y, int width, int height){
          this.x = x;
          this.y = y;
          this.width = width;
          this.height = height;
       }
    
       public void drawMe(Graphics g){
          g.setColor(Color.BLACK);
          g.drawRect(x, y, width, height);
       }
    }
    And you want to create a Rectangle that shows up red instead of black, so you subclass it:

    Java Code:
    public class RedRectangle extends Rectangle{
       public void drawMe(Graphics g){
          g.setColor(Color.RED);
          g.drawRect(x, y, width, height);
       }
    }
    But now you want to create a blue Rectangle, so you subclass again:

    Java Code:
    public class BlueRectangle extends Rectangle{
       public void drawMe(Graphics g){
          g.setColor(Color.BLUE);
          g.drawRect(x, y, width, height);
       }
    }
    All of this extending is pretty silly. What if you want a green rectangle? What if you want a rectangle for a randomly generated color? Instead, you could have just added a Color variable to your original Rectangle class, and then pass in whatever value you want!

    Java Code:
    public class Rectangle{
       int x;
       int y;
       int width;
       int height;
       Color color;
    
       public Rectangle(int x, int y, int width, int height, Color color){
          this.x = x;
          this.y = y;
          this.width = width;
          this.height = height;
          this.color = color;
       }
    
       public void drawMe(Graphics g){
          g.setColor(color);
          g.drawRect(x, y, width, height);
       }
    }
    kkid likes this.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  20. #20
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,091
    Rep Power
    20

    Default Re: How should I go about making my own version of Scanner?

    Quote Originally Posted by KevinWorkman View Post
    Doh. I even thought that but then figured, naaah, all these smart people are talking about extending it, so I must just be misremembering.

    OP- This means that you **can't** extend Scanner. You have to use composition instead.
    Oh marvellous.
    That'll teach me...always check the API!

    Of course (back pedal, back pedal) my principle still stands...if it weren't a final class..:p
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Page 1 of 2 12 LastLast

Similar Threads

  1. Making more than one version of the same class?
    By JamieEdwards in forum New To Java
    Replies: 5
    Last Post: 10-12-2013, 07:03 PM
  2. New Version 30
    By alicez in forum New To Java
    Replies: 4
    Last Post: 12-19-2011, 04:43 AM
  3. Version 6.0.25
    By alicez in forum New To Java
    Replies: 10
    Last Post: 05-20-2011, 06:48 PM
  4. java -version pointing to older version
    By deepakts in forum New To Java
    Replies: 4
    Last Post: 05-06-2010, 09:59 AM
  5. to version
    By Alan in forum New To Java
    Replies: 2
    Last Post: 05-31-2007, 06:05 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •