Link to home
Start Free TrialLog in
Avatar of Jay Roy
Jay RoyFlag for United States of America

asked on

generate percentage in java

hi guys

I have a requirment where user has 30 answers to fill in a UI form. The answers are then stored in a java object AnswerVO. finally the answers are stored  in a list

List answerList = new ArrayList()
for int i=0; i< 30; i++
{
answerList.add(AnswerVO) --put all the answers in a list.
}

The answer is stored in AnswerVO
public Class AnswerVO {
public string answervalue;
set()
get()
}

Based on the number of answers filled out by users i have to generate a percentage.
any idea how best that can be done?

thanks
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

A percentage of what though?
If it's a percentage of the complete number of answers, it would be
double percent = n / 30.0 * 100; // (n is number of answers filled in)

Open in new window

Avatar of Jay Roy

ASKER

i have to check how many answers are NOT empty (meaning they are filled with answers) and then calcualte how many of the answers have been filled out of the 30 and generate a percentage completed
You probably check how many correct answers, say you have method
returning boolean for correct answer and then
something like that:

int count = 0;

for(int j=0; j<answerList.size(); j++){
AnswrVO aa = (AnswerVO)answerLits.get(j);
if(aa.correct())count++;
}

float percent = ((float)count/(float)answerList.size())*100.0

Sistem.out.println("Good answers: " + percent + "%");
 

Then you should hav ethe checck method if answer is responded
int count = 0;

for(int j=0; j<answerList.size(); j++){
AnswrVO aa = (AnswerVO)answerLits.get(j);
if(aa.getAnswerValue() != null)count++;
}

float percent = ((float)count/(float)answerList.size())*100.0

Sistem.out.println(" answers responded: " + percent + "%");
ASKER CERTIFIED SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
:)