Java/program help

Advertisement


Question
I'm working on an assignment that says,
" Complete the percentTrue() method, which calculates and returns the percentage (as a double) of the number of elements that have the value true in an array of boolean values."
I have absolutely no idea how to make this method. I've done everything else besides this and I was wondering if you could give me an idea how to do it.

Answer
The percent value is (# true) / (total number in array). You can get the total number in the array with (use your own variable names):
int arrayLength = array.length;

To determine the number of true element loop over the array. If an element is true, increment your # true counter (which you define outside of your loop and starts at 0).

Finally when you do your division, you have to convert the numerator or denominator to a double value, as otherwise it will round down to 0. You can do this by defining your arrayLength to be 0 or something like:

double precent = numberTrue / (double) totalNumber

Java

All Answers


Answers by Expert:


Ask Experts

Volunteer


Artemus Harper

Expertise

I have a Masters in computer science. I can answer questions on core J2SE, swing and graphics. Please no questions about JSP or J2ME.

Experience

I have experience in Core Java, good background in Java swing/gui, some experience with JNI, Java reflection. Some experience in bio-informatics. Basics in c++ and c#

Organizations
Washington State University

Education/Credentials
MS in Computer Science from Washington State University and a BS in Mathematics and Computer Science from Central Washington University.

©2016 About.com. All rights reserved.