Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!

# Percentage issue

apollo abel
Ranch Hand
Posts: 32
Hi, i have written a program, it reads the user's input and tells which numbers are perfect, abundant and deficient.
now, for example, the user enters 28. this is what the program will show:
Enter a number:
28
1 DEFICIENT
2 DEFICIENT
3 DEFICIENT
4 DEFICIENT
5 DEFICIENT
6 PERFECT
7 DEFICIENT
8 DEFICIENT
9 DEFICIENT
10 DEFICIENT
11 DEFICIENT
12 ABUNDANT
13 DEFICIENT
14 DEFICIENT
15 DEFICIENT
16 DEFICIENT
17 DEFICIENT
18 ABUNDANT
19 DEFICIENT
20 ABUNDANT
21 DEFICIENT
22 DEFICIENT
23 DEFICIENT
24 ABUNDANT
25 DEFICIENT
26 DEFICIENT
27 DEFICIENT
28 PERFECT
Press any key to continue...
i need then a percentage of how many perfect numbers i have, how many abundant and how many deficients.
i dont know how to do something like that, any ideas?

Ernest Friedman-Hill
author and iconoclast
Marshal
Posts: 24212
35
To compute the percentages, you'll need the total number of inputs, and the total number of results of each type. Are you currently keeping count of those? If not, then that's the first step: go do that.

apollo abel
Ranch Hand
Posts: 32
Ok, so the way it works is, i created a for loop,
for loop:

so then i did this:

i get the results i want, however, i dont know how to get the percentages,
is there like a formula?? and you said something about count them, i dont know what u mean by that.

Garrett Rowe
Ranch Hand
Posts: 1296
and you said something about count them, i dont know what u mean by that.
Inroduce a variable to keep track of how many of each type you have (in pseudo-code)

Ernest Friedman-Hill
author and iconoclast
Marshal
Posts: 24212
35
And the "formula" is just

(number of perfect)/(total number) * 100

apollo abel
Ranch Hand
Posts: 32
nvm
[ April 09, 2006: Message edited by: apollo abel ]

apollo abel
Ranch Hand
Posts: 32
perfectCount++,
it stores the numbers that are perfect,
so perfectCount/total*100
is total the number of all the numbers, including, perfect, abundant amd deficient??

Rusty Shackleford
Ranch Hand
Posts: 490
Originally posted by apollo abel:
perfectCount++,
it stores the numbers that are perfect,
so perfectCount/total*100
is total the number of all the numbers, including, perfect, abundant amd deficient??

No that is the percentage of perfect numbers. total, not surprisingly is the total of perfect, abundant and deficient numbers.

perfectCount/total == ratio of perfect numbers to the total, a decimal value is returned that is 0<=x<=1. The *100 just puts the value in standard percentage.

If you have 10 perfect, out of 25, that is 10/25*100=.4*100=40%. Of course the % is not automatically added to the resulting value.
[ April 09, 2006: Message edited by: Rusty Shackleford ]

apollo abel
Ranch Hand
Posts: 32
i got this errors:
required: variable
found : value
howdy.getPerfectCount()++;
^
required: variable
found : value
howdy.getAbundantCount()++;
^
required: variable
found : value
howdy.getDeficientCount()++;
what i tried to do was, i created some variables in the main class with the get methods:

and then i used them in the test class, however, i get the errors above mentioned, would you guys give me a clue as to why are im getting the errors??

thanks for the support.
[ April 09, 2006: Message edited by: apollo abel ]

Rusty Shackleford
Ranch Hand
Posts: 490
howdy.getPerfectCount()++

You are asking howdy for an value and then trying to increment it, the problem is there is no variable to place the value in, hence the complaints from the compiler.

You might try adding set methods in NumPad and incrementing there, instead of in main. The NumPad functionality should be in NumPad. Then you can retrieve them and manipulate them at the end.
[ April 09, 2006: Message edited by: Rusty Shackleford ]

apollo abel
Ranch Hand
Posts: 32
Originally posted by Rusty Shackleford:
howdy.getPerfectCount()++

You are asking howdy for an value and then trying to increment it, the problem is there is no variable to place the value in, hence the complaints from the compiler.

You might try adding set methods in NumPad and incrementing there, instead of in main. The NumPad functionality should be in NumPad. Then you can retrieve them and manipulate them at the end.

[ April 09, 2006: Message edited by: Rusty Shackleford ]

I've been trying and trying to move:

to the NumPAD class, however, it does not work, howdy.getPerfectCount()++; was my only hope, is there any other way around this?
the errors show:
required: variable
found : value
howdy.getPerfectCount()++;
^
required: variable
found : value
howdy.getAbundantCount()++;
^
required: variable
found : value
howdy.getDeficientCount()++;
they point at the "()", if i add something there would it work?

Rusty Shackleford
Ranch Hand
Posts: 490
You do not need to move all that to NumPad, and you should not.

What I meant was:

public void setPerfectCount()
{
perfectCount++;

}

Then instead of this ugly line, howdy.getPerfectCount()++, just use howdy.setPerfectCount();

Then when you are ready to make the calculations, just call getPerfectCount(). You can even add methods that calculates and returns all the values in NumPad if you like. Here is one possibility:

public int getTotalCount()
{
return perfectCount+abunduntCount+deficientCount;
}

If you insist on doing it the way you had it, you still need a variable so howdy.getPerfectCount()++ has something to place the value in.
perfectCount=howdy.getPerfectCount()++;

No, do not pass values to getPerfectCount(), it accepts no parameters and passing anything will result in more errors.

apollo abel
Ranch Hand
Posts: 32
Originally posted by Rusty Shackleford:
You do not need to move all that to NumPad, and you should not.

What I meant was:

public void setPerfectCount()
{
perfectCount++;

}

Then instead of this ugly line, howdy.getPerfectCount()++, just use howdy.setPerfectCount();

Then when you are ready to make the calculations, just call getPerfectCount(). You can even add methods that calculates and returns all the values in NumPad if you like. Here is one possibility:

public int getTotalCount()
{
return perfectCount+abunduntCount+deficientCount;
}

If you insist on doing it the way you had it, you still need a variable so howdy.getPerfectCount()++ has something to place the value in.
perfectCount=howdy.getPerfectCount()++;

No, do not pass values to getPerfectCount(), it accepts no parameters and passing anything will result in more errors.

Oh i see what you mean, well i can say i've learned something today, i'd go ahead and give it a try

apollo abel
Ranch Hand
Posts: 32
Well, i think i've got it this time, now i know why the difference between set and get, i used to use gets method for everything
the result i got:
Enter a number:
28
1 DEFICIENT
2 DEFICIENT
3 DEFICIENT
4 DEFICIENT
5 DEFICIENT
6 PERFECT
7 DEFICIENT
8 DEFICIENT
9 DEFICIENT
10 DEFICIENT
11 DEFICIENT
12 ABUNDANT
13 DEFICIENT
14 DEFICIENT
15 DEFICIENT
16 DEFICIENT
17 DEFICIENT
18 ABUNDANT
19 DEFICIENT
20 ABUNDANT
21 DEFICIENT
22 DEFICIENT
23 DEFICIENT
24 ABUNDANT
25 DEFICIENT
26 DEFICIENT
27 DEFICIENT
28 PERFECT
The percentage of perfect numbers is: 7.142857142857142
The percentage of abundant numbers is: 14.285714285714285
The percentage of deficient numbers is: 78.57142857142857
Press any key to continue...