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.
Create a Java method that takes a String as input value and returns the number of...
Create a Java method that takes a String as input value and returns the number of vowels contained in that string.
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...
1. Write a method called isPalindrome that accepts a string as a parameter and returns true...
1. Write a method called isPalindrome that accepts a string as a parameter and returns true if the string is a palindrome otherwise returns false. This method uses a stack and a Queue to test whether the given string parameter is a palindrome [ that is, whether the characters read the same both forward and backward. For example “race car”, and “Are we not drawn onward, to new era.” are Palindromes] They are palindrome sentences, not just a word. 2....
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT