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

Please help me solve following questions: (1)Write a function that returns the a

ID: 3582672 • Letter: P

Question

Please help me solve following questions:

(1)Write a function that returns the average of an array of integers. The function returns an int and is passed an array of ints and it's size as an int.

(2)Write a function that returns the average of a file that contains one integer per line. The function returns as int and is passed a string which contains the filename.

(3)Write a function that counts the number of occurrences of a specific character in a string. The function returns as int and is passed a string which is searched and a char which is searched for.

(4)Write a function that determines if there are three of the same value in a row in an array. The function returns a bool and is passed an array of ints and it's size as an int.

(5)Write a function that determines if there are three of the same line in a row in a file. The function returns a bool and is passed a string which contains the filename.

(6)Write a function that determines if there are two words that are the same in a row in a string. The function returns a bool and is passed a string.

Explanation / Answer

SOURCE CODE:

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;


public class Functions {
//Solution 1
public static int intAverage(int arr[],int size)
{
int sum=0;
for(int i=0;i<size;i++)
sum+=arr[i];
sum/=size;
return sum;
}
//Solution 2
public static int intFileAverage(String filename) throws FileNotFoundException, IOException
{
int count=0,avg=0;
FileInputStream fis = new FileInputStream(filename);
InputStreamReader isr = new InputStreamReader(fis);
BufferedReader br = new BufferedReader(isr);
String feed = br.readLine();
while (feed!=null)
{
count++;
avg+=Integer.parseInt(feed);
feed = null;
feed = br.readLine();
  
}
avg/=count;
br.close();
isr.close();
fis.close();
return avg;
}
//Solution 3
public static int noOfOccurrences(String str, char ch)
{
int count=0;
for(int i=0;i<str.length();i++)
if(str.charAt(i)==ch)
count++;
return count;
}
//Solution 4
public static boolean intThrice(int arr[],int size)
{
int max=arr[0];
for(int i=1;i<size;i++)
if(max<arr[i])
max=arr[i];
int array[]=new int[max+1];
for(int i=0;i<size;i++)
array[arr[i]]++;
for(int i=0;i<array.length;i++)
if(array[i]==3)
return true;
return false;
}
//Solution 5
public static boolean lineThrice(String filename) throws FileNotFoundException, IOException
{
String file="";
FileInputStream fis = new FileInputStream(filename);
InputStreamReader isr = new InputStreamReader(fis);
BufferedReader br = new BufferedReader(isr);
String feed = br.readLine();
while (feed!=null)
{
file=feed+"#"+file;
feed = null;
feed = br.readLine();
  
}
br.close();
isr.close();
fis.close();
String arr[]=file.split("#");
for(int i=0;i<arr.length;i++)
{
int index = file.lastIndexOf(arr[i]);
String temp=file.substring(0, index);
if(temp.contains(arr[i]))
{
index = temp.lastIndexOf(arr[i]);
temp=temp.substring(0, index);
if(temp.contains(arr[i]))
return true;
}
}
return false;
}
//Solution 6
public static boolean stringTwice(String str)
{

String arr[]=str.split(" ");
for(int i=0;i<arr.length;i++)
{
int index = str.lastIndexOf(str);
String temp=str.substring(0, index);
if(temp.contains(arr[i]))
return true;
}
return false;
}
  
public static void main(String args[]) throws FileNotFoundException, IOException
{
int a[]={3,4,6,7,0,12,4,5,1,6,4,9,6};
System.out.println("Average of the array :"+intAverage(a,a.length));
System.out.println("Average of the array extracted from file :"+intFileAverage("D:\ad.txt"));
System.out.println("Checking whether the given string contains two similar words :"+stringTwice("Chegg Chegg yoi"));
System.out.println("Checking whether the given string contains two similar words :"+stringTwice("Let me tell you a tale about a tail."));
System.out.println("Checking whether it contains three similar element :"+intThrice(a,a.length));
System.out.println("No. occurrences of a character in the given string :"+noOfOccurrences("Let me tell you a tale about a tail.",'a'));
System.out.println("Checking whether the given file contains the same line three times :"+lineThrice("D:\ad.txt"));
}
}

OUTPUT:

Average of the array :5
Average of the array extracted from file :13
Checking whether the given string contains two similar words :false
Checking whether the given string contains two similar words :false
Checking whether it contains three similar element :true
No. occurrences of a character in the given string :5
Checking whether the given file contains the same line three times :true

D:d.txt

4
6
7
8
9
9
2
5
6
78
23
1
7
45
4
9

[NOTE: Since it has been not specified what language to use, I have used Java.

The version used to compile this is jdk 1.7]