Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Need help for these 2 steps please. 1. Complete the TruePercent()method, which c

ID: 3546096 • Letter: N

Question

Need help for these 2 steps please.
1. Complete the TruePercent()method, which calculates and returns the percentage (as double) of the number of elements that have the value truein an array of boolean values. 2. Complete the change()method, which calculates and returns the difference between the largest and the smallest elements in an array of unsorted integers.
Sample Code:
import java.util.*;

public class Arrays
{
public static void main (String [ ] args)
{

Random r = new Random();


// Test the TruePercent() method
boolean [ ] vals = new boolean[20];
for (int i = 0; i < vals.length; i = i + 1)
{
int v = r.nextInt(2);
if (v == 0)
{

vals = false;
}

else
{
vals = true;
}
}

System.out.println("Percent true: " + TruePercent(vals));


// Test the change() method

int [ ] nums = new int[20];

for (int n = 0; n < nums.length; n = n + 1)
{
nums[n] = r.nextInt(100) + 1; // value in range 1-100
}

System.out.print("Original list of numbers: ");
printArray(nums);
System.out.println("Difference: " + change(nums));
}

public static double TruePercent(boolean [ ] values)
{
// COMPLETE THIS METHOD FOR STEP 1

{
return -1.0; // CHANGE THIS LINE
}
public static int change (int [ ] numbers)
{
// COMPLETE THIS METHOD FOR STEP 2
return -1; // CHANGE THIS LINE
}
} Need help for these 2 steps please.
1. Complete the TruePercent()method, which calculates and returns the percentage (as double) of the number of elements that have the value truein an array of boolean values. 2. Complete the change()method, which calculates and returns the difference between the largest and the smallest elements in an array of unsorted integers.
Sample Code:
import java.util.*;

public class Arrays
{
public static void main (String [ ] args)
{

Random r = new Random();


// Test the TruePercent() method
boolean [ ] vals = new boolean[20];
for (int i = 0; i < vals.length; i = i + 1)
{
int v = r.nextInt(2);
if (v == 0)
{

vals = false;
}

else
{
vals = true;
}
}

System.out.println("Percent true: " + TruePercent(vals));


// Test the change() method

int [ ] nums = new int[20];

for (int n = 0; n < nums.length; n = n + 1)
{
nums[n] = r.nextInt(100) + 1; // value in range 1-100
}

System.out.print("Original list of numbers: ");
printArray(nums);
System.out.println("Difference: " + change(nums));
}

public static double TruePercent(boolean [ ] values)
{
// COMPLETE THIS METHOD FOR STEP 1

{
return -1.0; // CHANGE THIS LINE
}
public static int change (int [ ] numbers)
{
// COMPLETE THIS METHOD FOR STEP 2
return -1; // CHANGE THIS LINE
}
} Complete the TruePercent()method, which calculates and returns the percentage (as double) of the number of elements that have the value truein an array of boolean values. 2. Complete the change()method, which calculates and returns the difference between the largest and the smallest elements in an array of unsorted integers.

Explanation / Answer

import java.util.*;


public class Array {

public static void main(String[] args) {


Random r = new Random();


// Test the TruePercent() method

boolean[] vals = new boolean[20];

for (int i = 0; i < vals.length; i = i + 1) {

int v = r.nextInt(2);

if (v == 0) {


vals[i] = false;

}


else {

vals[i] = true;

}

}

System.out.print("List: " );

for (int i = 0; i < vals.length; i++) {

System.out.print(vals[i] + " ");

}

System.out.println();

System.out.println("Percent true: " + TruePercent(vals));


// Test the change() method


int[] nums = new int[20];


for (int n = 0; n < nums.length; n = n + 1) {

nums[n] = r.nextInt(100) + 1; // value in range 1-100

}


System.out.print("Original list of numbers: ");

for (int i = 0; i < nums.length; i++) {

System.out.print(nums[i] + " ");

}

System.out.println();

System.out.println("Difference: " + change(nums));

}


public static double TruePercent(boolean[] values) {

double trues = 0;

for (int i = 0; i < values.length; i++) {

if (values[i]){

trues++;

}

}

return trues/ values.length;

}


public static int change(int[] numbers) {

int min = numbers[0];

int max = numbers[0];

for (int i = 0; i < numbers.length; i++) {

if(numbers[i] < min){

min = numbers[i];

}

if(numbers[i] > max){

max = numbers[i];

}

}

return max - min;

}

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote