Forums Register Login

Is this good a programming practice?

+Pie Number of slices to send: Send
Hi, I am relatively new to programming concepts in relation to "good programming practice".

Is it a good programming practice to override a method that refers to the same method.  
What I am trying to do is to write a method with different primitive type parameters that does the same thing.
This is what I mean, consider the following code.

These are my practice programs.  



2
+Pie Number of slices to send: Send
Seems like it would be much easier to do this in the opposite direction -it is much easier to determine whether a string is a palindrome than it is to determine whether an integer is palindromic (it is a simple one-line do-loop, with no need to call other methods). So the version that takes an int argument  passes the String representation of the integer to the version that takes a String argument.
1
+Pie Number of slices to send: Send
That is an acceptable practice. One thing to watch for in your case is that Integer.parseInt() could throw an exception.

Also...
I think you need tempPalidrome.
1
+Pie Number of slices to send: Send
Welcome to the Ranch

First, get rid of the habit to do painting/styling/decorating and other similar kind of stuff in your code. That simply makes your code unreadable. Don't get me wrong - i'm not saying the comments are bad, they are bad when they are used in a wrong place and at a wrong time (which is the case in most cases what we see here on Ranch).

  • Comments should be written in the form of 'JavaDocs' when the components of the class are tempted to be re-used across the bigger project or other projects. Search for how to write JavaDocs.
  • Comments should be written in the form of 'very short and descriptive' when there is a need to explain some extra bits which you can't easily express in a code.

  • An example of second bullet point:
    Any other forms of comments used in the code quite often turs out to be useless. An example of such:

    After all, you should aim to write expressive code, that means: descriptive class names, variables names, method names. So all these would reveal code intention, so it wouldn't require you write obvious things once again just in the actual comments form.

    That was an intro, now about your actual question.

    First. You don't override that method. You can override only inherited method if it isn't static. What you have here, is overloaded method, and it is perfectly valid (in general) case to do so, same as many methods in the Java API does that - overloads each other, so they could accept different parameters or different amount of parameters or both.

    Second. Think a little about it. What if somebody would pass an argument "1234Frankie"? What would happen? Something not what you'd want it to happen, right?
    Now think a little if you'd have that method implemented otherway round, regardless if it is a integer passed, or string, you'd treat it as a string. That would work in both cases probably.

    Let's see what others think here.
    +Pie Number of slices to send: Send
    There is probably only one sort of comment worse than those:-
    int count = 3; // initialise count to 3
    That tells you only what you already know, viz. that counting starts at 3, and doesn't tell you what you really want to know, viz. what happened to 1 and 2.
    +Pie Number of slices to send: Send
    Awesome!!! Thank you for all the inputs here.
    +Pie Number of slices to send: Send
    Our pleasure to help
    +Pie Number of slices to send: Send
    Just want to point out that checking if an integer is a palindrome can be done solely with integer arithmetic operations. No need to convert to string then use string/substring methods to check.

    If you want a better challenge, figure out how to implement isPalindrome(int number) by using only these integer operators: * / % +

    Here's some code to get you started:

    You have to provide the ??? parts to make this method work.

    Hint: The ??? on line 4 can be implemented in as few as three lines of code, including a closing brace, }, on a line by itself.
    Sometimes you feel like a nut. Sometimes you feel like a tiny ad.
    a bit of art, as a gift, that will fit in a stocking
    https://gardener-gift.com


    reply
    reply
    This thread has been viewed 470 times.
    Similar Threads
    how to check whether a word is a palindrome
    Please Help
    More help on java modifications
    Recursive Java Methods
    Help on Java modifications
    More...

    All times above are in ranch (not your local) time.
    The current ranch time is
    Mar 28, 2024 18:57:16.