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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
• Campbell Ritchie
• Liutauras Vilda
• Bear Bibeault
• Tim Cooke
• Junilu Lacar
Sheriffs:
• Paul Clapham
• Devaka Cooray
• Knute Snortum
Saloon Keepers:
• Ron McLeod
• Tim Moores
• Stephan van Hulst
• Tim Holloway
• Frits Walraven
Bartenders:
• Carey Brown
• salvin francis
• Claude Moore

# Narcissistic number exercise

Ranch Hand
Posts: 37
The requirement is:
Write a class Exercise3 with a method public static boolean isNarcissisticNumber(int number)
The aim of the method is to judge a number which is a narcissistic number or not. A narcissistic number refers to a 3-digit decimal number which is the sum of the cubes of their digits: (e.g. 1 ^ 3 + 5 ^ 3 + 3 ^ 3 = 153). If the input parameter is a narcissistic number, the method returns true, conversely, returns false. In class Exercise3, add a main() method which invokes isNarcissisticNumber. Test your program.

The code so far:

Results: All works fine, just can't seem to handle wrong input.

The problem:
I can't get it to recognise if the input has more than or less than 3 digits. I tried to test if it catches the input of more than 3 digits on lines 18-21. I can't even begin to process what I would have to write to catch less than 3 digits or non int input. Very much a beginner here. Any help would be appreciated.
________________________________________________
This exercise has 2 parts actually, but I'm trying to finish the first part before I move on to the second. I don't know if I'm supposed to make a new topic for the second part, so I'll just add it here instead:

Part 2:
Requirement: Write a class Exercise3 with a method public static int sum()
The aim of the method is to compute the sum of all the narcissistic numbers. Note that narcissistic number is a 3-digit decimal number. In class Exercise3, add a main() method which invokes sum. Test your program.

Code so far (no actual code, just ideas):

Problem: Not sure if I'm going about it the right way. Can I get some advice on how I'm supposed to start it and if I'm heading in the right direction? Is saving elements in an array the best way to do this? Is there an easier way?

Marshal
Posts: 13447
222
Numbers that have less than three digits: 0-99

Numbers with three digits: 100-999

Numbers with more than three digits: 1000- onwards.

Can you write an if statement to check if a number has 3 digits based on the above information? It's certainly not impossible or very difficult.  Sometimes we make things harder than they need to be.

Junilu Lacar
Marshal
Posts: 13447
222
This:

can be written simply as this:

The isNarcissisticNumber() method returns a boolean so it shouldn't be displaying anything to System.out.  Keep things like that separate: if a method returns something, then it shouldn't display. If it displays, then it shouldn't return anything.

Your code should be organized like this:

Only the main has System.out.println() statements. The isNarcissisticNumber method only returns the results of the calculation. Keep display and calculation logic separate.

Suane Mane
Ranch Hand
Posts: 37

Junilu Lacar wrote:Numbers that have less than three digits: 0-99

Numbers with three digits: 100-999

Numbers with more than three digits: 1000- onwards.

Can you write an if statement to check if a number has 3 digits based on the above information? It's certainly not impossible or very difficult.  Sometimes we make things harder than they need to be.

OOOOO got this now. Although now having trouble getting the program to work properly for the 3 digit numbers.

Code:

Result:

Suane Mane
Ranch Hand
Posts: 37

Junilu Lacar wrote:This:

can be written simply as this:

The isNarcissisticNumber() method returns a boolean so it shouldn't be displaying anything to System.out.  Keep things like that separate: if a method returns something, then it shouldn't display. If it displays, then it shouldn't return anything.

Your code should be organized like this:

Only the main has System.out.println() statements. The isNarcissisticNumber method only returns the results of the calculation. Keep display and calculation logic separate.

I didn't get a thing of all this post lol

Bartender
Posts: 2232
94

Suane Mane wrote:...

if count is set to 0, how will it be ever greater than 3 ?

Suane Mane
Ranch Hand
Posts: 37

Suane Mane wrote:...

if count is set to 0, how will it be ever greater than 3 ?

It's meant to be a counter of the number digits so initialising with 0.

Suane Mane
Ranch Hand
Posts: 37
Updated code:

salvin francis
Bartender
Posts: 2232
94
First of all, lets not re-quote the complete previous post, just the important bits.

Suane Mane wrote:...It's meant to be a counter of the number digits so initialising with 0.

your counter is 0, Why are you checking if its greater than 3 if you are not changing it ?

Suane Mane
Ranch Hand
Posts: 37
Sorry about the quoting! I don't know how to quote just parts of a post.
I initially had the if statements checking if the counter is more than 3 digits inside the while loop but it didn't detect it at all then so I tried different placement and then it worked. Couldn't make much sense of it.

salvin francis
Bartender
Posts: 2232
94
Your updated code will still not work. "count" is still not modified at all.

salvin francis
Bartender
Posts: 2232
94
Junilu mentioned a while ago :

Junilu Lacar wrote:Numbers that have less than three digits: 0-99

Numbers with three digits: 100-999

Numbers with more than three digits: 1000- onwards.

Can you write an if statement to check if a number has 3 digits based on the above information?

I see that your updated code is trying to use this idea but on the wrong variable.
Hint: The if condition would not be on "count" in this case.

Suane Mane
Ranch Hand
Posts: 37
Must change variable to number instead of count. Got it. Program works (even if messy lol). Only considering how to catch the wrong type of input now (string instead of int) and part B.

Thanks for trying to clean up my code btw guys, I'm just less focused on writing the best formatted code and more focused on actually understanding what each part of the code does and practicing with it. I prefer some long-winded versions because they help me see each step of the logic. (repeating year 1 undergraduate CS and a slow learner due to med issues so... yeah. I take a while to understand seemingly easy concepts).
Apologies beforehand if it seems like you're explaining stuff to a wall.

I have a whole assignment with several exercises to complete by next week. This is just one exercise. Been working on it for a while, but can't afford to lose marks, so need to make sure I get the logic down first and have a correct version of the code before I start looking at the "cleanliness" of it.

Junilu Lacar
Marshal
Posts: 13447
222
• 2
The irony is that the messier the code is, the harder it is to understand.

If you indent properly, choose good names, and organize the logic better, the code will be MUCH easier to understand.

So if your goal is to understand, your first order of business is to write cleaner code.

Suane Mane
Ranch Hand
Posts: 37

Junilu Lacar wrote:This:

can be written simply as this:

Help me understand then please. In this case, it takes all those lines and apparently contracts them to just a few words.
So I don't understand how an if else statement is replaced by just a return statement. How does that work? How will I recognize that

actually refers to the logic of: "if total == number then our boolean variable verify is true, otherwise it's false and it will print True and False respectively"

salvin francis
Bartender
Posts: 2232
94
• 1
If I am reading some code, and I come across a method with a signature as :
public static boolean isNarcissisticNumber

I would assume that this method returns true and false based on the number. Your method also has System.out.print statements. This means that the method is not only deciding, it's also reporting the result. This may be undesirable if you use this same code in some other program.

If we remove the print statements, your code becomes:This can be shortened to:This can be shortened to:or:and finally:

Hope it helps

Junilu Lacar
Marshal
Posts: 13447
222

Suane Mane wrote:
Help me understand then please. In this case, it takes all those lines and apparently contracts them to just a few words.
So I don't understand how an if else statement is replaced by just a return statement. How does that work? How will I recognize that

actually refers to the logic of: "if total == number then our boolean variable verify is true, otherwise it's false and it will print True and False respectively"

That code wasn't entirely equivalent, I have to admit. What Salvin showed with the System.out.println() statements omitted was closer to what I was shooting for.

The expression total == number is a boolean expression; it is either true or false. As such, it is redundant to write:

because if you evaluate the expression, it is either equivalent to this:

or this

Either of the above snippets can be written simply as

As I explained in my first reply, the System.out.println() statements should be removed from the isNarcissisticNumber() method because the method's job is to calculate a result; it shouldn't display the result as well. You don't want to mix calculating a result with displaying that result because it leads to code that is more difficult to maintain. Rather, your main() method should have the System.out.println() statement that displays the result of calling isNarcissisticNumber() as I showed. Something like this:

Junilu Lacar
Marshal
Posts: 13447
222
Since you already put in some effort to write a solution, I'll show you how I would have arranged the logic:

salvin francis
Bartender
Posts: 2232
94

Junilu Lacar wrote:

I suggest going for a isThreeDigit instead of a isNotThreeDigit method.

Junilu Lacar
Marshal
Posts: 13447
222

salvin francis wrote:

Junilu Lacar wrote:

I suggest going for a isThreeDigit instead of a isNotThreeDigit method.

If you do that, then the guard clause will have to be written as:

which is actually less readable than isNotThreeDigit(number). I know where you're coming from but there are many good APIs that spell out the "not" rather than force users to use the ! operator. The spelled out "not" in the name actually addresses the readability issue quite well and does not have the same cognitive weight as the ! operator.  JUnit, for example, has assertNotEquals(), assertNotSame(), and assertNotNull() with no hit on readability.

Junilu Lacar
Marshal
Posts: 13447
222
If you want your API to have more symmetry, then you might provide both isThreeDigit(int number) and isNotThreeDigit(int number), implemented like so:

I only showed the one method since there wasn't any other logic that needed the opposite. Keeping things small should trump keeping the code symmetrical. In other words, only write the code you actually need, not what you might need.

Marshal
Posts: 64179
215
• 1
I presume we are sticking to the natural numbers, not all ints, otherwise −123 would count as a three‑digit number.

salvin francis
Bartender
Posts: 2232
94
I'd agree for a isNotXXX method only if there is a corresponding isXXX method that is present. Having a single isNotXXX method in a code is very confusing especially if in the future, someone needs to negates it:
e.g. if(!isNotThreeDigit()) is very confusing to read. This is also true for natural spoken english, two negatives in a sentence is confusing. "Push button to release oxygen" vs "Do not push this button if you do not want to release oxygen" !!!

Junilu Lacar
Marshal
Posts: 13447
222

salvin francis wrote:Having a single isNotXXX method in a code is very confusing especially if in the future, someone needs to negates it: e.g. if(!isNotThreeDigit()) is very confusing to read.

That is quite a stretch of an argument. If you need to negate isNotThreeDigit(), then write a isThreeDigit() method instead. ¯\_(ツ)_/¯

I don't know, maybe it's just me but I try to find ways to make code easier to read, not think of ways it's going to be harder.

salvin francis
Bartender
Posts: 2232
94

Junilu Lacar wrote:...That is quite a stretch of an argument. If you need to negate isNotThreeDigit(), then write a isThreeDigit() method instead. ¯\_(ツ)_/¯  ...

Let's simply agree to disagree

I understand your perspective about keeping code easier to read and I think you understand my perspective about having a single negation method without it's partner assertive method.

Junilu Lacar
Marshal
Posts: 13447
222

salvin francis wrote:Let's simply agree to disagree

Not sure where we disagree. I thought I was agreeing that you should write an isThreeDigit() method if you needed code that used the positive assertion. What I disagreed with was the argument that you'd have to write something convoluted like (!isNotThreeDigit(n)) instead, which to me defies common sense.

salvin francis
Bartender
Posts: 2232
94

Junilu Lacar wrote:...Not sure where we disagree. ...

A single method isNotThreeDigit() is what I disagreed on since my first post.

Junilu Lacar
Marshal
Posts: 13447
222
I pointed out that the code at that point really only needed isNotThreeDigit() -- there was no code that needed isThreeDigit(). So you'd rather have dead code just for the sake of symmetry? Sure, if there is code that would benefit from having isThreeDigit(), then absolutely, add it. However, the code only needed this:

Avoiding the ! operator as a general rule for high-level code is a good rule of thumb but isNotThreeDigit() does not have the same cognitive weight as !isThreeDigit(), so I see no reason to be averse to it.

One more thought: for a public API, I'd agree that having something like isNotThreeDigit() just by itself would be less than ideal. If the methods are public, ideally you'd have both isThreeDigit() and isNotThreeDigit(). Notice though that the isNotThreeDigit() method I showed was declared as private.

 After some pecan pie, you might want to cleanse your palatte with this tiny ad: Create Edit Print & Convert PDF Using Free API with Java https://coderanch.com/wiki/703735/Create-Convert-PDF-Free-Spire