[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 393
  • Last Modified:

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
0
royjayd
Asked:
royjayd
3 Solutions
 
CEHJCommented:
A percentage of what though?
0
 
CEHJCommented:
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

0
 
royjaydAuthor Commented:
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
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
for_yanCommented:
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 + "%");
 

0
 
for_yanCommented:
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 + "%");
0
 
CEHJCommented:
Your code would be something like
List<AnswerVO> answers = ...;
for(AnswerVO a : answers) {
	String userAnswer = a.getAnswerValue();
	if (userAnswer != null && userAnswer.trim().length() > 0) {
		count++;
	}
}
double percent = count / 30.0 * 100; // (don't use the magic number 30 though)

Open in new window

0
 
for_yanCommented:


Then you should hav ethe checck method if answer is responded
You just need to know what is the correcrt name of your method
for checking if there i answer:

int count = 0;

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

float percent = ((float)count/(float)answerList.size())*100.0
formatter = new DecimalFormat("#.#");

Sistem.out.println(" answers responded: " + formatter.format(percent )+ "%");
0
 
msk_apkCommented:
have a user object that wraps over AnswerVO. if percentage alone needed then there is no need to store the answer objects in the list at all. its enough to have two counts as correctAnswerCount or answeredCount and wrongAnsweredCount or notAnsweredCount.

public class User
{

     private int correctAnswerCount = 0;
     private int wrongAnswerCount = 0;

     public void addAnswer(AnswerVO vo)
    {
              if(vo.get() != null)
              {
                        correctAnswerCount ++;
              }
             else
             {
                         wrongAnswerCount++;
              }
     }

     public int getPercentage()
     {
                   return correctAnswerCount * 100/(correctAnswerCount + wrongAnswerCount);
      }

}
0
 
CEHJCommented:
:)
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now