Question

Write a program named Review.java and implement the following two methods: public static int NumberOfElementsLessThanAverage(int[] arr)...

Write a program named Review.java and implement the following two methods:

public static int NumberOfElementsLessThanAverage(int[] arr) that returns the number of elements that are less than the average of all elements of the array.

public static int[] countChars(File file), this method reads data from a file and counts the frequency of each alphabetic letters, and returns those numbers as an int array.

the data file name is "data.txt"

0 0
Add a comment Improve this question Transcribed image text
Answer #1


import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

/**
*
* @author Vamsi
*/
public class Review {
  
public static int NumberOfElementsLessThanAverage(int[] arr)
{
//first finding average
double av=0;
for(int i=0;i<arr.length;i++)av=av+arr[i];//ffinding sum
//finding average
av=av/arr.length;
  
int count=0;//to store the count of numbers less than the average
for(int i=0;i<arr.length;i++)if(arr[i]<av)count++;//counting
  
//returning count
return count;
  
}
  
public static int[] countChars(File file) throws FileNotFoundException, IOException
{
//creating frequency array
int feq[]=new int[26];//taking case insensitive
  
//to read file
BufferedReader br = new BufferedReader(new FileReader(file));
  
String st;//reading line by line
while ((st = br.readLine()) != null)
{
//now at each line finding char frequency
  
for(int i=0;i<st.length();i++)
{
  
char c=st.charAt(i);
int n = (int)c;
  
if(n<=91&&65<=n)//capital letters
feq[n-65]++;//increasing feqrency
else if(n<=123&&97<=n)//small letters
feq[n-97]++;
}
  
  
}
//returning frequency
return feq;
}
  
//method to test
public static void main(String argv[]) throws FileNotFoundException, IOException
{
  
File file = new File("C:\\Users\\Vamsi\\Desktop\\data.txt"); //change path as per ur requirement
int feq[] = countChars(file) ;
  
//displaying frequencies
for(int i=0;i<feq.length;i++)
{
char c = (char)(97+i);
System.out.println(c+":"+feq[i]);
  
}
  
  
int count=NumberOfElementsLessThanAverage(feq);
  
System.out.println("NumberOfElementsLessThanAverage :"+count);
}
  
  
}

output:

run:
a:2
b:0
c:0
d:0
e:4
f:0
g:1
h:1
i:0
j:0
k:1
l:2
m:1
n:0
o:3
p:0
q:0
r:2
s:1
t:1
u:2
v:0
w:1
x:0
y:1
z:0
NumberOfElementsLessThanAverage :12
BUILD SUCCESSFUL (total time: 1 second)


//PLS give a thumbs up if you find this helpful, its helps me alot, thanks.

Add a comment
Know the answer?
Add Answer to:
Write a program named Review.java and implement the following two methods: public static int NumberOfElementsLessThanAverage(int[] arr)...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a program named Remainder.java then implement the following method: public static int divisibleBy(int[] arr, int...

    Write a program named Remainder.java then implement the following method: public static int divisibleBy(int[] arr, int M, int K) this method determines the number of elements in the int array (arr) that are divisible by M but not K. Then write code inside main method to test your method: your main method accepts three numbers from the command argument list, N, M, K, use the first number to create int array with size of N, assign random number between 0...

  • (IN JAVA) Write a program named Review.java and implement the following methods: public static boolean isValidTicTacToeBoardString(String...

    (IN JAVA) Write a program named Review.java and implement the following methods: public static boolean isValidTicTacToeBoardString(String str) This method takes as its parameter a String that contains 9 characters representing the status of the Tic Tact Toe game. The String is mixed with X, O, x, o, and other letters. The first three letters represent the first row, letter 4 through letter 6 represent the second row, and the rest for the last row. To be considered as a valid...

  • must provide the following public interface: public static void insertSort(int [] arr); public static void selectSort(int...

    must provide the following public interface: public static void insertSort(int [] arr); public static void selectSort(int [] arr); public static void quickSort(int [] arr); public static void mergeSort(int [] arr); The quick sort and merge sort must be implemented by using recursive thinking. So the students may provide the following private static methods: //merge method //merge two sorted portions of given array arr, namely, from start to middle //and from middle + 1 to end into one sorted portion, namely,...

  • 5. (40 Points) Write a complete Java class named ArrayMethods that implements the methods listed below....

    5. (40 Points) Write a complete Java class named ArrayMethods that implements the methods listed below. All the methods accept the following parameters: Parameters: intar An array of int values. int firstIndex The index of the first element to include in the sum. int lastIndex The index of the last element to include in the sum. Please note that all methods must verify the validity of first Index and lastIndex. public static int sum(int[] arr, int first Index, int lastIndex)...

  • Write a program that instantiates an array of integers named scores. Let the size of the...

    Write a program that instantiates an array of integers named scores. Let the size of the array be 10. The program then first invokes randomFill to fill the scores array with random numbers in the range of 0 -100. Once the array is filled with data, methods below are called such that the output resembles the expected output given. The program keeps prompting the user to see if they want to evaluate a new set of random data. Find below...

  • Design and implement a Java program (name it ArrayMethods), that defines 4 methods as follows: int...

    Design and implement a Java program (name it ArrayMethods), that defines 4 methods as follows: int arrayMax (int [ ] arr) determines and returns the maximum value within an array int arrayMin (int [ ] arr) determines and returns the minimum value within an array void arraySquared (int [] arr) changes every value within the array to value2 void arrayReverse (int [ ] arr) reverses the array (for example: 7 8 12 becomes 2 18 7) Test your methods by...

  • Our 1st new array operation/method is remove. Implement as follows: public static boolean remove( int[] arr,...

    Our 1st new array operation/method is remove. Implement as follows: public static boolean remove( int[] arr, int count, int key ) { 1: find the index of the first occurance of key. By first occurance we mean lowest index that contains this value. hint: copy the indexOf() method from Lab#3 into the bottom of this project file and call it from inside this remove method. The you will have the index of the value to remove from the array 2:...

  • 14.8 Prog 8 Overload methods (find avg) Write a program to find the average of a...

    14.8 Prog 8 Overload methods (find avg) Write a program to find the average of a set numbers (assume that the numbers in the set is less than 20) using overload methods. The program first prompts the user to enter a character; if the character is 'I' or 'i', then invokes the appropriate methods to process integer data set; if the character is 'R' or 'r', then invokes the appropriate methods to process real number data set; if the inputted...

  • Python please! Exercise #3: Design and implement a program (name it ArrayMethods), that defines 4 methods...

    Python please! Exercise #3: Design and implement a program (name it ArrayMethods), that defines 4 methods as follows: int arrayMax(int[] arr) returns the maximum value in the an array int arrayMin(int[] arr) returns the minimum value in an array void arraySquared(int[] arr) changes every value in the array to its square (value²) void arrayReverse(int[] arr) reverses the array (for example: array storing 7 8 9 becomes 9 8 7 ) The program main method creates a single-dimensional array of length...

  • In Java* ​​​​​​​ Write a program that reads an arbitrary number of 20 integers that are...

    In Java* ​​​​​​​ Write a program that reads an arbitrary number of 20 integers that are in the range 0 to 100 inclusive. The program will ask a user to re-enter an integer if the user inputs a number outside of that range. The inputted integers must then be stored in a single dimensional array of size 20. Please create 3 methods: 1. Write a method public static int countEven(int[] inputArray) The method counts how many even numbers are in...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT