Solved

# generate percentage in java

Posted on 2011-05-12
352 Views
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++
{
}

public Class AnswerVO {
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
Question by:royjayd

LVL 86

Expert Comment

A percentage of what though?
0

LVL 86

Expert Comment

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)
``````
0

Author Comment

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

LVL 47

Expert Comment

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++){
if(aa.correct())count++;
}

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

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

0

LVL 47

Expert Comment

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

for(int j=0; j<answerList.size(); j++){
}

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

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

LVL 86

Accepted Solution

Your code would be something like
``````List<AnswerVO> answers = ...;
if (userAnswer != null && userAnswer.trim().length() > 0) {
count++;
}
}
double percent = count / 30.0 * 100; // (don't use the magic number 30 though)
``````
0

LVL 47

Assisted Solution

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++){
}

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

Sistem.out.println(" answers responded: " + formatter.format(percent )+ "%");
0

LVL 4

Assisted Solution

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;

{
if(vo.get() != null)
{
}
else
{
}
}

public int getPercentage()
{
}

}
0

LVL 86

Expert Comment

:)
0

## Join & Write a Comment Already a member? Login.

### Suggested Solutions

Title # Comments Views Activity
classnotfoundexception 3 29
scoresIncreasing challenge 10 49
strCount chalenge 3 36
mapShare challenge 13 42
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

#### 745 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

#### Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!