aspose file tools*
The moose likes Beginning Java and the fly likes Percentage issue Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Percentage issue" Watch "Percentage issue" New topic
Author

Percentage issue

apollo abel
Ranch Hand

Joined: Mar 31, 2006
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

Joined: Jul 08, 2003
Posts: 24185
    
  34

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.


[Jess in Action][AskingGoodQuestions]
apollo abel
Ranch Hand

Joined: Mar 31, 2006
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

Joined: Jan 17, 2006
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)


Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peter
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24185
    
  34

And the "formula" is just

(number of perfect)/(total number) * 100
apollo abel
Ranch Hand

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

Joined: Mar 31, 2006
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

Joined: Jan 03, 2006
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 ]

"Computer science is no more about computers than astronomy is about telescopes" - Edsger Dijkstra
apollo abel
Ranch Hand

Joined: Mar 31, 2006
Posts: 32
i got this errors:
F:\Codes\NumPADTester.java:27: unexpected type
required: variable
found : value
howdy.getPerfectCount()++;
^
F:\Codes\NumPADTester.java:32: unexpected type
required: variable
found : value
howdy.getAbundantCount()++;
^
F:\Codes\NumPADTester.java:37: unexpected type
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

Joined: Jan 03, 2006
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

Joined: Mar 31, 2006
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:
F:\Codes\NumPADTester.java:27: unexpected type
required: variable
found : value
howdy.getPerfectCount()++;
^
F:\Codes\NumPADTester.java:32: unexpected type
required: variable
found : value
howdy.getAbundantCount()++;
^
F:\Codes\NumPADTester.java:37: unexpected type
required: variable
found : value
howdy.getDeficientCount()++;
they point at the "()", if i add something there would it work?
Rusty Shackleford
Ranch Hand

Joined: Jan 03, 2006
Posts: 490
You do not need to move all that to NumPad, and you should not.

What I meant was:

in NumPad:

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

Joined: Mar 31, 2006
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:

in NumPad:

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

Joined: Mar 31, 2006
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...
Looks about right, doesnt it?
Oh and thanks alot for the support, you guys are the best
[ April 09, 2006: Message edited by: apollo abel ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Percentage issue