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

Aim: Create a Class containing several useful static methods that can be used fo

ID: 3779493 • Letter: A

Question

Aim: Create a Class containing several useful static methods that can be used for future programs and as practice for creating future methods. Each method must use the given Method header and must have java documentation. It should also include comments inside the method where appropriate.

1. Read File to String Method Header: public static String readFileToString(String file)

This method should take in the fully qualified path of a file and then read the file into a string. It should then return the string.

2. Write to a File Method Header: public static void writeToFile(String file, String contents)

This method should take in the fully qualified path of a file you wish to write to as well as a string containing text you would like to write to the file. Bonus (Up to 5 points): Use overloading to allow an optional third boolean parameter named append, that allows the caller to decide whether to overwrite the file with the new contents or just append them to the end of the file.

3. Sort an Array Method Header: public static int[] sortArray(int[] array)

This method should take in an unsorted array and sort it in ascending order (smallest to largest). It should return the sorted array.

4. Find a Value in an Array. Method Header: public static int findValueInArray(String[] searchArray, String value)

This method should take in an array of Strings to search and a value to search for. It should search through the array for the first element containing the value and and return the index of the first element containing the value. If the value is not found in the array, the method should return a -1.

5. Determine If a Positive Integer is Prime Method Header: public static boolean isPrime(int number)

A positive integer is considered prime if its only be divided evenly by 1 and itself. This method should take in an integer and determine if it is a prime number. If the number is prime the method should return true otherwise, it should return false.

Hint: 2 is the smallest prime number, so any number below 2 is not a prime.

Explanation / Answer

Here is the code for the first 4 methods:

import java.util.*;
import java.io.*;
class FileHandlingMethods
{
//1. Read File to String Method Header: public static String readFileToString(String file)
//This method should take in the fully qualified path of a file and then read the file into
//a string. It should then return the string.
public static String readFileToString(String file) throws FileNotFoundException
{
Scanner sc = new Scanner(new File(file));
String output = "";
while(sc.hasNextLine())
output += sc.nextLine();
return output;
}
//2. Write to a File Method Header: public static void writeToFile(String file, String contents)
//This method should take in the fully qualified path of a file you wish to write to as
//well as a string containing text you would like to write to the file. Bonus (Up to 5 points):
//Use overloading to allow an optional third boolean parameter named append, that allows the
//caller to decide whether to overwrite the file with the new contents or just append them to the end of the file.
public static void writeToFile(String file, String contents) throws FileNotFoundException
{
PrintWriter pw = new PrintWriter(new File(file));
pw.write(contents);
pw.close();
}
//3. Sort an Array Method Header: public static int[] sortArray(int[] array)
//This method should take in an unsorted array and sort it in ascending order
//(smallest to largest). It should return the sorted array.
public static int[] sortArray(int[] array)
{
boolean changed = true;
do
{
changed = false;
for(int j = 0; j < array.length-1; j++)
if(array[j] > array[j+1])
{
int temp = array[j];
array[j] = array[j+1];
array[j+1] = temp;
changed = true;
}
}while(changed);
return array;
}
//4. Find a Value in an Array. Method Header: public static int findValueInArray(String[] searchArray, String value)
//This method should take in an array of Strings to search and a value to search for.
//It should search through the array for the first element containing the value and and
//return the index of the first element containing the value. If the value
//is not found in the array, the method should return a -1.
public static int findValueInArray(String[] searchArray, String value)
{
for(int i = 0; i < searchArray.length; i++)
if(value.equals(searchArray[i]))
return i;
return -1;
}
}

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