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

Write the following methods in a Java program: a) A Java method Avg that returns

ID: 3877983 • Letter: W

Question

Write the following methods in a Java program:

a) A Java method Avg that returns the Average of multiples of a number N. The method input

parameters are an array A of numbers and the number N.

b) A Java method Sum that returns the sum of an array numbers greater than a number N. The method

input parameters are an array A of numbers and the number N.

c) A Java method OddArray that returns an array of odd numbers less than a value N given as parameter.

d) A Java method StringInFile that returns the number of times a string is present in a file. The filename

and the string are the parameters of the method.

e) A Java method called aboveAverage(...) of type integer that finds and returns how many numbers in an

array A as parameter are above the average of the array.

f) Test the above methods from the main method using the samples given in the class.

help me ....

Explanation / Answer

import java.io.*;
import java.util.*;
public class HelloWorld{

public static void main(String []args){
int A[]={1,2,3,4,5};
System.out.println("Average of numbers multiples of 2: "+Avg(A,2));
System.out.println("Sum of numbers greater than 2: "+Sum(A,2));
System.out.println("Odd array greater less than 5: "+Arrays.toString(OddArray(A,5)));
System.out.println("Above Average: "+aboveAverage(A));

}

public static double Avg(int A[], int N){
int sum = 0;
int count=0;
for(int i=0;i<A.length;i++){
if(A[i] % N == 0){ //checking for multiples for N
sum = sum+A[i];
count++;
}
}
double avg = sum/count;
return avg;
}
public static int Sum(int A[], int N){
int sum = 0;
for(int i=0;i<A.length;i++){
if(A[i]>N){ //checking if array number greater than N
sum = sum+A[i];
}
}
return sum;
}
public static int[] OddArray(int A[], int N){
int count = 0;
for(int i=0;i<A.length;i++){
if(A[i] < N && A[i]%2!=0){
count++;
}
}
int newArray[] = new int[count];
int pos=0;
for(int i=0;i<A.length;i++){
if(A[i] < N && A[i]%2!=0){
newArray[pos] = A[i];
pos++;
}
}
return newArray;
}
public static int StringInFile(String fileName, String sub){
int ind,count=0;
String line=null;
try{
// FileReader reads text files in the default encoding.
FileReader fileReader =
new FileReader(fileName);

// Always wrap FileReader in BufferedReader.
BufferedReader bufferedReader =
new BufferedReader(fileReader);

while((line = bufferedReader.readLine()) != null) {
for(int i=0; i+sub.length()<=line.length(); i++) //i+sub.length() is used to reduce comparisions
{
ind=line.indexOf(sub,i);
if(ind>=0)
{
count++;
i=ind;
ind=-1;
}
}

}

// Always close files.
bufferedReader.close();
return count;
}

catch(FileNotFoundException ex) {
System.out.println(
"Unable to open file '" +
fileName + "'");
return 0;
}
catch(IOException ex) {
System.out.println(
"Error reading file '"
+ fileName + "'");   
// Or we could just do this:
// ex.printStackTrace();
return 0;
}

}
public static int aboveAverage(int A[]){
int count = 0;
int sum=0;
for(int i=0;i<A.length;i++){
sum+=A[i];
}
double avg = sum/A.length;
for(int i=0;i<A.length;i++){
if(A[i]>avg){
count++;
}
}
return count;
}
}