Question

In: Computer Science

Write a method that returns the percentage of  the number of elements that have the value true...

Write a method that returns the percentage of  the number of elements that have the value true in an array booleans.

Solutions

Expert Solution

SOLUTION -

CODE -

//java code
import java.lang.Math;
public class pT{
  
    public static float percentTrue(boolean[] a){
      
        float ct, pt;
        ct = 0;
        pt = 0;
        if(a.length == 0){
            System.out.println("Array is empty");
        }
        else{
            for(int i = 0; i < a.length; i++){
                if(a[i] == true)ct = ct + 1;
            }
            pt = ct/a.length * 100;
        }
        return pt;
    }
     public static void main(String []args){
         boolean[] array = {true, false, true, false, false, false, true};
         System.out.print("The elements are ");
         for(int i = 0; i < array.length; i++){
             System.out.print(array[i] + " ");
         }
        System.out.println("\nThe percentage of elements having the value true is " + Math.round(percentTrue(array) * 10) / 10.0 + "%");
     }
}

Output:

IF YOU HAVE ANY DOUBT PLEASE COMMENT DOWN BELOW I WILL SOLVE IT FOR YOU:)
----------------PLEASE RATE THE ANSWER-----------THANK YOU!!!!!!!!----------


Related Solutions

JAVA PLEASE!! Write a value-returning method, isVowel, that returns the value true if a given character...
JAVA PLEASE!! Write a value-returning method, isVowel, that returns the value true if a given character is a vowel, and otherwise returns false. Also write a program to test your method. 2) Write a program that prompts the user to input a sequence of characters and outputs the number of vowels. (Use the method isVowel written in Programming Exercise 1.)
write a method that returns the index of the second smallest element in an array of integers. If the number of such elements is greater than 1.
write a method that returns the index of the second smallest element in an array of integers. If the number of such elements is greater than 1. return the second smallest index. Use the following header:public static int index of seconds sma11eststenent tint array
write a recursive method that returns the product of all elements in java linked list
write a recursive method that returns the product of all elements in java linked list
Java Question 5: Count elements in the heap Write a function that returns the number of...
Java Question 5: Count elements in the heap Write a function that returns the number of elements in a min heap strictly less than a given number. Method signature: public static int elemNumHeap(PriorityQueue minHeap, int val) Please also include testers.
In Java Write a method countOddInternalNodes that returns the number of internal nodes in a binary...
In Java Write a method countOddInternalNodes that returns the number of internal nodes in a binary tree that contain odd numbers. You are essentially writing a method that will become part of the IntegerTree class. You may define private helper methods to solve this problem. Make your own trees in the main method of the code. I specifically kept vague the way the nodes are being added to the tree so you can practice building your own trees from scratch...
​Write a recursive method, vowels, that returns the number of vowels in a string. Also, write a program to test your method.
Write a recursive method, vowels, that returns the number of vowels in a string. Also, write a program to test your method.(JAVA Code)
write the “largerComponents” method that takes in two integer arrays and returns true or false if...
write the “largerComponents” method that takes in two integer arrays and returns true or false if the first array’s components are strictly greater than the second array’s components. The arrays must be the same size or else return false. Clarification Note: Components meaning each value at a specific index. For instance, if we had the arrays {5,2,7} and {1,3,1} then this method would return false as the value “2” is not greater than “3”. here is a provided code //Do...
write the method “getMaxValue” that finds and returns the maximum value in an integer linked list....
write the method “getMaxValue” that finds and returns the maximum value in an integer linked list. If the list is empty, then it should return 0. use the provided code below public class Question03 { public class ListNode//public for testing purposes { public int data;//public for testing purposes public ListNode link;//public for testing purposes public ListNode(int aData, ListNode aLink) { data = aData; link = aLink; } } public ListNode head;//public for testing purposes public int getMaxValue() { //----------------------------------------------------------------------------------- //Write...
Question 1: Write a method getSmallest(), which returns the smallest number in the linked list. Question...
Question 1: Write a method getSmallest(), which returns the smallest number in the linked list. Question 2: Write a member method getPosition(int entry) which returns the position of the entry is in the linked list. If the entry is not in the list, return -1. Please use C++ language for both questions, I only need functions.
Write a function hitRate(A, B) to return the percentage of elements in array A that match...
Write a function hitRate(A, B) to return the percentage of elements in array A that match the elements with the same index in array B. A and B are 1darrays or 2darrays of the same shape. Sample: if X = np.array([1, 3, 5, 8]); Y = np.array([2, 1, 3, 8]), then hitRate(X, Y) returns 0.25. in python program
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT